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

perf(atoms): Introduce AtomStoreCell #8232

Merged
merged 5 commits into from
Nov 9, 2023
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
15 changes: 15 additions & 0 deletions crates/swc_atoms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,18 @@ impl AtomStore {
Atom(self.0.atom(s))
}
}

/// A fast internally mutable cell for [AtomStore].
#[derive(Default)]
pub struct AtomStoreCell(std::cell::UnsafeCell<AtomStore>);

impl AtomStoreCell {
#[inline]
pub fn atom<'a>(&self, s: impl Into<Cow<'a, str>>) -> Atom {
// SAFETY: We can skip the borrow check of RefCell because
// this API enforces a safe contract. It is slightly faster
// to use an UnsafeCell. Note the borrow here is short lived
// only to this block.
unsafe { (*self.0.get()).atom(s) }
}
}
9 changes: 4 additions & 5 deletions crates/swc_ecma_parser/src/lexer/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl<'a> Lexer<'a> {
});

return Ok(Some(Token::JSXText {
raw: self.atoms.borrow_mut().atom(out),
raw: self.atoms.atom(out),
}));
}
'>' => {
Expand Down Expand Up @@ -322,10 +322,9 @@ impl<'a> Lexer<'a> {

raw.push(quote);

let mut b = self.atoms.borrow_mut();
Ok(Token::Str {
value: b.atom(out),
raw: b.atom(raw),
value: self.atoms.atom(out),
raw: self.atoms.atom(raw),
})
}

Expand All @@ -351,7 +350,7 @@ impl<'a> Lexer<'a> {
});

Ok(Token::JSXName {
name: self.atoms.borrow_mut().atom(slice),
name: self.atoms.atom(slice),
})
}
}
Expand Down
29 changes: 13 additions & 16 deletions crates/swc_ecma_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{cell::RefCell, char, iter::FusedIterator, rc::Rc};
use either::Either::{Left, Right};
use smallvec::{smallvec, SmallVec};
use smartstring::SmartString;
use swc_atoms::{Atom, AtomStore};
use swc_atoms::{Atom, AtomStoreCell};
use swc_common::{comments::Comments, input::StringInput, BytePos, Span};
use swc_ecma_ast::{op, AssignOp, EsVersion};

Expand Down Expand Up @@ -133,7 +133,7 @@ pub struct Lexer<'a> {

buf: Rc<RefCell<String>>,

atoms: Rc<RefCell<AtomStore>>,
atoms: Rc<AtomStoreCell>,
}

impl FusedIterator for Lexer<'_> {}
Expand Down Expand Up @@ -767,9 +767,8 @@ impl<'a> Lexer<'a> {
fn read_ident_unknown(&mut self) -> LexResult<Token> {
debug_assert!(self.cur().is_some());

let (word, _) = self.read_word_as_str_with(|l, s, _, _| {
Word::Ident(IdentLike::Other(l.atoms.borrow_mut().atom(s)))
})?;
let (word, _) = self
.read_word_as_str_with(|l, s, _, _| Word::Ident(IdentLike::Other(l.atoms.atom(s))))?;

Ok(Word(word))
}
Expand All @@ -790,7 +789,7 @@ impl<'a> Lexer<'a> {
}
}

Word::Ident(IdentLike::Other(l.atoms.borrow_mut().atom(s)))
Word::Ident(IdentLike::Other(l.atoms.atom(s)))
})?;

// Note: ctx is store in lexer because of this error.
Expand Down Expand Up @@ -1022,10 +1021,9 @@ impl<'a> Lexer<'a> {

l.bump();

let mut b = l.atoms.borrow_mut();
return Ok(Token::Str {
value: b.atom(&*out),
raw: b.atom(raw),
value: l.atoms.atom(&*out),
raw: l.atoms.atom(raw),
});
}
'\\' => {
Expand Down Expand Up @@ -1057,10 +1055,9 @@ impl<'a> Lexer<'a> {

l.emit_error(start, SyntaxError::UnterminatedStrLit);

let mut b = l.atoms.borrow_mut();
Ok(Token::Str {
value: b.atom(&*out),
raw: b.atom(raw),
value: l.atoms.atom(&*out),
raw: l.atoms.atom(raw),
})
})
}
Expand Down Expand Up @@ -1108,7 +1105,7 @@ impl<'a> Lexer<'a> {
buf.push(c);
}

