mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2025-01-05 19:53:55 +03:00
Changing from uluru to HashMap
This commit is contained in:
parent
3177fa9edc
commit
ca15a59ace
@ -25,7 +25,7 @@ spans = ["wasm-bindgen-macro/spans"]
|
|||||||
std = []
|
std = []
|
||||||
serde-serialize = ["serde", "serde_json", "std"]
|
serde-serialize = ["serde", "serde_json", "std"]
|
||||||
nightly = []
|
nightly = []
|
||||||
enable-interning = ["std", "uluru"]
|
enable-interning = ["std"]
|
||||||
|
|
||||||
# Whether or not the `#[wasm_bindgen]` macro is strict and generates an error on
|
# Whether or not the `#[wasm_bindgen]` macro is strict and generates an error on
|
||||||
# all unused attributes
|
# all unused attributes
|
||||||
@ -39,7 +39,6 @@ xxx_debug_only_print_generated_code = ["wasm-bindgen-macro/xxx_debug_only_print_
|
|||||||
wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.48" }
|
wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.48" }
|
||||||
serde = { version = "1.0", optional = true }
|
serde = { version = "1.0", optional = true }
|
||||||
serde_json = { version = "1.0", optional = true }
|
serde_json = { version = "1.0", optional = true }
|
||||||
uluru = { version = "0.3.0", optional = true }
|
|
||||||
cfg-if = "0.1.9"
|
cfg-if = "0.1.9"
|
||||||
|
|
||||||
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
|
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
|
||||||
|
36
src/cache/intern.rs
vendored
36
src/cache/intern.rs
vendored
@ -7,45 +7,27 @@ cfg_if! {
|
|||||||
use std::string::String;
|
use std::string::String;
|
||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use std::collections::HashMap;
|
||||||
use crate::JsValue;
|
use crate::JsValue;
|
||||||
use crate::convert::IntoWasmAbi;
|
use crate::convert::IntoWasmAbi;
|
||||||
use uluru::{LRUCache, Entry};
|
|
||||||
|
|
||||||
|
|
||||||
struct Pair {
|
|
||||||
key: String,
|
|
||||||
value: JsValue,
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO figure out a good default capacity
|
|
||||||
type Entries = LRUCache::<[Entry<Pair>; 1_024]>;
|
|
||||||
|
|
||||||
struct Cache {
|
struct Cache {
|
||||||
entries: RefCell<Entries>,
|
entries: RefCell<HashMap<String, JsValue>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
static CACHE: Cache = Cache {
|
static CACHE: Cache = Cache {
|
||||||
entries: RefCell::new(LRUCache::default()),
|
entries: RefCell::new(HashMap::new()),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_js_string<'a>(cache: &'a mut Entries, key: &str) -> Option<&'a JsValue> {
|
|
||||||
cache.find(|p| p.key == key).map(|x| &x.value)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This returns the raw index of the cached JsValue, so you must take care
|
/// This returns the raw index of the cached JsValue, so you must take care
|
||||||
/// so that you don't use it after it is freed.
|
/// so that you don't use it after it is freed.
|
||||||
pub(crate) fn unsafe_get_str(s: &str) -> Option<<JsValue as IntoWasmAbi>::Abi> {
|
pub(crate) fn unsafe_get_str(s: &str) -> Option<<JsValue as IntoWasmAbi>::Abi> {
|
||||||
CACHE.with(|cache| {
|
CACHE.with(|cache| {
|
||||||
let mut cache = cache.entries.borrow_mut();
|
let cache = cache.entries.borrow();
|
||||||
|
|
||||||
if let Some(value) = get_js_string(&mut cache, s) {
|
cache.get(s).map(|x| x.into_abi())
|
||||||
Some(value.into_abi())
|
|
||||||
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,11 +35,9 @@ cfg_if! {
|
|||||||
CACHE.with(|cache| {
|
CACHE.with(|cache| {
|
||||||
let mut cache = cache.entries.borrow_mut();
|
let mut cache = cache.entries.borrow_mut();
|
||||||
|
|
||||||
if get_js_string(&mut cache, key).is_none() {
|
// Can't use `entry` because `entry` requires a `String`
|
||||||
cache.insert(Pair {
|
if !cache.contains_key(key) {
|
||||||
key: key.to_owned(),
|
cache.insert(key.to_owned(), JsValue::from(key));
|
||||||
value: JsValue::from(key),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user