@@ -7,57 +7,37 @@ cfg_if! {
7
7
use std:: string:: String ;
8
8
use std:: borrow:: ToOwned ;
9
9
use std:: cell:: RefCell ;
10
+ use std:: collections:: HashMap ;
10
11
use crate :: JsValue ;
11
12
use crate :: convert:: IntoWasmAbi ;
12
- use uluru:: { LRUCache , Entry } ;
13
-
14
-
15
- struct Pair {
16
- key: String ,
17
- value: JsValue ,
18
- }
19
-
20
- // TODO figure out a good default capacity
21
- type Entries = LRUCache :: <[ Entry <Pair >; 1_024 ] >;
22
13
23
14
struct Cache {
24
- entries: RefCell <Entries >,
15
+ entries: RefCell <HashMap < String , JsValue > >,
25
16
}
26
17
27
18
thread_local! {
28
19
static CACHE : Cache = Cache {
29
- entries: RefCell :: new( LRUCache :: default ( ) ) ,
20
+ entries: RefCell :: new( HashMap :: new ( ) ) ,
30
21
} ;
31
22
}
32
23
33
- fn get_js_string<' a>( cache: & ' a mut Entries , key: & str ) -> Option <& ' a JsValue > {
34
- cache. find( |p| p. key == key) . map( |x| & x. value)
35
- }
36
-
37
24
/// This returns the raw index of the cached JsValue, so you must take care
38
25
/// so that you don't use it after it is freed.
39
26
pub ( crate ) fn unsafe_get_str( s: & str ) -> Option <<JsValue as IntoWasmAbi >:: Abi > {
40
27
CACHE . with( |cache| {
41
- let mut cache = cache. entries. borrow_mut ( ) ;
28
+ let cache = cache. entries. borrow ( ) ;
42
29
43
- if let Some ( value) = get_js_string( & mut cache, s) {
44
- Some ( value. into_abi( ) )
45
-
46
- } else {
47
- None
48
- }
30
+ cache. get( s) . map( |x| x. into_abi( ) )
49
31
} )
50
32
}
51
33
52
34
fn intern_str( key: & str ) {
53
35
CACHE . with( |cache| {
54
36
let mut cache = cache. entries. borrow_mut( ) ;
55
37
56
- if get_js_string( & mut cache, key) . is_none( ) {
57
- cache. insert( Pair {
58
- key: key. to_owned( ) ,
59
- value: JsValue :: from( key) ,
60
- } ) ;
38
+ // Can't use `entry` because `entry` requires a `String`
39
+ if !cache. contains_key( key) {
40
+ cache. insert( key. to_owned( ) , JsValue :: from( key) ) ;
61
41
}
62
42
} )
63
43
}
0 commit comments