Skip to content

Commit

Permalink
perf(es): Delete useless partition and extend in comments (#8214)
Browse files Browse the repository at this point in the history
I've used `bizcharts` as a benchmark, and:

before this PR 17s:

<img width="288" alt="image"
src="https://github.com/web-infra-dev/rspack/assets/30187863/35232863-247a-4825-a08f-977de46e7216">

after this PR 1.7s:

<img width="585" alt="image"
src="https://github.com/swc-project/swc/assets/30187863/b802abf0-d699-45c7-b9f3-2f52192f7477">


You can get more info at
web-infra-dev/rspack#4525
  • Loading branch information
bvanjoi authored Nov 4, 2023
1 parent 7144855 commit 67e2c4a
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions crates/swc/src/dropped_comments_preserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,14 @@ impl DroppedCommentsPreserver {
fn shift_leading_comments(&self, comments: &SingleThreadedComments) -> CommentEntries {
let mut existing_comments = self.collect_existing_comments(comments);

for span in self.known_spans.iter() {
let (comments_to_move, next_byte_positions): (CommentEntries, CommentEntries) =
existing_comments
.drain(..)
.partition(|(bp, _)| *bp <= span.lo);

existing_comments.extend(next_byte_positions);

let collected_comments = comments_to_move.into_iter().flat_map(|(_, c)| c).collect();
existing_comments.sort_by(|(bp_a, _), (bp_b, _)| bp_a.cmp(bp_b));

for span in self.known_spans.iter() {
let cut_point = existing_comments.partition_point(|(bp, _)| *bp <= span.lo);
let collected_comments = existing_comments
.drain(..cut_point)
.flat_map(|(_, c)| c)
.collect::<Vec<Comment>>();
self.comments
.add_leading_comments(span.lo, collected_comments)
}
Expand All @@ -125,11 +123,9 @@ impl DroppedCommentsPreserver {
let last_trailing = self
.known_spans
.iter()
.copied()
.fold(
DUMMY_SP,
|acc, span| if span.hi > acc.hi { span } else { acc },
);
.max_by_key(|span| span.hi)
.cloned()
.unwrap_or(DUMMY_SP);

self.comments.add_trailing_comments(
last_trailing.hi,
Expand Down

0 comments on commit 67e2c4a

Please sign in to comment.