Skip to content

Commit ca15a59

Browse files
committed
Changing from uluru to HashMap
1 parent 3177fa9 commit ca15a59

File tree

2 files changed

+9
-30
lines changed

2 files changed

+9
-30
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ spans = ["wasm-bindgen-macro/spans"]
2525
std = []
2626
serde-serialize = ["serde", "serde_json", "std"]
2727
nightly = []
28-
enable-interning = ["std", "uluru"]
28+
enable-interning = ["std"]
2929

3030
# Whether or not the `#[wasm_bindgen]` macro is strict and generates an error on
3131
# all unused attributes
@@ -39,7 +39,6 @@ xxx_debug_only_print_generated_code = ["wasm-bindgen-macro/xxx_debug_only_print_
3939
wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.48" }
4040
serde = { version = "1.0", optional = true }
4141
serde_json = { version = "1.0", optional = true }
42-
uluru = { version = "0.3.0", optional = true }
4342
cfg-if = "0.1.9"
4443

4544
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]

src/cache/intern.rs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,37 @@ cfg_if! {
77
use std::string::String;
88
use std::borrow::ToOwned;
99
use std::cell::RefCell;
10+
use std::collections::HashMap;
1011
use crate::JsValue;
1112
use crate::convert::IntoWasmAbi;
12-
use uluru::{LRUCache, Entry};
13-
14-
15-
struct Pair {
16-
key: String,
17-
value: JsValue,
18-
}
19-
20-
// TODO figure out a good default capacity
21-
type Entries = LRUCache::<[Entry<Pair>; 1_024]>;
2213

2314
struct Cache {
24-
entries: RefCell<Entries>,
15+
entries: RefCell<HashMap<String, JsValue>>,
2516
}
2617

2718
thread_local! {
2819
static CACHE: Cache = Cache {
29-
entries: RefCell::new(LRUCache::default()),
20+
entries: RefCell::new(HashMap::new()),
3021
};
3122
}
3223

33-
fn get_js_string<'a>(cache: &'a mut Entries, key: &str) -> Option<&'a JsValue> {
34-
cache.find(|p| p.key == key).map(|x| &x.value)
35-
}
36-
3724
/// This returns the raw index of the cached JsValue, so you must take care
3825
/// so that you don't use it after it is freed.
3926
pub(crate) fn unsafe_get_str(s: &str) -> Option<<JsValue as IntoWasmAbi>::Abi> {
4027
CACHE.with(|cache| {
41-
let mut cache = cache.entries.borrow_mut();
28+
let cache = cache.entries.borrow();
4229

43-
if let Some(value) = get_js_string(&mut cache, s) {
44-
Some(value.into_abi())
45-
46-
} else {
47-
None
48-
}
30+
cache.get(s).map(|x| x.into_abi())
4931
})
5032
}
5133

5234
fn intern_str(key: &str) {
5335
CACHE.with(|cache| {
5436
let mut cache = cache.entries.borrow_mut();
5537

56-
if get_js_string(&mut cache, key).is_none() {
57-
cache.insert(Pair {
58-
key: key.to_owned(),
59-
value: JsValue::from(key),
60-
});
38+
// Can't use `entry` because `entry` requires a `String`
39+
if !cache.contains_key(key) {
40+
cache.insert(key.to_owned(), JsValue::from(key));
6141
}
6242
})
6343
}

0 commit comments

Comments
 (0)