-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Initial interning implementation #1612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 21 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
6767371
Initial interning implementation
Pauan 86a8842
Changing IntoWasmAbi to use interning
Pauan 0a61e12
Making interning manual
Pauan 0359da2
Potential fix for OptionIntoWasmAbi?
Pauan f28cfc2
Fixing some things for the cache
Pauan f7e8e70
Fixing compile errors
Pauan 2ee4c54
Changing to use WasmSlice for the caching
Pauan fd88626
Fixing compile errors
Pauan 4e50465
Undoing some formatting
Pauan 1e4cac9
Simplifying the output
Pauan 1723e9d
More simplifications
Pauan cc6ec86
Fixing compile errors
Pauan adf21fe
Removing unneeded size argument
Pauan 8572255
Making uluru optional
Pauan c3676bc
Removing unneeded if statement
Pauan 2405fad
Shifting the unsafety guarantees around
Pauan 3177fa9
Minor doc fix
Pauan ca15a59
Changing from uluru to HashMap
Pauan 544ec49
Shifting the unsafe responsibility a bit
Pauan 366ed23
Adding in docs for intern
Pauan c82253c
Fixing doc test error
Pauan 554ef90
Fixing issue with wasm-interpreter
Pauan 10ab4cb
Fixing TypeScript types for cached strings
Pauan f8da1e6
Fixing unsafe_get_str function
Pauan aef62bd
Adding in enable-interning to CI test suite
Pauan 7b6ef70
Adding in note to the intern docs
Pauan 59af318
Fixing CI build error
Pauan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use cfg_if::cfg_if; | ||
|
||
|
||
cfg_if! { | ||
if #[cfg(feature = "enable-interning")] { | ||
use std::thread_local; | ||
use std::string::String; | ||
use std::borrow::ToOwned; | ||
use std::cell::RefCell; | ||
use std::collections::HashMap; | ||
use crate::JsValue; | ||
use crate::convert::IntoWasmAbi; | ||
|
||
struct Cache { | ||
entries: RefCell<HashMap<String, JsValue>>, | ||
} | ||
|
||
thread_local! { | ||
static CACHE: Cache = Cache { | ||
entries: RefCell::new(HashMap::new()), | ||
}; | ||
} | ||
|
||
/// This returns the raw index of the cached JsValue, so you must take care | ||
/// so that you don't use it after it is freed. | ||
pub(crate) fn unsafe_get_str(s: &str) -> Option<<JsValue as IntoWasmAbi>::Abi> { | ||
Pauan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
CACHE.with(|cache| { | ||
let cache = cache.entries.borrow(); | ||
|
||
cache.get(s).map(|x| x.into_abi()) | ||
}) | ||
} | ||
|
||
fn intern_str(key: &str) { | ||
CACHE.with(|cache| { | ||
let mut cache = cache.entries.borrow_mut(); | ||
|
||
// Can't use `entry` because `entry` requires a `String` | ||
if !cache.contains_key(key) { | ||
cache.insert(key.to_owned(), JsValue::from(key)); | ||
alexcrichton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
||
|
||
/// Interns Rust strings so that it's much faster to send them to JS. | ||
/// | ||
/// Sending strings from Rust to JS is slow, because it has to do a full `O(n)` | ||
/// copy and *also* encode from UTF-8 to UTF-16. This must be done every single | ||
/// time a string is sent to JS. | ||
/// | ||
/// If you are sending the same string multiple times, you can call this `intern` | ||
/// function, which simply returns its argument unchanged: | ||
/// | ||
/// ```rust | ||
/// # use wasm_bindgen::intern; | ||
/// intern("foo") // returns "foo" | ||
/// ``` | ||
/// | ||
/// However, if you enable the `"enable-interning"` feature for wasm-bindgen, | ||
/// then it will add the string into an internal cache. | ||
/// | ||
/// When you send that cached string to JS, it will look it up in the cache, | ||
/// which completely avoids the `O(n)` copy and encoding. This has a significant | ||
/// speed boost (as high as 783%)! | ||
/// | ||
/// However, there is a small cost to this caching, so you shouldn't cache every | ||
/// string. Only cache strings which have a high likelihood of being sent | ||
/// to JS multiple times. | ||
#[inline] | ||
pub fn intern(s: &str) -> &str { | ||
alexcrichton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[cfg(feature = "enable-interning")] | ||
intern_str(s); | ||
|
||
s | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod intern; |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.