Skip to content

Commit 84c15a5

Browse files
committed
use indexmap
1 parent a219e86 commit 84c15a5

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/swc_ecma_ast/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ shrink-to-fit = [
3939
arbitrary = { workspace = true, features = ["derive"], optional = true }
4040
bitflags = { workspace = true }
4141
bytecheck = { workspace = true, optional = true }
42-
hashbrown = { workspace = true }
42+
indexmap = { workspace = true }
4343
is-macro = { workspace = true }
4444
num-bigint = { workspace = true, features = ["serde"] }
4545
once_cell = { workspace = true }

crates/swc_ecma_ast/src/ident.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66
ops::{Deref, DerefMut},
77
};
88

9+
use indexmap::map::RawEntryApiV1;
910
use once_cell::sync::Lazy;
1011
use phf::phf_set;
1112
use rustc_hash::FxHashSet;
@@ -545,28 +546,41 @@ impl IdIdx {
545546

546547
#[derive(Default)]
547548
pub struct Ids {
548-
map: hashbrown::HashMap<Id, IdIdx, rustc_hash::FxBuildHasher>,
549+
map: indexmap::IndexMap<Id, (), rustc_hash::FxBuildHasher>,
549550
}
550551

551552
impl Ids {
553+
#[inline(always)]
554+
pub fn intern_ident(&mut self, ident: &Ident) -> IdIdx {
555+
self.intern(&ident.sym, ident.ctxt)
556+
}
557+
552558
pub fn intern(&mut self, atom: &Atom, ctxt: SyntaxContext) -> IdIdx {
553559
let mut hasher = rustc_hash::FxHasher::default();
554560
atom.hash(&mut hasher);
555561
ctxt.hash(&mut hasher);
556562
let hash = hasher.finish();
557563

558-
let len = self.map.len();
559-
560-
let (_, idx) = self
564+
use indexmap::map::raw_entry_v1::RawEntryMut::*;
565+
let idx = match self
561566
.map
562-
.raw_entry_mut()
567+
.raw_entry_mut_v1()
563568
.from_hash(hash, |id| id.1 == ctxt && id.0.eq(atom))
564-
.or_insert_with(|| {
565-
let idx = IdIdx(len as u32);
569+
{
570+
Occupied(occ) => occ.index(),
571+
Vacant(vac) => {
566572
let id = (atom.clone(), ctxt);
567-
(id, idx)
568-
});
569-
*idx
573+
let idx = vac.index();
574+
vac.insert_hashed_nocheck(hash, id, ());
575+
idx
576+
}
577+
};
578+
IdIdx(idx as u32)
579+
}
580+
581+
#[inline(always)]
582+
pub fn get(&self, idx: IdIdx) -> &Id {
583+
&self.map.get_index(idx.0 as usize).unwrap().0
570584
}
571585
}
572586

0 commit comments

Comments
 (0)