Fixing some things for the cache

This commit is contained in:
Pauan
2019-07-02 17:10:51 +02:00
parent 0359da2060
commit f28cfc26fe
6 changed files with 95 additions and 111 deletions

View File

@ -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",

38
src/cache/intern.rs vendored
View File

@ -1,7 +1,12 @@
use cfg_if::cfg_if;
cfg_if! {
if #[cfg(feature = "enable-interning")] {
use std::thread_local;
use std::string::String;
use std::borrow::ToOwned;
use std::cell::{Cell, RefCell};
use std::cell::RefCell;
use crate::JsValue;
use uluru::{LRUCache, Entry};
@ -18,7 +23,6 @@ struct Cache {
entries: RefCell<Entries>,
}
// TODO figure out a good max_str_len
thread_local! {
static CACHE: Cache = Cache {
entries: RefCell::new(LRUCache::default()),
@ -29,14 +33,7 @@ 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();
@ -49,31 +46,26 @@ fn get_str(s: &str) -> JsValue {
})
}
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
}

View File

@ -315,13 +315,6 @@ impl IntoWasmAbi for JsValue {
}
}
impl OptionIntoWasmAbi for JsValue {
#[inline]
fn none() -> u32 {
32
}
}
impl FromWasmAbi for JsValue {
type Abi = u32;

View File

@ -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() }
}
}
}

View File

@ -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]> {

View File

@ -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.