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

feat(mangler): reuse variable names #8562

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sapphi-red
Copy link
Contributor

@sapphi-red sapphi-red commented Jan 17, 2025

Changed the mangler to reuse variable names where possible.

This will reduce the code size as shorter variable names can be used in more places. But requires global information and limits parallelism in a single file and requires more memory.

@github-actions github-actions bot added A-semantic Area - Semantic A-minifier Area - Minifier C-enhancement Category - New feature or request labels Jan 17, 2025
Copy link
Contributor Author


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@sapphi-red sapphi-red force-pushed the 01-17-feat_mangler_reuse_variable_names branch from dd44926 to 4d6c3ee Compare January 17, 2025 08:04
Copy link

codspeed-hq bot commented Jan 17, 2025

CodSpeed Performance Report

Merging #8562 will degrade performances by 72.24%

Comparing 01-17-feat_mangler_reuse_variable_names (1b9026f) with main (007e8c0)

Summary

❌ 3 regressions
✅ 29 untouched benchmarks

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

Benchmarks breakdown

Benchmark main 01-17-feat_mangler_reuse_variable_names Change
mangler[antd.js] 6 ms 12.4 ms -51.65%
mangler[react.development.js] 138.8 µs 263.9 µs -47.42%
mangler[typescript.js] 9.6 ms 34.5 ms -72.24%

@sapphi-red sapphi-red force-pushed the 01-17-feat_mangler_reuse_variable_names branch from 4d6c3ee to 1b9026f Compare January 17, 2025 08:11
@sapphi-red
Copy link
Contributor Author

The perf regression looks really bad, but if you take a step back and see the overall minifier timings, it's not that big regression.

antd: 196.8ms (6.4ms is 3.3%)
react: 1.8ms (125.1µs is 7.0%)
typescript: 344.6ms (24.9ms is 7.2%)

@overlookmotel
Copy link
Contributor

overlookmotel commented Jan 17, 2025

Just to raise one other concern:

The addition of scope_id field to Reference will cause difficulties in transformer. In transformer, we update semantic data as we mutate the AST, to keep AST + semantic in sync.

This is quite difficult already, and if Reference has a scope_id field, we'd need to update that field too whenever we move an IdentifierReference into a different scope, or e.g. wrap some code in an IIFE (which introduces a new scope). Both of these are fairly common operations in the transformer.

The changes to TraverseScoping::create_bound_reference and create_unbound_reference in this PR aren't correct - often we are creating a Reference in a different scope from the current one.

FYI: The bigger picture is: Currently we run semantic analysis multiple times during the pipeline. The hope (well certainly my hope) is that we can eventually make all stages of the pipeline keep semantic data in sync with AST changes. Then we'll be able to remove these extra semantic passes, which will be a large perf improvement.


The performance issue needs to be looked at first. But if we decide that it's worth it (very possibly it is), I would suggest the following:

  • Split the introduction of Reference::scope_id field into a separate PR.
  • Add checks for correctness of that field to the transformer conformance checker (which checks semantic data is correct after transform), and see how many errors we have due to incorrect Reference::scope_id.
  • Assess how much work required to handle updating this extra state in transformer.

It will of course be achievable with some effort, but it'd be useful to understand how much effort before we go ahead. Maybe it's easier than I think!

cc @Dunqing


Last thing: I'm not very familiar with the mangler. Could you possibly give a brief explanation of how this algorithm works? I'm wondering if any other way to achieve the effect without the Reference::scope_id field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-minifier Area - Minifier A-semantic Area - Semantic C-enhancement Category - New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants