8000 perf(swc_common): Remove `char_indices` calls by bvanjoi · Pull Request #10541 · swc-project/swc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

perf(swc_common): Remove char_indices calls #10541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/silly-humans-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_common: patch
---

perf(swc/common): rm char_indices
40 changes: 23 additions & 17 deletions crates/swc_common/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,19 @@ impl<'a> Input<'a> for StringInput<'a> {
where
F: FnMut(char) -> bool,
{
let s = self.iter.as_str();
let mut last = 0;

for (i, c) in s.char_indices() {
if pred(c) {
last = i + c.len_utf8();
} else {
break;
let last = {
let mut last = 0;
for c in self.iter.clone() {
if pred(c) {
last += c.len_utf8();
} else {
break;
}
}
}
last
};

let s = self.iter.as_str();
debug_assert!(last <= s.len());
let ret = unsafe { s.get_unchecked(..last) };

Expand All @@ -178,19 +181,22 @@ impl<'a> Input<'a> for StringInput<'a> {
where
F: FnMut(char) -> bool,
{
let s = self.iter.as_str();
let mut last = 0;

for (i, c) in s.char_indices() {
if pred(c) {
last = i + c.len_utf8();
break;
let last = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Input/find appears unused, can we remove it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But with a separate PR

let mut last = 0;
for c in self.iter.clone() {
last += c.len_utf8();
if pred(c) {
break;
}
}
}
last
};

if last == 0 {
return None;
}

let s = self.iter.as_str();
debug_assert!(last <= s.len());

self.last_pos = self.last_pos + BytePos(last as _);
Expand Down
Loading
0