perf(es): Delete useless partition and extend in comments (#8214)

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
https://github.com/web-infra-dev/rspack/issues/4525
This commit is contained in:
bohan 2023-11-04 16:26:25 +08:00 committed by GitHub
parent 71448552e5
commit 67e2c4a4fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -100,16 +100,14 @@ impl DroppedCommentsPreserver {
fn shift_leading_comments(&self, comments: &SingleThreadedComments) -> CommentEntries {
let mut existing_comments = self.collect_existing_comments(comments);
existing_comments.sort_by(|(bp_a, _), (bp_b, _)| bp_a.cmp(bp_b));
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();
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)
}
@ -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,