Skip to content

Commit 6a9fa49

Browse files
authored
perf(common): Improve StringInput#bump_bytes (#11230)
1 parent c11bca5 commit 6a9fa49

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

.changeset/tiny-eyes-knock.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
swc_common: patch
3+
swc_ecma_parser: patch
4+
swc_core: patch
5+
---
6+
7+
perf(common): Improve `StringInput#bump_bytes`

crates/swc_common/src/input.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,19 @@ impl<'a> StringInput<'a> {
7575

7676
#[inline]
7777
pub fn bump_bytes(&mut self, n: usize) {
78-
unsafe {
79-
// Safety: We only proceed, not go back.
80-
self.reset_to(self.last_pos + BytePos(n as u32));
78+
let s = self.iter.as_str();
79+
self.iter = unsafe { s.get_unchecked(n..) }.chars();
80+
self.last_pos.0 += n as u32;
81+
}
82+
83+
#[inline]
84+
pub fn bump_one(&mut self) {
85+
if self.iter.next().is_some() {
86+
self.last_pos.0 += 1;
87+
} else {
88+
unsafe {
89+
debug_unreachable!("bump should not be called when cur() == None");
90+
}
8191
}
8292
}
8393

crates/swc_ecma_parser/src/lexer/table.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ const PIP: ByteHandler = |lexer| lexer.read_token_logical::<b'|'>();
341341
macro_rules! single_char {
342342
($name:ident, $c:literal, $token:ident) => {
343343
const $name: ByteHandler = |lexer| {
344-
lexer.input.bump_bytes(1);
344+
lexer.input.bump_one();
345345
Ok(Token::$token)
346346
};
347347
};
@@ -368,9 +368,9 @@ single_char!(BEC, b'}', RBrace);
368368
/// `^`
369369
const CRT: ByteHandler = |lexer| {
370370
// Bitwise xor
371-
lexer.input.bump_bytes(1);
371+
lexer.input.bump_one();
372372
Ok(if lexer.input.cur_as_ascii() == Some(b'=') {
373-
lexer.input.bump_bytes(1);
373+
lexer.input.bump_one();
374374
Token::BitXorEq
375375
} else {
376376
Token::Caret

0 commit comments

Comments
 (0)