Skip to content

Commit

Permalink
perf(parser): faster lexing template strings
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel authored and Boshen committed Feb 29, 2024
1 parent 9d7ea6b commit 286a4a9
Show file tree
Hide file tree
Showing 4 changed files with 316 additions and 121 deletions.
2 changes: 0 additions & 2 deletions crates/oxc_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ mod regex;
mod search;
mod source;
mod string;
mod string_builder;
mod template;
mod token;
mod trivia_builder;
Expand All @@ -38,7 +37,6 @@ use oxc_span::{SourceType, Span};
use self::{
byte_handlers::handle_byte,
source::{Source, SourcePosition},
string_builder::AutoCow,
trivia_builder::TriviaBuilder,
};
pub use self::{
Expand Down
14 changes: 14 additions & 0 deletions crates/oxc_parser/src/lexer/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,20 @@ impl<'a> Source<'a> {
self.str_between_positions_unchecked(pos, SourcePosition::new(self.ptr))
}

/// Get string slice from current position of `Source` up to a `SourcePosition`, without checks.
///
/// # SAFETY
/// `pos` must not be before current position of `Source`.
/// This is always the case if both:
/// 1. `Source::set_position` has not been called since `pos` was created.
/// 2. `pos` has not been moved backwards with `SourcePosition::sub`.
#[inline]
pub(super) unsafe fn str_from_current_to_pos_unchecked(&self, pos: SourcePosition) -> &'a str {
// SAFETY: Caller guarantees `pos` is not before current position of `Source`.
// `self.ptr` is always a valid `SourcePosition` due to invariants of `Source`.
self.str_between_positions_unchecked(SourcePosition::new(self.ptr), pos)
}

/// Get string slice from a `SourcePosition` up to the end of `Source`.
#[inline]
pub(super) fn str_from_pos_to_end(&self, pos: SourcePosition) -> &'a str {
Expand Down
74 changes: 0 additions & 74 deletions crates/oxc_parser/src/lexer/string_builder.rs

This file was deleted.

Loading

0 comments on commit 286a4a9

Please sign in to comment.