From 67e2c4a4fd17436732099422c25d1c0d82f815dd Mon Sep 17 00:00:00 2001 From: bohan Date: Sat, 4 Nov 2023 16:26:25 +0800 Subject: [PATCH] perf(es): Delete useless partition and extend in comments (#8214) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I've used `bizcharts` as a benchmark, and: before this PR 17s: image after this PR 1.7s: image You can get more info at https://github.com/web-infra-dev/rspack/issues/4525 --- crates/swc/src/dropped_comments_preserver.rs | 24 ++++++++------------ 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/crates/swc/src/dropped_comments_preserver.rs b/crates/swc/src/dropped_comments_preserver.rs index 3899ef2f2442..b0c15a55b131 100644 --- a/crates/swc/src/dropped_comments_preserver.rs +++ b/crates/swc/src/dropped_comments_preserver.rs @@ -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::>(); 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,