Skip to content

Commit 12c2807

Browse files
authored
perf(common): Use next instead of nth (#10403)
https://github.com/rust-lang/rust/blob/1.86.0/compiler/rustc_lexer/src/cursor.rs#L56 mentions that using `next` is more optimized than `nth`, let's give this optimization a try.
1 parent 6008ba2 commit 12c2807

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

.changeset/flat-humans-sneeze.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc_common: patch
4+
---
5+
6+
pref(es/common): use next rather than nth

crates/swc_common/src/input.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,19 @@ impl Input for StringInput<'_> {
8080

8181
#[inline]
8282
fn peek(&mut self) -> Option<char> {
83-
self.iter.clone().nth(1)
83+
let mut iter = self.iter.clone();
84+
// https://github.com/rust-lang/rust/blob/1.86.0/compiler/rustc_lexer/src/cursor.rs#L56 say `next` is faster.
85+
iter.next();
86+
iter.next()
8487
}
8588

8689
#[inline]
8790
fn peek_ahead(&mut self) -> Option<char> {
88-
self.iter.clone().nth(2)
91+
let mut iter = self.iter.clone();
92+
// https://github.com/rust-lang/rust/blob/1.86.0/compiler/rustc_lexer/src/cursor.rs#L56 say `next` is faster
93+
iter.next();
94+
iter.next();
95+
iter.next()
8996
}
9097

9198
#[inline]

0 commit comments

Comments
 (0)