From f28cfc26fe28f5c87b32387415aedc52eea14cb8 Mon Sep 17 00:00:00 2001 From: Pauan Date: Tue, 2 Jul 2019 17:10:51 +0200 Subject: [PATCH] Fixing some things for the cache --- Cargo.toml | 3 +- src/cache/intern.rs | 114 ++++++++++++++++++++---------------------- src/convert/impls.rs | 7 --- src/convert/slices.rs | 70 +++++++++++++------------- src/describe.rs | 4 +- src/lib.rs | 8 +-- 6 files changed, 95 insertions(+), 111 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a216c0b5..83415e86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", diff --git a/src/cache/intern.rs b/src/cache/intern.rs index 02484d4d..ac3fb6f8 100644 --- a/src/cache/intern.rs +++ b/src/cache/intern.rs @@ -1,79 +1,71 @@ -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 { - key: String, - value: JsValue, -} +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}; -// TODO figure out a good default capacity -type Entries = LRUCache::<[Entry; 1_024]>; -struct Cache { - entries: RefCell, -} - -// TODO figure out a good max_str_len -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> { - 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 { - CACHE.with(|cache| { - let mut cache = cache.entries.borrow_mut(); - - if let Some(value) = get_js_string(&mut cache, s) { - value.clone() - - } else { - JsValue::from(s) + struct Pair { + key: String, + value: JsValue, } - }) -} -fn intern_str(s: &str) { - CACHE.with(|cache| { - let mut cache = cache.entries.borrow_mut(); + // TODO figure out a good default capacity + type Entries = LRUCache::<[Entry; 1_024]>; - if get_js_string(&mut cache, s).is_none() { - insert_js_string(&mut cache, s, JsValue::from(s)); + struct Cache { + entries: RefCell, } - }) -} -#[inline] -pub(crate) fn str(s: &str) -> JsValue { - if cfg!(feature = "disable-interning") { - JsValue::from(s) + thread_local! { + static CACHE: Cache = Cache { + entries: RefCell::new(LRUCache::default()), + }; + } - } else { - get_str(s) + fn get_js_string<'a>(cache: &'a mut Entries, key: &str) -> Option<&'a JsValue> { + cache.find(|p| p.key == key).map(|x| &x.value) + } + + pub(crate) fn get_str(s: &str) -> JsValue { + CACHE.with(|cache| { + let mut cache = cache.entries.borrow_mut(); + + if let Some(value) = get_js_string(&mut cache, s) { + value.clone() + + } else { + JsValue::from(s) + } + }) + } + + fn intern_str(key: &str) { + CACHE.with(|cache| { + let mut cache = cache.entries.borrow_mut(); + + if get_js_string(&mut cache, key).is_none() { + cache.insert(Pair { + key: key.to_owned(), + value: JsValue::from(key), + }); + } + }) + } } } + #[inline] pub fn intern(s: &str) -> &str { - if !cfg!(feature = "disable-interning") { - intern_str(s); - } + #[cfg(feature = "enable-interning")] + intern_str(s); s } diff --git a/src/convert/impls.rs b/src/convert/impls.rs index 5556fd3c..4161e4a9 100644 --- a/src/convert/impls.rs +++ b/src/convert/impls.rs @@ -315,13 +315,6 @@ impl IntoWasmAbi for JsValue { } } -impl OptionIntoWasmAbi for JsValue { - #[inline] - fn none() -> u32 { - 32 - } -} - impl FromWasmAbi for JsValue { type Abi = u32; diff --git a/src/convert/slices.rs b/src/convert/slices.rs index 2e87d688..fee728a7 100644 --- a/src/convert/slices.rs +++ b/src/convert/slices.rs @@ -150,28 +150,13 @@ if_std! { } cfg_if! { - if #[cfg(feature = "disable-interning")] { - impl IntoWasmAbi for String { - type Abi = 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 = ::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! { ::none() } } + + } else { + impl IntoWasmAbi for String { + type Abi = 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 = ::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! { ::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() } + } } } diff --git a/src/describe.rs b/src/describe.rs index 5df5d764..f737aa99 100644 --- a/src/describe.rs +++ b/src/describe.rs @@ -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 WasmDescribe for Box<[T]> { diff --git a/src/lib.rs b/src/lib.rs index ad544e70..a65a0930 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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.