-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(es/minifier): Speed up
merge_sequences_in_exprs
by caching com…
…putation (#9843) ## 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: ```js // 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** <img width="1246" alt="image" src="https://github.com/user-attachments/assets/43d38775-e487-4f5a-a95c-8331ee85f109" /> **After: 1013ms(~10x)** <img width="1207" alt="image" src="https://github.com/user-attachments/assets/1b0d2852-343f-4bf6-8140-2b9f2d2772a4" />
- Loading branch information
Showing
2 changed files
with
99 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
swc_core: patch | ||
swc_ecma_minifier: patch | ||
--- | ||
|
||
perf(es/minifier): Speed up `merge_sequences_in_exprs` by caching computation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters