wry_bindgen/intern.rs
1// https://github.com/wasm-bindgen/wasm-bindgen/blob/dda4821ee2fbcaa7adc58bc8c385ed8d3627a272/src/cache/intern.rs
2
3#[cfg(feature = "enable-interning")]
4use crate::JsValue;
5#[cfg(feature = "enable-interning")]
6use std::borrow::ToOwned;
7#[cfg(feature = "enable-interning")]
8use std::cell::RefCell;
9#[cfg(feature = "enable-interning")]
10use std::collections::HashMap;
11#[cfg(feature = "enable-interning")]
12use std::string::String;
13#[cfg(feature = "enable-interning")]
14use std::thread_local;
15
16#[cfg(feature = "enable-interning")]
17struct Cache {
18 entries: RefCell<HashMap<String, JsValue>>,
19}
20
21#[cfg(feature = "enable-interning")]
22thread_local! {
23 static CACHE: Cache = Cache {
24 entries: RefCell::new(HashMap::new()),
25 };
26}
27
28#[cfg(feature = "enable-interning")]
29/// This returns the raw index of the cached JsValue, so you must take care
30/// so that you don't use it after it is freed.
31#[allow(dead_code)]
32pub(crate) fn unsafe_get_str(s: &str) -> Option<u64> {
33 CACHE.with(|cache| {
34 let cache = cache.entries.borrow();
35
36 cache.get(s).map(|x| x.idx)
37 })
38}
39
40#[cfg(feature = "enable-interning")]
41fn intern_str(key: &str) {
42 CACHE.with(|cache| {
43 let entries = &cache.entries;
44
45 // Can't use `entry` because `entry` requires a `String`
46 if !entries.borrow().contains_key(key) {
47 // Note: we must not hold the borrow while we create the `JsValue`,
48 // because it will try to look up the value in the cache first.
49 let value = JsValue::from(key);
50 entries.borrow_mut().insert(key.to_owned(), value);
51 }
52 })
53}
54
55#[cfg(feature = "enable-interning")]
56fn unintern_str(key: &str) {
57 CACHE.with(|cache| {
58 let mut cache = cache.entries.borrow_mut();
59
60 cache.remove(key);
61 })
62}
63
64/// Interns Rust strings so that it's much faster to send them to JS.
65///
66/// Sending strings from Rust to JS is slow, because it has to do a full `O(n)`
67/// copy and *also* encode from UTF-8 to UTF-16. This must be done every single
68/// time a string is sent to JS.
69///
70/// If you are sending the same string multiple times, you can call this `intern`
71/// function, which simply returns its argument unchanged:
72///
73/// ```rust
74/// # use wry_bindgen::intern;
75/// intern("foo") // returns "foo"
76/// # ;
77/// ```
78///
79/// However, if you enable the `"enable-interning"` feature for wasm-bindgen,
80/// then it will add the string into an internal cache.
81///
82/// When you send that cached string to JS, it will look it up in the cache,
83/// which completely avoids the `O(n)` copy and encoding. This has a significant
84/// speed boost (as high as 783%)!
85///
86/// However, there is a small cost to this caching, so you shouldn't cache every
87/// string. Only cache strings which have a high likelihood of being sent
88/// to JS multiple times.
89///
90/// Also, keep in mind that this function is a *performance hint*: it's not
91/// *guaranteed* that the string will be cached, and the caching strategy
92/// might change at any time, so don't rely upon it.
93#[inline]
94pub fn intern(s: &str) -> &str {
95 #[cfg(feature = "enable-interning")]
96 intern_str(s);
97
98 s
99}
100
101/// Removes a Rust string from the intern cache.
102///
103/// This does the opposite of the [`intern`](fn.intern.html) function.
104///
105/// If the [`intern`](fn.intern.html) function is called again then it will re-intern the string.
106#[allow(unused_variables)]
107#[inline]
108pub fn unintern(s: &str) {
109 #[cfg(feature = "enable-interning")]
110 unintern_str(s);
111}