Skip to content

Commit

Permalink
perf: add SourceTextProvider (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Aug 24, 2024
1 parent 27fe4e9 commit 9182484
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ All (`SourceRanged` trait):

- `.start(&self) -> SourcePos`
- `.end(&self) -> SourcePos`
- `.text_fast(&self, root_node: &dyn SourceTextInfoProvider) -> &'a str` -- Doesn't require going up the tree to the root node
- `.text_fast(&self, root_node: &dyn SourceTextProvider) -> &'a str` -- Doesn't require going up the tree to the root node
- `.start_line_fast(&self, root_node: &dyn SourceTextInfoProvider) -> usize`
- `.end_line_fast(&self, root_node: &dyn SourceTextInfoProvider) -> usize`
- `.start_column_fast(&self, root_node: &dyn SourceTextInfoProvider) -> usize`
Expand Down
6 changes: 3 additions & 3 deletions rs-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ bumpalo = { version = "3.16.0", optional = true, features = ["collections", "all
num-bigint = "0.4"
rustc-hash = "1.1.0"
swc_atoms = "0.6.7"
swc_common = "0.37.0"
swc_ecma_ast = "0.118.0"
swc_ecma_parser = "0.149.0"
swc_common = "0.37.5"
swc_ecma_ast = "0.118.2"
swc_ecma_parser = "0.149.1"
text_lines = "0.6.0"

[dev-dependencies]
Expand Down
10 changes: 5 additions & 5 deletions rs-lib/src/common/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,14 @@ macro_rules! source_ranged_trait {
text_slice.chars().count()
}

fn char_width_fast<'a, P: SourceTextInfoProvider<'a>>(&self, source: P) -> usize {
fn char_width_fast<'a, P: SourceTextProvider<'a>>(&self, source: P) -> usize {
self.text_fast(source).chars().count()
}

fn text_fast<'a, P: SourceTextInfoProvider<'a>>(&self, source: P) -> &'a str {
let text_info = source.text_info();
let byte_range = self.range().as_byte_range(text_info.range().start);
&text_info.text_str()[byte_range]
fn text_fast<'a, P: SourceTextProvider<'a>>(&self, source: P) -> &'a str {
let text = source.text();
let byte_range = self.range().as_byte_range(source.start_pos());
&text[byte_range]
}

fn tokens_fast<'a>(&self, program: impl RootNode<'a>) -> &'a [TokenAndSpan] {
Expand Down
18 changes: 18 additions & 0 deletions rs-lib/src/common/text_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,24 @@ impl<'a> SourceTextInfoProvider<'a> for &'a SourceTextInfo {
}
}

pub trait SourceTextProvider<'a> {
fn text(&self) -> &'a Arc<str>;
fn start_pos(&self) -> StartSourcePos;
}

impl<'a, T> SourceTextProvider<'a> for T
where
T: SourceTextInfoProvider<'a>,
{
fn text(&self) -> &'a Arc<str> {
&self.text_info().text
}

fn start_pos(&self) -> StartSourcePos {
self.text_info().start_pos
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 9182484

Please # to comment.