diff --git a/crates/backend/Cargo.toml b/crates/backend/Cargo.toml index f2f11449..7aebc02b 100644 --- a/crates/backend/Cargo.toml +++ b/crates/backend/Cargo.toml @@ -15,6 +15,7 @@ spans = [] extra-traits = ["syn/extra-traits"] [dependencies] +bumpalo = "2.1" lazy_static = "1.0.0" log = "0.4" proc-macro2 = "0.4.8" diff --git a/crates/backend/src/encode.rs b/crates/backend/src/encode.rs index 2e0dca7d..2cec95f5 100644 --- a/crates/backend/src/encode.rs +++ b/crates/backend/src/encode.rs @@ -1,6 +1,6 @@ use proc_macro2::{Ident, Span}; use std::cell::RefCell; -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; use std::env; use std::fs; use std::path::PathBuf; @@ -24,8 +24,7 @@ pub fn encode(program: &ast::Program) -> Result { } struct Interner { - map: RefCell>, - strings: RefCell>, + bump: bumpalo::Bump, files: RefCell>, root: PathBuf, crate_name: String, @@ -40,8 +39,7 @@ struct LocalFile { impl Interner { fn new() -> Interner { Interner { - map: RefCell::new(HashMap::new()), - strings: RefCell::new(HashSet::new()), + bump: bumpalo::Bump::new(), files: RefCell::new(HashMap::new()), root: env::var_os("CARGO_MANIFEST_DIR").unwrap().into(), crate_name: env::var("CARGO_PKG_NAME").unwrap(), @@ -49,22 +47,14 @@ impl Interner { } fn intern(&self, s: &Ident) -> &str { - let mut map = self.map.borrow_mut(); - if let Some(s) = map.get(s) { - return unsafe { &*(&**s as *const str) }; - } - map.insert(s.clone(), s.to_string()); - unsafe { &*(&*map[s] as *const str) } + self.intern_str(&s.to_string()) } fn intern_str(&self, s: &str) -> &str { - let mut strings = self.strings.borrow_mut(); - if let Some(s) = strings.get(s) { - return unsafe { &*(&**s as *const str) }; - } - strings.insert(s.to_string()); - drop(strings); - self.intern_str(s) + // NB: eventually this could be used to intern `s` to only allocate one + // copy, but for now let's just "transmute" `s` to have the same + // lifetmie as this struct itself (which is our main goal here) + bumpalo::collections::String::from_str_in(s, &self.bump).into_bump_str() } /// Given an import to a local module `id` this generates a unique module id diff --git a/crates/backend/src/lib.rs b/crates/backend/src/lib.rs index 325cd795..70e054e8 100755 --- a/crates/backend/src/lib.rs +++ b/crates/backend/src/lib.rs @@ -2,6 +2,7 @@ #![cfg_attr(feature = "extra-traits", deny(missing_debug_implementations))] #![doc(html_root_url = "https://docs.rs/wasm-bindgen-backend/0.2")] +extern crate bumpalo; #[macro_use] extern crate log; extern crate proc_macro2;