Skip to content

Commit f3f3ebe

Browse files
committed
Remove unsafe usage in backend/src/encode.rs
Using `unsafe` was just a little too eager there so let's use an off-the-shelf solution for solving the actual problem we have, which is to allocate strings with a lifetime of `Interner` rather than deduplicating strings.
1 parent 93a1301 commit f3f3ebe

File tree

3 files changed

+10
-18
lines changed

3 files changed

+10
-18
lines changed

crates/backend/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ spans = []
1515
extra-traits = ["syn/extra-traits"]
1616

1717
[dependencies]
18+
bumpalo = "2.1"
1819
lazy_static = "1.0.0"
1920
log = "0.4"
2021
proc-macro2 = "0.4.8"

crates/backend/src/encode.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use proc_macro2::{Ident, Span};
22
use std::cell::RefCell;
3-
use std::collections::{HashMap, HashSet};
3+
use std::collections::HashMap;
44
use std::env;
55
use std::fs;
66
use std::path::PathBuf;
@@ -24,8 +24,7 @@ pub fn encode(program: &ast::Program) -> Result<EncodeResult, Diagnostic> {
2424
}
2525

2626
struct Interner {
27-
map: RefCell<HashMap<Ident, String>>,
28-
strings: RefCell<HashSet<String>>,
27+
bump: bumpalo::Bump,
2928
files: RefCell<HashMap<String, LocalFile>>,
3029
root: PathBuf,
3130
crate_name: String,
@@ -40,31 +39,22 @@ struct LocalFile {
4039
impl Interner {
4140
fn new() -> Interner {
4241
Interner {
43-
map: RefCell::new(HashMap::new()),
44-
strings: RefCell::new(HashSet::new()),
42+
bump: bumpalo::Bump::new(),
4543
files: RefCell::new(HashMap::new()),
4644
root: env::var_os("CARGO_MANIFEST_DIR").unwrap().into(),
4745
crate_name: env::var("CARGO_PKG_NAME").unwrap(),
4846
}
4947
}
5048

5149
fn intern(&self, s: &Ident) -> &str {
52-
let mut map = self.map.borrow_mut();
53-
if let Some(s) = map.get(s) {
54-
return unsafe { &*(&**s as *const str) };
55-
}
56-
map.insert(s.clone(), s.to_string());
57-
unsafe { &*(&*map[s] as *const str) }
50+
self.intern_str(&s.to_string())
5851
}
5952

6053
fn intern_str(&self, s: &str) -> &str {
61-
let mut strings = self.strings.borrow_mut();
62-
if let Some(s) = strings.get(s) {
63-
return unsafe { &*(&**s as *const str) };
64-
}
65-
strings.insert(s.to_string());
66-
drop(strings);
67-
self.intern_str(s)
54+
// NB: eventually this could be used to intern `s` to only allocate one
55+
// copy, but for now let's just "transmute" `s` to have the same
56+
// lifetmie as this struct itself (which is our main goal here)
57+
bumpalo::collections::String::from_str_in(s, &self.bump).into_bump_str()
6858
}
6959

7060
/// Given an import to a local module `id` this generates a unique module id

crates/backend/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![cfg_attr(feature = "extra-traits", deny(missing_debug_implementations))]
33
#![doc(html_root_url = "https://docs.rs/wasm-bindgen-backend/0.2")]
44

5+
extern crate bumpalo;
56
#[macro_use]
67
extern crate log;
78
extern crate proc_macro2;

0 commit comments

Comments
 (0)