Ok(l.atoms.borrow_mut().atom(&**buf))
Ok(l.atoms.atom(&**buf))
})?;

// input is terminated without following `/`
Expand All @@ -1129,7 +1126,7 @@ impl<'a> Lexer<'a> {
let flags = {
match self.cur() {
Some(c) if c.is_ident_start() => self
.read_word_as_str_with(|l, s, _, _| l.atoms.borrow_mut().atom(s))
.read_word_as_str_with(|l, s, _, _| l.atoms.atom(s))
.map(Some),
_ => Ok(None),
}
Expand All @@ -1151,7 +1148,7 @@ impl<'a> Lexer<'a> {
self.input.bump();
}
let s = self.input.uncons_while(|c| !c.is_line_terminator());
Ok(Some(self.atoms.borrow_mut().atom(s)))
Ok(Some(self.atoms.atom(s)))
}

fn read_tmpl_token(&mut self, start_of_tpl: BytePos) -> LexResult<Token> {
Expand All @@ -1176,7 +1173,7 @@ impl<'a> Lexer<'a> {
// TODO: Handle error
return Ok(Token::Template {
cooked: cooked.map(Atom::from),
raw: self.atoms.borrow_mut().atom(&*raw),
raw: self.atoms.atom(&*raw),
});
}

Expand Down
20 changes: 10 additions & 10 deletions crates/swc_ecma_parser/src/lexer/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<'a> Lexer<'a> {

return Ok(Either::Right((
Box::new(s.into_value()),
self.atoms.borrow_mut().atom(&*raw),
self.atoms.atom(&*raw),
)));
}

Expand All @@ -84,9 +84,9 @@ impl<'a> Lexer<'a> {
// e.g. `000` is octal
if start.0 != self.last_pos().0 - 1 {
// `-1` is utf 8 length of `0`
return self.make_legacy_octal(start, 0f64).map(|value| {
Either::Left((value, self.atoms.borrow_mut().atom(&*raw)))
});
return self
.make_legacy_octal(start, 0f64)
.map(|value| Either::Left((value, self.atoms.atom(&*raw))));
}
} else {
// strict mode hates non-zero decimals starting with zero.
Expand All @@ -113,9 +113,9 @@ impl<'a> Lexer<'a> {
panic!("failed to parse {} into float using BigInt", val_str)
});

return self.make_legacy_octal(start, val).map(|value| {
Either::Left((value, self.atoms.borrow_mut().atom(&*raw)))
});
return self
.make_legacy_octal(start, val)
.map(|value| Either::Left((value, self.atoms.atom(&*raw))));
}
}
}
Expand Down Expand Up @@ -227,7 +227,7 @@ impl<'a> Lexer<'a> {

self.ensure_not_ident()?;

Ok(Either::Left((val, self.atoms.borrow_mut().atom(&*raw_str))))
Ok(Either::Left((val, self.atoms.atom(&*raw_str))))
}

/// Returns `Left(value)` or `Right(BigInt)`
Expand Down Expand Up @@ -268,13 +268,13 @@ impl<'a> Lexer<'a> {

return Ok(Either::Right((
Box::new(s.into_value()),
l.atoms.borrow_mut().atom(&**buf),
l.atoms.atom(&**buf),
)));
}

l.ensure_not_ident()?;

Ok(Either::Left((val, l.atoms.borrow_mut().atom(&**buf))))
Ok(Either::Left((val, l.atoms.atom(&**buf))))
})
}

Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_parser/src/lexer/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl<'a> Lexer<'a> {
let cmt = Comment {
kind: CommentKind::Line,
span: Span::new(start, end, SyntaxContext::empty()),
text: self.atoms.borrow_mut().atom(s),
text: self.atoms.atom(s),
};

if is_for_next {
Expand Down Expand Up @@ -343,7 +343,7 @@ impl<'a> Lexer<'a> {
let cmt = Comment {
kind: CommentKind::Block,
span: Span::new(start, end, SyntaxContext::empty()),
text: self.atoms.borrow_mut().atom(s),
text: self.atoms.atom(s),
};

let _ = self.input.peek();
Expand Down