mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-27 19:51:34 +00:00
Fixing some things for the cache
This commit is contained in:
@ -25,7 +25,7 @@ spans = ["wasm-bindgen-macro/spans"]
|
||||
std = []
|
||||
serde-serialize = ["serde", "serde_json", "std"]
|
||||
nightly = []
|
||||
disable-interning = []
|
||||
enable-interning = ["std"]
|
||||
|
||||
# Whether or not the `#[wasm_bindgen]` macro is strict and generates an error on
|
||||
# all unused attributes
|
||||
@ -52,7 +52,6 @@ wasm-bindgen-test-crate-b = { path = 'tests/crates/b', version = '0.1' }
|
||||
[workspace]
|
||||
members = [
|
||||
"benchmarks",
|
||||
"crates/cache",
|
||||
"crates/cli",
|
||||
"crates/js-sys",
|
||||
"crates/test",
|
||||
|
72
src/cache/intern.rs
vendored
72
src/cache/intern.rs
vendored
@ -1,42 +1,39 @@
|
||||
use std::thread_local;
|
||||
use std::string::String;
|
||||
use std::borrow::ToOwned;
|
||||
use std::cell::{Cell, RefCell};
|
||||
use crate::JsValue;
|
||||
use uluru::{LRUCache, Entry};
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
|
||||
struct Pair {
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "enable-interning")] {
|
||||
use std::thread_local;
|
||||
use std::string::String;
|
||||
use std::borrow::ToOwned;
|
||||
use std::cell::RefCell;
|
||||
use crate::JsValue;
|
||||
use uluru::{LRUCache, Entry};
|
||||
|
||||
|
||||
struct Pair {
|
||||
key: String,
|
||||
value: JsValue,
|
||||
}
|
||||
}
|
||||
|
||||
// TODO figure out a good default capacity
|
||||
type Entries = LRUCache::<[Entry<Pair>; 1_024]>;
|
||||
// TODO figure out a good default capacity
|
||||
type Entries = LRUCache::<[Entry<Pair>; 1_024]>;
|
||||
|
||||
struct Cache {
|
||||
struct Cache {
|
||||
entries: RefCell<Entries>,
|
||||
}
|
||||
}
|
||||
|
||||
// TODO figure out a good max_str_len
|
||||
thread_local! {
|
||||
thread_local! {
|
||||
static CACHE: Cache = Cache {
|
||||
entries: RefCell::new(LRUCache::default()),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn get_js_string<'a>(cache: &'a mut Entries, key: &str) -> Option<&'a JsValue> {
|
||||
fn get_js_string<'a>(cache: &'a mut Entries, key: &str) -> Option<&'a JsValue> {
|
||||
cache.find(|p| p.key == key).map(|x| &x.value)
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_js_string(cache: &mut Entries, key: &str, value: JsValue) {
|
||||
cache.insert(Pair {
|
||||
key: key.to_owned(),
|
||||
value,
|
||||
});
|
||||
}
|
||||
|
||||
fn get_str(s: &str) -> JsValue {
|
||||
pub(crate) fn get_str(s: &str) -> JsValue {
|
||||
CACHE.with(|cache| {
|
||||
let mut cache = cache.entries.borrow_mut();
|
||||
|
||||
@ -47,33 +44,28 @@ fn get_str(s: &str) -> JsValue {
|
||||
JsValue::from(s)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn intern_str(s: &str) {
|
||||
fn intern_str(key: &str) {
|
||||
CACHE.with(|cache| {
|
||||
let mut cache = cache.entries.borrow_mut();
|
||||
|
||||
if get_js_string(&mut cache, s).is_none() {
|
||||
insert_js_string(&mut cache, s, JsValue::from(s));
|
||||
if get_js_string(&mut cache, key).is_none() {
|
||||
cache.insert(Pair {
|
||||
key: key.to_owned(),
|
||||
value: JsValue::from(key),
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn str(s: &str) -> JsValue {
|
||||
if cfg!(feature = "disable-interning") {
|
||||
JsValue::from(s)
|
||||
|
||||
} else {
|
||||
get_str(s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
pub fn intern(s: &str) -> &str {
|
||||
if !cfg!(feature = "disable-interning") {
|
||||
#[cfg(feature = "enable-interning")]
|
||||
intern_str(s);
|
||||
}
|
||||
|
||||
s
|
||||
}
|
||||
|
@ -315,13 +315,6 @@ impl IntoWasmAbi for JsValue {
|
||||
}
|
||||
}
|
||||
|
||||
impl OptionIntoWasmAbi for JsValue {
|
||||
#[inline]
|
||||
fn none() -> u32 {
|
||||
32
|
||||
}
|
||||
}
|
||||
|
||||
impl FromWasmAbi for JsValue {
|
||||
type Abi = u32;
|
||||
|
||||
|
@ -150,28 +150,13 @@ if_std! {
|
||||
}
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "disable-interning")] {
|
||||
impl IntoWasmAbi for String {
|
||||
type Abi = <Vec<u8> as IntoWasmAbi>::Abi;
|
||||
|
||||
#[inline]
|
||||
fn into_abi(self) -> Self::Abi {
|
||||
self.into_bytes().into_abi()
|
||||
}
|
||||
}
|
||||
|
||||
impl OptionIntoWasmAbi for String {
|
||||
#[inline]
|
||||
fn none() -> Self::Abi { null_slice() }
|
||||
}
|
||||
|
||||
} else {
|
||||
if #[cfg(feature = "enable-interning")] {
|
||||
impl IntoWasmAbi for String {
|
||||
type Abi = <JsValue as IntoWasmAbi>::Abi;
|
||||
|
||||
#[inline]
|
||||
fn into_abi(self, extra: &mut dyn Stack) -> Self::Abi {
|
||||
crate::cache::intern::str(&self).into_abi(extra)
|
||||
fn into_abi(self) -> Self::Abi {
|
||||
crate::cache::intern::get_str(&self).into_abi()
|
||||
}
|
||||
}
|
||||
|
||||
@ -181,6 +166,21 @@ if_std! {
|
||||
<JsValue as OptionIntoWasmAbi>::none()
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
impl IntoWasmAbi for String {
|
||||
type Abi = <Vec<u8> as IntoWasmAbi>::Abi;
|
||||
|
||||
#[inline]
|
||||
fn into_abi(self, extra: &mut dyn Stack) -> Self::Abi {
|
||||
self.into_bytes().into_abi(extra)
|
||||
}
|
||||
}
|
||||
|
||||
impl OptionIntoWasmAbi for String {
|
||||
#[inline]
|
||||
fn none() -> Self::Abi { null_slice() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -200,27 +200,13 @@ if_std! {
|
||||
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "disable-interning")] {
|
||||
impl<'a> IntoWasmAbi for &'a str {
|
||||
type Abi = <&'a [u8] as IntoWasmAbi>::Abi;
|
||||
|
||||
#[inline]
|
||||
fn into_abi(self) -> Self::Abi {
|
||||
self.as_bytes().into_abi()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> OptionIntoWasmAbi for &'a str {
|
||||
fn none() -> Self::Abi { null_slice() }
|
||||
}
|
||||
|
||||
} else {
|
||||
if #[cfg(feature = "enable-interning")] {
|
||||
impl<'a> IntoWasmAbi for &'a str {
|
||||
type Abi = <JsValue as IntoWasmAbi>::Abi;
|
||||
|
||||
#[inline]
|
||||
fn into_abi(self, extra: &mut dyn Stack) -> Self::Abi {
|
||||
crate::cache::intern::str(self).into_abi(extra)
|
||||
fn into_abi(self) -> Self::Abi {
|
||||
crate::cache::intern::get_str(self).into_abi()
|
||||
}
|
||||
}
|
||||
|
||||
@ -230,6 +216,20 @@ cfg_if! {
|
||||
<JsValue as OptionIntoWasmAbi>::none()
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
impl<'a> IntoWasmAbi for &'a str {
|
||||
type Abi = <&'a [u8] as IntoWasmAbi>::Abi;
|
||||
|
||||
#[inline]
|
||||
fn into_abi(self, extra: &mut dyn Stack) -> Self::Abi {
|
||||
self.as_bytes().into_abi(extra)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> OptionIntoWasmAbi for &'a str {
|
||||
fn none() -> Self::Abi { null_slice() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -75,7 +75,7 @@ simple! {
|
||||
f64 => F64
|
||||
bool => BOOLEAN
|
||||
char => CHAR
|
||||
str => if cfg!(feature = "disable-interning") { STRING } else { ANYREF }
|
||||
str => if cfg!(feature = "enable-interning") { ANYREF } else { STRING }
|
||||
JsValue => ANYREF
|
||||
}
|
||||
|
||||
@ -116,7 +116,7 @@ if_std! {
|
||||
use std::prelude::v1::*;
|
||||
|
||||
impl WasmDescribe for String {
|
||||
fn describe() { inform(if cfg!(feature = "disable-interning") { STRING } else { ANYREF }) }
|
||||
fn describe() { inform(if cfg!(feature = "enable-interning") { ANYREF } else { STRING }) }
|
||||
}
|
||||
|
||||
impl<T: WasmDescribe> WasmDescribe for Box<[T]> {
|
||||
|
@ -57,13 +57,10 @@ pub mod prelude {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
mod cache;
|
||||
|
||||
pub mod convert;
|
||||
pub mod describe;
|
||||
|
||||
pub use cache::intern::intern;
|
||||
|
||||
mod cast;
|
||||
pub use crate::cast::JsCast;
|
||||
|
||||
@ -72,6 +69,9 @@ if_std! {
|
||||
use std::prelude::v1::*;
|
||||
pub mod closure;
|
||||
mod anyref;
|
||||
|
||||
mod cache;
|
||||
pub use cache::intern::intern;
|
||||
}
|
||||
|
||||
/// Representation of an object owned by JS.
|
||||
|
Reference in New Issue
Block a user