perf(oxc_parser): use u8 for offset

This commit is contained in:
Boshen 2023-02-21 10:34:01 +08:00
parent 73663f274c
commit 0bbbc7768f
5 changed files with 9 additions and 8 deletions

View file

@ -70,7 +70,7 @@ impl<'a> Parser<'a> {
}
/// Peek nth token
pub fn nth(&mut self, n: usize) -> &Token {
pub fn nth(&mut self, n: u8) -> &Token {
if n == 0 {
return self.cur_token();
}
@ -78,12 +78,12 @@ impl<'a> Parser<'a> {
}
/// Peek at nth kind
pub fn nth_at(&mut self, n: usize, kind: Kind) -> bool {
pub fn nth_at(&mut self, n: u8, kind: Kind) -> bool {
self.nth(n).kind == kind
}
/// Peek nth kind
pub fn nth_kind(&mut self, n: usize) -> Kind {
pub fn nth_kind(&mut self, n: u8) -> Kind {
self.nth(n).kind
}

View file

@ -328,7 +328,7 @@ impl<'a> Parser<'a> {
&mut self,
r#async: bool,
) -> IsParenthesizedArrowFunction {
let offset = usize::from(r#async);
let offset = u8::from(r#async);
match self.nth_kind(offset) {
Kind::LParen => match self.nth_kind(offset + 1) {

View file

@ -114,7 +114,8 @@ impl<'a> Lexer<'a> {
}
/// Find the nth lookahead token lazily
pub fn lookahead(&mut self, n: usize) -> &Token {
pub fn lookahead(&mut self, n: u8) -> &Token {
let n = n as usize;
debug_assert!(n > 0);
if self.lookahead.len() > n - 1 {

View file

@ -183,7 +183,7 @@ impl<'a> Parser<'a> {
self.nth_at(offset + 2, Kind::Colon)
}
pub fn is_nth_at_modifier(&mut self, n: usize, is_constructor_parameter: bool) -> bool {
pub fn is_nth_at_modifier(&mut self, n: u8, is_constructor_parameter: bool) -> bool {
let nth = self.nth(n);
if !(matches!(
nth.kind,
@ -299,7 +299,7 @@ impl<'a> Parser<'a> {
.map(|decl| Statement::Declaration(Declaration::TSModuleDeclaration(decl)))
}
pub fn is_nth_at_ts_namespace_declaration(&mut self, n: usize) -> bool {
pub fn is_nth_at_ts_namespace_declaration(&mut self, n: u8) -> bool {
if self.nth(n + 1).is_on_new_line {
return false;
}

View file

@ -616,7 +616,7 @@ impl<'a> Parser<'a> {
}
pub fn is_at_named_tuple_element(&mut self) -> bool {
let offset = usize::from(self.at(Kind::Dot3));
let offset = u8::from(self.at(Kind::Dot3));
let has_colon = self.nth_at(offset + 1, Kind::Colon);
let has_question_colon =
self.nth_at(offset + 1, Kind::Question) && self.nth_at(offset + 2, Kind::Colon);