Skip to content

Commit 30c048b

Browse files
committed
Making interning manual
1 parent 84b444d commit 30c048b

File tree

2 files changed

+29
-40
lines changed

2 files changed

+29
-40
lines changed

src/cache/intern.rs

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,78 +15,65 @@ struct Pair {
1515
type Entries = LRUCache::<[Entry<Pair>; 1_024]>;
1616

1717
struct Cache {
18-
enabled: Cell<bool>,
19-
max_str_len: Cell<usize>,
2018
entries: RefCell<Entries>,
2119
}
2220

2321
// TODO figure out a good max_str_len
2422
thread_local! {
2523
static CACHE: Cache = Cache {
26-
enabled: Cell::new(true),
27-
max_str_len: Cell::new(128),
2824
entries: RefCell::new(LRUCache::default()),
2925
};
3026
}
3127

32-
fn get_js_string(cache: &mut Entries, key: &str) -> JsValue {
33-
if let Some(p) = cache.find(|p| p.key == key) {
34-
p.value.clone()
35-
36-
} else {
37-
let value = JsValue::from(key);
38-
39-
cache.insert(Pair {
40-
key: key.to_owned(),
41-
value: value.clone(),
42-
});
28+
fn get_js_string<'a>(cache: &'a mut Entries, key: &str) -> Option<&'a JsValue> {
29+
cache.find(|p| p.key == key).map(|x| &x.value)
30+
}
4331

44-
value
45-
}
32+
fn insert_js_string(cache: &mut Entries, key: &str, value: JsValue) {
33+
cache.insert(Pair {
34+
key: key.to_owned(),
35+
value,
36+
});
4637
}
4738

48-
fn cache_str(s: &str) -> JsValue {
39+
fn get_str(s: &str) -> JsValue {
4940
CACHE.with(|cache| {
50-
let should_cache =
51-
cache.enabled.get() &&
52-
s.len() <= cache.max_str_len.get();
41+
let mut cache = cache.entries.borrow_mut();
5342

54-
if should_cache {
55-
get_js_string(&mut cache.entries.borrow_mut(), s)
43+
if let Some(value) = get_js_string(&mut cache, s) {
44+
value.clone()
5645

5746
} else {
5847
JsValue::from(s)
5948
}
6049
})
6150
}
6251

52+
fn intern_str(s: &str) {
53+
CACHE.with(|cache| {
54+
let mut cache = cache.entries.borrow_mut();
55+
56+
if get_js_string(&mut cache, s).is_none() {
57+
insert_js_string(&mut cache, s, JsValue::from(s));
58+
}
59+
})
60+
}
61+
6362
#[inline]
64-
pub fn str(s: &str) -> JsValue {
63+
pub(crate) fn str(s: &str) -> JsValue {
6564
if cfg!(feature = "disable-interning") {
6665
JsValue::from(s)
6766

6867
} else {
69-
cache_str(s)
70-
}
71-
}
72-
73-
#[inline]
74-
pub fn set_max_str_len(len: usize) {
75-
if !cfg!(feature = "disable-interning") {
76-
CACHE.with(|cache| cache.max_str_len.set(len));
68+
get_str(s)
7769
}
7870
}
7971

8072
#[inline]
81-
pub fn enable() {
73+
pub fn intern(s: &str) -> &str {
8274
if !cfg!(feature = "disable-interning") {
83-
CACHE.with(|cache| cache.enabled.set(true));
75+
intern_str(s);
8476
}
85-
}
8677

87-
#[inline]
88-
pub fn disable() {
89-
if !cfg!(feature = "disable-interning") {
90-
CACHE.with(|cache| cache.enabled.set(false));
91-
}
78+
s
9279
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ mod cache;
6262
pub mod convert;
6363
pub mod describe;
6464

65+
pub use cache::intern::intern;
66+
6567
mod cast;
6668
pub use crate::cast::JsCast;
6769

0 commit comments

Comments
 (0)