Skip to content

Commit

Permalink
Fixing some things for the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Pauan committed Jul 16, 2019
1 parent 3d06f9e commit ae71087
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 92 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down
108 changes: 50 additions & 58 deletions src/cache/intern.rs
Original file line number Diff line number Diff line change
@@ -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,
}

// TODO figure out a good default capacity
type Entries = LRUCache::<[Entry<Pair>; 1_024]>;
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 Cache {
entries: RefCell<Entries>,
}

// 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)
}
struct Pair {
key: String,
value: JsValue,
}

fn insert_js_string(cache: &mut Entries, key: &str, value: JsValue) {
cache.insert(Pair {
key: key.to_owned(),
value,
});
}
// TODO figure out a good default capacity
type Entries = LRUCache::<[Entry<Pair>; 1_024]>;

fn get_str(s: &str) -> JsValue {
CACHE.with(|cache| {
let mut cache = cache.entries.borrow_mut();
struct Cache {
entries: RefCell<Entries>,
}

if let Some(value) = get_js_string(&mut cache, s) {
value.clone()
thread_local! {
static CACHE: Cache = Cache {
entries: RefCell::new(LRUCache::default()),
};
}

} else {
JsValue::from(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)
}
})
}

fn intern_str(s: &str) {
CACHE.with(|cache| {
let mut cache = cache.entries.borrow_mut();
pub(crate) fn get_str(s: &str) -> JsValue {
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 let Some(value) = get_js_string(&mut cache, s) {
value.clone()

#[inline]
pub(crate) fn str(s: &str) -> JsValue {
if cfg!(feature = "disable-interning") {
JsValue::from(s)
} else {
JsValue::from(s)
}
})
}

} else {
get_str(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
}
7 changes: 0 additions & 7 deletions src/convert/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,6 @@ impl IntoWasmAbi for JsValue {
}
}

impl OptionIntoWasmAbi for JsValue {
#[inline]
fn none() -> u32 {
32
}
}

impl FromWasmAbi for JsValue {
type Abi = u32;

Expand Down
38 changes: 19 additions & 19 deletions src/convert/slices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,36 +150,36 @@ if_std! {
}

cfg_if! {
if #[cfg(feature = "disable-interning")] {
if #[cfg(feature = "enable-interning")] {
impl IntoWasmAbi for String {
type Abi = <Vec<u8> as IntoWasmAbi>::Abi;
type Abi = <JsValue as IntoWasmAbi>::Abi;

#[inline]
fn into_abi(self) -> Self::Abi {
self.into_bytes().into_abi()
crate::cache::intern::get_str(&self).into_abi()
}
}

impl OptionIntoWasmAbi for String {
#[inline]
fn none() -> Self::Abi { null_slice() }
fn none() -> Self::Abi {
<JsValue as OptionIntoWasmAbi>::none()
}
}

} else {
impl IntoWasmAbi for String {
type Abi = <JsValue as IntoWasmAbi>::Abi;
type Abi = <Vec<u8> as IntoWasmAbi>::Abi;

#[inline]
fn into_abi(self, extra: &mut dyn Stack) -> Self::Abi {
crate::cache::intern::str(&self).into_abi(extra)
self.into_bytes().into_abi(extra)
}
}

impl OptionIntoWasmAbi for String {
#[inline]
fn none() -> Self::Abi {
<JsValue as OptionIntoWasmAbi>::none()
}
fn none() -> Self::Abi { null_slice() }
}
}
}
Expand All @@ -200,35 +200,35 @@ if_std! {


cfg_if! {
if #[cfg(feature = "disable-interning")] {
if #[cfg(feature = "enable-interning")] {
impl<'a> IntoWasmAbi for &'a str {
type Abi = <&'a [u8] as IntoWasmAbi>::Abi;
type Abi = <JsValue as IntoWasmAbi>::Abi;

#[inline]
fn into_abi(self) -> Self::Abi {
self.as_bytes().into_abi()
crate::cache::intern::get_str(self).into_abi()
}
}

impl<'a> OptionIntoWasmAbi for &'a str {
fn none() -> Self::Abi { null_slice() }
#[inline]
fn none() -> Self::Abi {
<JsValue as OptionIntoWasmAbi>::none()
}
}

} else {
impl<'a> IntoWasmAbi for &'a str {
type Abi = <JsValue as IntoWasmAbi>::Abi;
type Abi = <&'a [u8] as IntoWasmAbi>::Abi;

#[inline]
fn into_abi(self, extra: &mut dyn Stack) -> Self::Abi {
crate::cache::intern::str(self).into_abi(extra)
self.as_bytes().into_abi(extra)
}
}

impl<'a> OptionIntoWasmAbi for &'a str {
#[inline]
fn none() -> Self::Abi {
<JsValue as OptionIntoWasmAbi>::none()
}
fn none() -> Self::Abi { null_slice() }
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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]> {
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
Expand Down

0 comments on commit ae71087

Please sign in to comment.