Skip to content
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

perf(es/minifier): Speed up merge_sequences_in_exprs by caching computation #9843

Merged
merged 10 commits into from
Jan 7, 2025

Conversation

CPunisher
Copy link
Contributor

@CPunisher CPunisher commented Jan 6, 2025

Problem

In https://github.com/chenjiahan/rspack-swc-minify-test:
Rspack + SWC minify: 15.89 s
Rspack + esbuild minify: 1.74 s

This is caused by merge_sequences_in_exprs which tries to merge every pair of exprs with time complexity of O(n^2). In the given example, a vec of 5003 exprs (react component declarations and other declarations) are passed to this function.

From the profiling result below we can see visit_children_with takes a lot of times, which is called by IdentUsageFinder and idents_used_by to check whether an ident is used in an ast node. However, in the O(n^2) loops, this part of the result is heavily recalculated.

Example:

// input.js
export function source() {
    let c = 0, a = 1;
    c += a, a += 5;
    let b = c;
    console.log(a, b, c);
}

// esbuild/terser output
export function source(){let o=0,e=1;o+=e,e+=5,console.log(e,o,o)}

// swc output
export function source(){console.log(6,1,1)}

Solution

To be honest, I don't come up with a better algorithm to reduce time complexity or pruning. But caching part of the computation does work very well in this bad case. Anyway, I'm proposing this pr first.

Result

Before: 10128ms
image

After: 1013ms(~10x)
image

@CPunisher CPunisher requested a review from a team as a code owner January 6, 2025 10:17
Copy link

changeset-bot bot commented Jan 6, 2025

🦋 Changeset detected

Latest commit: 75045a4

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

kdy1
kdy1 previously approved these changes Jan 6, 2025
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

CI failed, and I verified locally that CI failure does not happen on the main branch.

I love the idea behind this PR. I think it's fine to simply update test cases of the swc crate in this case, considering the performance difference and the amount of difference in outputs. Can you update or fix them? You can do either of them.

@kdy1 kdy1 added this to the Planned milestone Jan 6, 2025
@kdy1 kdy1 changed the title perf(es/minifier): Speed up merge_sequences_in_exprs by caching computation perf(es/minifier): Speed up merge_sequences_in_exprs by caching computation Jan 6, 2025
Copy link

codspeed-hq bot commented Jan 6, 2025

CodSpeed Performance Report

Merging #9843 will degrade performances by 3.33%

Comparing CPunisher:perf/merge_sequences_in_exprs (9ddccb8) with main (66bf0e5)

Summary

❌ 1 regressions
✅ 205 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main CPunisher:perf/merge_sequences_in_exprs Change
common/allocator/alloc/cached-no-scope/1000000 108.6 ms 112.4 ms -3.33%

@kdy1 kdy1 requested a review from a team as a code owner January 7, 2025 09:57
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

Thank you!

@kdy1 kdy1 merged commit 6e5632f into swc-project:main Jan 7, 2025
4 checks passed
@kdy1 kdy1 modified the milestones: Planned, v1.10.6 Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants