Skip to content

Commit 6fb9b5f

Browse files
committed
Don't cache type strings now that inline strings are available
1 parent 0977356 commit 6fb9b5f

File tree

5 files changed

+50
-61
lines changed

5 files changed

+50
-61
lines changed

crates/derive/src/koto_impl.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::PREFIX_FUNCTION;
21
use proc_macro::TokenStream;
32
use quote::{format_ident, quote};
43
use syn::{
@@ -7,6 +6,8 @@ use syn::{
76
Type, TypePath,
87
};
98

9+
const PREFIX_FUNCTION: &str = "__koto_";
10+
1011
struct KotoImplParser {
1112
runtime_path: Path,
1213
}

crates/derive/src/koto_type.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::{attributes::koto_derive_attributes, PREFIX_STATIC};
1+
use crate::attributes::koto_derive_attributes;
22
use proc_macro::TokenStream;
3-
use quote::{format_ident, quote};
3+
use quote::quote;
44
use syn::{parse_macro_input, DeriveInput};
55

66
pub fn derive_koto_type(input: TokenStream) -> TokenStream {
@@ -14,23 +14,37 @@ pub fn derive_koto_type(input: TokenStream) -> TokenStream {
1414
.type_name
1515
.unwrap_or_else(|| quote!(#name).to_string());
1616

17-
let type_string_name = format_ident!("{PREFIX_STATIC}TYPE_STRING_{}", type_name.to_uppercase());
18-
19-
let result = quote! {
20-
#[automatically_derived]
21-
impl #impl_generics KotoType for #name #ty_generics #where_clause {
22-
fn type_static() -> &'static str {
23-
#type_name
24-
}
25-
26-
fn type_string(&self) -> KString {
27-
#type_string_name.with(KString::clone)
17+
// Short type names don't need to be cached, 22 is the `MAX_INLINE_STRING_LEN` constant
18+
let result = if type_name.len() <= 22 {
19+
quote! {
20+
#[automatically_derived]
21+
impl #impl_generics KotoType for #name #ty_generics #where_clause {
22+
fn type_static() -> &'static str {
23+
#type_name
24+
}
25+
26+
fn type_string(&self) -> KString {
27+
#type_name.into()
28+
}
2829
}
2930
}
31+
} else {
32+
quote! {
33+
#[automatically_derived]
34+
impl #impl_generics KotoType for #name #ty_generics #where_clause {
35+
fn type_static() -> &'static str {
36+
#type_name
37+
}
38+
39+
fn type_string(&self) -> KString {
40+
thread_local! {
41+
static TYPE_NAME: KString = #type_name.into();
42+
}
43+
44+
TYPE_NAME.with(KString::clone)
45+
}
46+
}
3047

31-
#[automatically_derived]
32-
thread_local! {
33-
static #type_string_name: KString = #type_name.into();
3448
}
3549
};
3650

crates/derive/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,3 @@ pub fn koto_impl(attr: TokenStream, item: TokenStream) -> TokenStream {
142142
pub fn koto_method(_attr: TokenStream, item: TokenStream) -> TokenStream {
143143
item
144144
}
145-
146-
const PREFIX_STATIC: &str = "__KOTO_";
147-
const PREFIX_FUNCTION: &str = "__koto_";

crates/runtime/src/io/stdio.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub struct DefaultStdin {}
77

88
impl KotoFile for DefaultStdin {
99
fn id(&self) -> KString {
10-
STDIN_ID.with(|id| id.clone())
10+
"_stdin_".into()
1111
}
1212
}
1313

@@ -35,7 +35,7 @@ pub struct DefaultStdout {}
3535

3636
impl KotoFile for DefaultStdout {
3737
fn id(&self) -> KString {
38-
STDOUT_ID.with(|id| id.clone())
38+
"_stdout_".into()
3939
}
4040
}
4141

@@ -63,7 +63,7 @@ pub struct DefaultStderr {}
6363

6464
impl KotoFile for DefaultStderr {
6565
fn id(&self) -> KString {
66-
STDERR_ID.with(|id| id.clone())
66+
"_stderr_".into()
6767
}
6868
}
6969

@@ -84,9 +84,3 @@ impl KotoWrite for DefaultStderr {
8484
io::stdout().flush().map_err(map_io_err)
8585
}
8686
}
87-
88-
thread_local! {
89-
static STDIN_ID: KString = "_stdin_".into();
90-
static STDOUT_ID: KString = "_stdout_".into();
91-
static STDERR_ID: KString = "_stderr_".into();
92-
}

crates/runtime/src/types/value.rs

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -156,34 +156,33 @@ impl KValue {
156156
/// Returns the value's type as a [KString]
157157
pub fn type_as_string(&self) -> KString {
158158
use KValue::*;
159+
159160
match &self {
160-
Null => TYPE_NULL.with(|x| x.clone()),
161-
Bool(_) => TYPE_BOOL.with(|x| x.clone()),
162-
Number(_) => TYPE_NUMBER.with(|x| x.clone()),
163-
List(_) => TYPE_LIST.with(|x| x.clone()),
164-
Range { .. } => TYPE_RANGE.with(|x| x.clone()),
161+
Null => "Null".into(),
162+
Bool(_) => "Bool".into(),
163+
Number(_) => "Number".into(),
164+
List(_) => "List".into(),
165+
Range { .. } => "Range".into(),
165166
Map(m) if m.meta_map().is_some() => match m.get_meta_value(&MetaKey::Type) {
166167
Some(Str(s)) => s,
167168
Some(_) => "Error: expected string as result of @type".into(),
168169
None => match m.get_meta_value(&MetaKey::Base) {
169170
Some(base @ Map(_)) => base.type_as_string(),
170-
_ => TYPE_OBJECT.with(|x| x.clone()),
171+
_ => "Object".into(),
171172
},
172173
},
173-
Map(_) => TYPE_MAP.with(|x| x.clone()),
174-
Str(_) => TYPE_STRING.with(|x| x.clone()),
175-
Tuple(_) => TYPE_TUPLE.with(|x| x.clone()),
176-
Function(f) if f.generator => TYPE_GENERATOR.with(|x| x.clone()),
177-
CaptureFunction(f) if f.info.generator => TYPE_GENERATOR.with(|x| x.clone()),
178-
Function(_) | CaptureFunction(_) | NativeFunction(_) => {
179-
TYPE_FUNCTION.with(|x| x.clone())
180-
}
174+
Map(_) => "Map".into(),
175+
Str(_) => "String".into(),
176+
Tuple(_) => "Tuple".into(),
177+
Function(f) if f.generator => "Generator".into(),
178+
CaptureFunction(f) if f.info.generator => "Generator".into(),
179+
Function(_) | CaptureFunction(_) | NativeFunction(_) => "Function".into(),
181180
Object(o) => o.try_borrow().map_or_else(
182181
|_| "Error: object already borrowed".into(),
183182
|o| o.type_string(),
184183
),
185-
Iterator(_) => TYPE_ITERATOR.with(|x| x.clone()),
186-
TemporaryTuple { .. } => TYPE_TEMPORARY_TUPLE.with(|x| x.clone()),
184+
Iterator(_) => "Iterator".into(),
185+
TemporaryTuple { .. } => "Temporary_tuple".into(),
187186
}
188187
}
189188

@@ -215,22 +214,6 @@ impl KValue {
215214
}
216215
}
217216

218-
thread_local! {
219-
static TYPE_NULL: KString = "Null".into();
220-
static TYPE_BOOL: KString = "Bool".into();
221-
static TYPE_NUMBER: KString = "Number".into();
222-
static TYPE_LIST: KString = "List".into();
223-
static TYPE_RANGE: KString = "Range".into();
224-
static TYPE_MAP: KString = "Map".into();
225-
static TYPE_OBJECT: KString = "Object".into();
226-
static TYPE_STRING: KString = "String".into();
227-
static TYPE_TUPLE: KString = "Tuple".into();
228-
static TYPE_FUNCTION: KString = "Function".into();
229-
static TYPE_GENERATOR: KString = "Generator".into();
230-
static TYPE_ITERATOR: KString = "Iterator".into();
231-
static TYPE_TEMPORARY_TUPLE: KString = "TemporaryTuple".into();
232-
}
233-
234217
impl fmt::Debug for KValue {
235218
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
236219
write!(f, "{}", self.type_as_string())

0 commit comments

Comments
 (0)