summaryrefslogtreecommitdiffstats
path: root/vendor/wasm-bindgen/src/cache
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
commit10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 (patch)
treebdffd5d80c26cf4a7a518281a204be1ace85b4c1 /vendor/wasm-bindgen/src/cache
parentReleasing progress-linux version 1.70.0+dfsg1-9~progress7.99u1. (diff)
downloadrustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.tar.xz
rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.zip
Merging upstream version 1.70.0+dfsg2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/wasm-bindgen/src/cache')
-rw-r--r--vendor/wasm-bindgen/src/cache/intern.rs103
-rw-r--r--vendor/wasm-bindgen/src/cache/mod.rs1
2 files changed, 104 insertions, 0 deletions
diff --git a/vendor/wasm-bindgen/src/cache/intern.rs b/vendor/wasm-bindgen/src/cache/intern.rs
new file mode 100644
index 000000000..c8aa51b2c
--- /dev/null
+++ b/vendor/wasm-bindgen/src/cache/intern.rs
@@ -0,0 +1,103 @@
+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;
+
+ 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<u32> {
+ CACHE.with(|cache| {
+ let cache = cache.entries.borrow();
+
+ cache.get(s).map(|x| x.idx)
+ })
+ }
+
+ 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));
+ }
+ })
+ }
+
+ fn unintern_str(key: &str) {
+ CACHE.with(|cache| {
+ let mut cache = cache.entries.borrow_mut();
+
+ cache.remove(key);
+ })
+ }
+ }
+}
+
+
+/// 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.
+///
+/// Also, keep in mind that this function is a *performance hint*: it's not
+/// *guaranteed* that the string will be cached, and the caching strategy
+/// might change at any time, so don't rely upon it.
+#[inline]
+pub fn intern(s: &str) -> &str {
+ #[cfg(feature = "enable-interning")]
+ intern_str(s);
+
+ s
+}
+
+
+/// Removes a Rust string from the intern cache.
+///
+/// This does the opposite of the [`intern`](fn.intern.html) function.
+///
+/// If the [`intern`](fn.intern.html) function is called again then it will re-intern the string.
+#[allow(unused_variables)]
+#[inline]
+pub fn unintern(s: &str) {
+ #[cfg(feature = "enable-interning")]
+ unintern_str(s);
+}
diff --git a/vendor/wasm-bindgen/src/cache/mod.rs b/vendor/wasm-bindgen/src/cache/mod.rs
new file mode 100644
index 000000000..96d1a4920
--- /dev/null
+++ b/vendor/wasm-bindgen/src/cache/mod.rs
@@ -0,0 +1 @@
+pub mod intern;