Skip to content
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

Adding in unintern function #1828

Merged
merged 2 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/cache/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ cfg_if! {
}
})
}

fn unintern_str(key: &str) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have some sort of reference counter perhaps? That is, if several parts of code use intern, but then one does unintern, maybe we shouldn't evict it yet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured that if you want that, it's easy enough to do your own ref-counting in your program.

I'm a bit wary of introducing too much complexity, it seems to me that ref-counting is orthogonal to interning.

CACHE.with(|cache| {
let mut cache = cache.entries.borrow_mut();

cache.remove(key);
})
}
}
}

Expand Down Expand Up @@ -80,3 +88,16 @@ pub fn intern(s: &str) -> &str {

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);
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ if_std! {
mod anyref;

mod cache;
pub use cache::intern::intern;
pub use cache::intern::{intern, unintern};
}

/// Representation of an object owned by JS.
Expand Down
5 changes: 4 additions & 1 deletion tests/wasm/simple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen::{JsCast, intern, unintern};
use wasm_bindgen_test::*;

#[wasm_bindgen(module = "tests/wasm/simple.js")]
Expand Down Expand Up @@ -157,6 +157,9 @@ fn binding_to_unimplemented_apis_doesnt_break_everything() {
fn optional_slices() {
optional_str_none(None);
optional_str_some(Some("x"));
optional_str_some(Some(intern("x")));
unintern("x");
optional_str_some(Some("x"));
optional_slice_none(None);
optional_slice_some(Some(&[1, 2, 3]));
optional_string_none(None);
Expand Down