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

Fix side effect detection in optional chains #5589

Merged
merged 4 commits into from
Jul 27, 2024

Conversation

lukastaegert
Copy link
Member

@lukastaegert lukastaegert commented Jul 25, 2024

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

List any relevant issue numbers:

Description

This reworks side effect detection in optional chaining so that in case there is an optional link in a chain that skips the remainder of the chain, any side effects in the start of the chain are still observed.

To do that, I introduce two new methods in chain elements:

  • getLiteralValueAtPathAsChainElement is the backbone of the new logic. It behaves the same as getLiteralValueAtPath but has an additional Symbol return value if it is called on a skipped chain. It first calls the same method on the previous chain link and short-circuits if that call also returns that Symbol.
  • hasEffectsAsChainElement similarly works like hasEffects but first relies on getLiteralValueAtPathAsChainElement to detect a broken chain. That way, we can observe all side effects left of the optional link while still skipping all side effects to the right.

Copy link

vercel bot commented Jul 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rollup ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 26, 2024 7:23pm

Copy link

vercel bot commented Jul 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rollup ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 25, 2024 10:40am

Copy link

github-actions bot commented Jul 25, 2024

Thank you for your contribution! ❤️

You can try out this pull request locally by installing Rollup via

npm install rollup/rollup#gh-5579_improve-chaining-side-effect-detection

Notice: Ensure you have installed the latest stable Rust toolchain. If you haven't installed it yet, please see https://www.rust-lang.org/tools/install to learn how to download Rustup and install Rust.

or load it into the REPL:
https://rollup-ui00fd8ir-rollup-js.vercel.app/repl/?pr=5589

Copy link

github-actions bot commented Jul 25, 2024

Performance report!

Rough benchmark

Command Mean [s] Min [s] Max [s] Relative
node _benchmark/previous/bin/rollup -i ./perf/entry.js -o _benchmark/result/previous.js 9.266 ± 0.130 9.119 9.365 1.00 ± 0.01
node _benchmark/current/bin/rollup -i ./perf/entry.js -o _benchmark/result/current.js 9.236 ± 0.028 9.204 9.255 1.00

Internal benchmark

  • BUILD: 8146ms, 751 MB
    • initialize: 0ms, 26 MB
    • generate module graph: 3157ms, 575 MB
      • generate ast: 1511ms, 568 MB
    • sort and bind modules: 447ms, 615 MB
    • mark included statements: 4537ms, 751 MB
      • treeshaking pass 1: 1537ms, 717 MB
      • treeshaking pass 2: 739ms, 740 MB
      • treeshaking pass 3: 288ms, 747 MB
      • treeshaking pass 4: 269ms, 744 MB
      • treeshaking pass 5: 311ms, 750 MB
      • treeshaking pass 6: 258ms, 748 MB
      • treeshaking pass 7: 241ms, 750 MB
      • treeshaking pass 8: 236ms, 748 MB
      • treeshaking pass 9: 214ms, 757 MB
      • treeshaking pass 10: 216ms, 756 MB
      • treeshaking pass 11: 213ms, 751 MB
  • GENERATE: 887ms, 1.02 GB
    • initialize render: 0ms, 914 MB
    • generate chunks: 85ms, 916 MB
      • optimize chunks: 0ms, 917 MB
    • render chunks: 780ms, 1 GB
    • transform chunks: 22ms, 1.02 GB
    • generate bundle: 0ms, 1.02 GB

Copy link

codecov bot commented Jul 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.05%. Comparing base (27260de) to head (e38415f).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #5589    /-   ##
=======================================
  Coverage   99.05%   99.05%           
=======================================
  Files         241      242     1     
  Lines        9273     9297    24     
  Branches     2444     2461    17     
=======================================
  Hits         9185     9209    24     
  Misses         58       58           
  Partials       30       30           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lukastaegert lukastaegert changed the title gh-5579_improve-chaining-side-effect-detection Fix side effect detection in optional chains Jul 26, 2024
Also, do not unnecessarily deoptimize chain expressions
@lukastaegert lukastaegert merged commit 57ea7b1 into master Jul 27, 2024
39 checks passed
@lukastaegert lukastaegert deleted the gh-5579_improve-chaining-side-effect-detection branch July 27, 2024 04:27
Copy link

This PR has been released as part of [email protected]. You can test it via npm install rollup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Function with assignment to global variable may be incorrectly tree-shaken
1 participant