-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
76 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters