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 = []
|
std = []
|
||||||
serde-serialize = ["serde", "serde_json", "std"]
|
serde-serialize = ["serde", "serde_json", "std"]
|
||||||
nightly = []
|
nightly = []
|
||||||
disable-interning = []
|
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
|
||||||
@ -52,7 +52,6 @@ wasm-bindgen-test-crate-b = { path = 'tests/crates/b', version = '0.1' }
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
"benchmarks",
|
"benchmarks",
|
||||||
"crates/cache",
|
|
||||||
"crates/cli",
|
"crates/cli",
|
||||||
"crates/js-sys",
|
"crates/js-sys",
|
||||||
"crates/test",
|
"crates/test",
|
||||||
|
114
src/cache/intern.rs
vendored
114
src/cache/intern.rs
vendored
@ -1,79 +1,71 @@
|
|||||||
use std::thread_local;
|
use cfg_if::cfg_if;
|
||||||
use std::string::String;
|
|
||||||
use std::borrow::ToOwned;
|
|
||||||
use std::cell::{Cell, RefCell};
|
|
||||||
use crate::JsValue;
|
|
||||||
use uluru::{LRUCache, Entry};
|
|
||||||
|
|
||||||
|
|
||||||
struct Pair {
|
cfg_if! {
|
||||||
key: String,
|
if #[cfg(feature = "enable-interning")] {
|
||||||
value: JsValue,
|
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<Pair>; 1_024]>;
|
|
||||||
|
|
||||||
struct Cache {
|
struct Pair {
|
||||||
entries: RefCell<Entries>,
|
key: String,
|
||||||
}
|
value: JsValue,
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn intern_str(s: &str) {
|
// TODO figure out a good default capacity
|
||||||
CACHE.with(|cache| {
|
type Entries = LRUCache::<[Entry<Pair>; 1_024]>;
|
||||||
let mut cache = cache.entries.borrow_mut();
|
|
||||||
|
|
||||||
if get_js_string(&mut cache, s).is_none() {
|
struct Cache {
|
||||||
insert_js_string(&mut cache, s, JsValue::from(s));
|
entries: RefCell<Entries>,
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
thread_local! {
|
||||||
pub(crate) fn str(s: &str) -> JsValue {
|
static CACHE: Cache = Cache {
|
||||||
if cfg!(feature = "disable-interning") {
|
entries: RefCell::new(LRUCache::default()),
|
||||||
JsValue::from(s)
|
};
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
fn get_js_string<'a>(cache: &'a mut Entries, key: &str) -> Option<&'a JsValue> {
|
||||||
get_str(s)
|
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]
|
#[inline]
|
||||||
pub fn intern(s: &str) -> &str {
|
pub fn intern(s: &str) -> &str {
|
||||||
if !cfg!(feature = "disable-interning") {
|
#[cfg(feature = "enable-interning")]
|
||||||
intern_str(s);
|
intern_str(s);
|
||||||
}
|
|
||||||
|
|
||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
@ -315,13 +315,6 @@ impl IntoWasmAbi for JsValue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OptionIntoWasmAbi for JsValue {
|
|
||||||
#[inline]
|
|
||||||
fn none() -> u32 {
|
|
||||||
32
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromWasmAbi for JsValue {
|
impl FromWasmAbi for JsValue {
|
||||||
type Abi = u32;
|
type Abi = u32;
|
||||||
|
|
||||||
|
@ -150,28 +150,13 @@ if_std! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cfg_if! {
|
cfg_if! {
|
||||||
if #[cfg(feature = "disable-interning")] {
|
if #[cfg(feature = "enable-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 {
|
|
||||||
impl IntoWasmAbi for String {
|
impl IntoWasmAbi for String {
|
||||||
type Abi = <JsValue as IntoWasmAbi>::Abi;
|
type Abi = <JsValue as IntoWasmAbi>::Abi;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn into_abi(self, extra: &mut dyn Stack) -> Self::Abi {
|
fn into_abi(self) -> Self::Abi {
|
||||||
crate::cache::intern::str(&self).into_abi(extra)
|
crate::cache::intern::get_str(&self).into_abi()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,6 +166,21 @@ if_std! {
|
|||||||
<JsValue as OptionIntoWasmAbi>::none()
|
<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! {
|
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;
|
|
||||||
|
|
||||||
#[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 {
|
|
||||||
impl<'a> IntoWasmAbi for &'a str {
|
impl<'a> IntoWasmAbi for &'a str {
|
||||||
type Abi = <JsValue as IntoWasmAbi>::Abi;
|
type Abi = <JsValue as IntoWasmAbi>::Abi;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn into_abi(self, extra: &mut dyn Stack) -> Self::Abi {
|
fn into_abi(self) -> Self::Abi {
|
||||||
crate::cache::intern::str(self).into_abi(extra)
|
crate::cache::intern::get_str(self).into_abi()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -230,6 +216,20 @@ cfg_if! {
|
|||||||
<JsValue as OptionIntoWasmAbi>::none()
|
<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
|
f64 => F64
|
||||||
bool => BOOLEAN
|
bool => BOOLEAN
|
||||||
char => CHAR
|
char => CHAR
|
||||||
str => if cfg!(feature = "disable-interning") { STRING } else { ANYREF }
|
str => if cfg!(feature = "enable-interning") { ANYREF } else { STRING }
|
||||||
JsValue => ANYREF
|
JsValue => ANYREF
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,7 +116,7 @@ if_std! {
|
|||||||
use std::prelude::v1::*;
|
use std::prelude::v1::*;
|
||||||
|
|
||||||
impl WasmDescribe for String {
|
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]> {
|
impl<T: WasmDescribe> WasmDescribe for Box<[T]> {
|
||||||
|
@ -57,13 +57,10 @@ pub mod prelude {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused)]
|
|
||||||
mod cache;
|
|
||||||
pub mod convert;
|
pub mod convert;
|
||||||
pub mod describe;
|
pub mod describe;
|
||||||
|
|
||||||
pub use cache::intern::intern;
|
|
||||||
|
|
||||||
mod cast;
|
mod cast;
|
||||||
pub use crate::cast::JsCast;
|
pub use crate::cast::JsCast;
|
||||||
|
|
||||||
@ -72,6 +69,9 @@ if_std! {
|
|||||||
use std::prelude::v1::*;
|
use std::prelude::v1::*;
|
||||||
pub mod closure;
|
pub mod closure;
|
||||||
mod anyref;
|
mod anyref;
|
||||||
|
|
||||||
|
mod cache;
|
||||||
|
pub use cache::intern::intern;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Representation of an object owned by JS.
|
/// Representation of an object owned by JS.
|
||||||
|
Reference in New Issue
Block a user