Skip to content

Commit 9182484

Browse files
authored
perf: add SourceTextProvider (#66)
1 parent 27fe4e9 commit 9182484

File tree

5 files changed

+33
-15
lines changed

5 files changed

+33
-15
lines changed

Cargo.lock

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ All (`SourceRanged` trait):
2222

2323
- `.start(&self) -> SourcePos`
2424
- `.end(&self) -> SourcePos`
25-
- `.text_fast(&self, root_node: &dyn SourceTextInfoProvider) -> &'a str` -- Doesn't require going up the tree to the root node
25+
- `.text_fast(&self, root_node: &dyn SourceTextProvider) -> &'a str` -- Doesn't require going up the tree to the root node
2626
- `.start_line_fast(&self, root_node: &dyn SourceTextInfoProvider) -> usize`
2727
- `.end_line_fast(&self, root_node: &dyn SourceTextInfoProvider) -> usize`
2828
- `.start_column_fast(&self, root_node: &dyn SourceTextInfoProvider) -> usize`

rs-lib/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ bumpalo = { version = "3.16.0", optional = true, features = ["collections", "all
2020
num-bigint = "0.4"
2121
rustc-hash = "1.1.0"
2222
swc_atoms = "0.6.7"
23-
swc_common = "0.37.0"
24-
swc_ecma_ast = "0.118.0"
25-
swc_ecma_parser = "0.149.0"
23+
swc_common = "0.37.5"
24+
swc_ecma_ast = "0.118.2"
25+
swc_ecma_parser = "0.149.1"
2626
text_lines = "0.6.0"
2727

2828
[dev-dependencies]

rs-lib/src/common/pos.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,14 @@ macro_rules! source_ranged_trait {
288288
text_slice.chars().count()
289289
}
290290

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

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

301301
fn tokens_fast<'a>(&self, program: impl RootNode<'a>) -> &'a [TokenAndSpan] {

rs-lib/src/common/text_info.rs

+18
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,24 @@ impl<'a> SourceTextInfoProvider<'a> for &'a SourceTextInfo {
237237
}
238238
}
239239

240+
pub trait SourceTextProvider<'a> {
241+
fn text(&self) -> &'a Arc<str>;
242+
fn start_pos(&self) -> StartSourcePos;
243+
}
244+
245+
impl<'a, T> SourceTextProvider<'a> for T
246+
where
247+
T: SourceTextInfoProvider<'a>,
248+
{
249+
fn text(&self) -> &'a Arc<str> {
250+
&self.text_info().text
251+
}
252+
253+
fn start_pos(&self) -> StartSourcePos {
254+
self.text_info().start_pos
255+
}
256+
}
257+
240258
#[cfg(test)]
241259
mod test {
242260
use super::*;

0 commit comments

Comments
 (0)