Skip to content

fix(minifier): should not merge conditional function calls if referencing the function has a side-effect#8922

Merged
graphite-app[bot] merged 1 commit intomainfrom
02-03-feat_minifier_merge_conditional_function_calls_with_side-effect_free_conditions
Feb 6, 2025
Merged

fix(minifier): should not merge conditional function calls if referencing the function has a side-effect#8922
graphite-app[bot] merged 1 commit intomainfrom
02-03-feat_minifier_merge_conditional_function_calls_with_side-effect_free_conditions

Conversation

@sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented Feb 6, 2025

a ? b(c, d) : b(e, d) should not be compressed to b(a ? c : e, d) if b has a side effect.
Because b may update a variable.

For example, compressing the following code changes the behavior.

var foo = () => console.log('foo')
var bar = () => { foo = () => console.log('foo2') }
bar() ? foo(1, 2) : foo(3, 2) // outputs 'foo'

// was compressed into
var foo = () => console.log('foo')
var bar = () => { foo = () => console.log('foo2') }
foo(bar() ? 1 : 3, 2); // outputs 'foo2'

We can improve this if we know that foo is not updated after it was declared (i.e. foo is a const). But I didn't implement it for now.

evaluation steps in detail

Before the compression, the code is evaluated in the following steps:

  1. Evaluate a
  2. Evaluate b
  3. Evaluate c / e
  4. Evaluate d
  5. Evaluate b(c,d) / b(e,d)

After the compression, the code is evaluated in the following steps:

  1. Evaluate b
  2. Evaluate a
  3. Evaluate c / e
  4. Evaluate d
  5. Evaluate b(c,d) / b(e,d)

The steps of 1 and 2 are reversed.

References

Copy link
Member 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.

@github-actions github-actions bot added A-minifier Area - Minifier C-bug Category - Bug labels Feb 6, 2025
@sapphi-red sapphi-red marked this pull request as ready for review February 6, 2025 11:20
@sapphi-red sapphi-red force-pushed the 02-03-feat_minifier_merge_conditional_function_calls_with_side-effect_free_conditions branch from 5559619 to e6446db Compare February 6, 2025 11:25
@codspeed-hq
Copy link

codspeed-hq bot commented Feb 6, 2025

CodSpeed Performance Report

Merging #8922 will not alter performance

Comparing 02-03-feat_minifier_merge_conditional_function_calls_with_side-effect_free_conditions (4a723f1) with main (a520986)

Summary

✅ 33 untouched benchmarks

@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label Feb 6, 2025
Copy link
Member

Boshen commented Feb 6, 2025

Merge activity

  • Feb 6, 10:06 AM EST: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Feb 6, 10:06 AM EST: A user added this pull request to the Graphite merge queue.
  • Feb 6, 10:13 AM EST: A user merged this pull request with the Graphite merge queue.

…cing the function has a side-effect (#8922)

`a ? b(c, d) : b(e, d)` should not be compressed to `b(a ? c : e, d)` if `b` has a side effect.
Because `b` may update `a` variable.

For example, compressing the following code changes the behavior.
```js
var foo = () => console.log('foo')
var bar = () => { foo = () => console.log('foo2') }
bar() ? foo(1, 2) : foo(3, 2) // outputs 'foo'

// was compressed into
var foo = () => console.log('foo')
var bar = () => { foo = () => console.log('foo2') }
foo(bar() ? 1 : 3, 2); // outputs 'foo2'
```

We can improve this if we know that `foo` is not updated after it was declared (i.e. `foo` is a const). But I didn't implement it for now.

<details>
<summary>evaluation steps in detail</summary>

Before the compression, the code is evaluated in the following steps:
1. Evaluate `a`
1. Evaluate `b`
1. Evaluate `c` / `e`
1. Evaluate `d`
1. Evaluate `b(c,d)` / `b(e,d)`

After the compression, the code is evaluated in the following steps:
1. Evaluate `b`
1. Evaluate `a`
1. Evaluate `c` / `e`
1. Evaluate `d`
1. Evaluate `b(c,d)` / `b(e,d)`

The steps of 1 and 2 are reversed.

</details>

**References**
- [Spec of conditional expression](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-conditional-operator-runtime-semantics-evaluation)
- [Spec of call expression](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-function-calls-runtime-semantics-evaluation)
@graphite-app graphite-app bot force-pushed the 02-03-feat_minifier_merge_conditional_function_calls_with_side-effect_free_conditions branch from e6446db to 4a723f1 Compare February 6, 2025 15:07
@graphite-app graphite-app bot merged commit 4a723f1 into main Feb 6, 2025
25 checks passed
@graphite-app graphite-app bot deleted the 02-03-feat_minifier_merge_conditional_function_calls_with_side-effect_free_conditions branch February 6, 2025 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

0-merge Merge with Graphite Merge Queue A-minifier Area - Minifier C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants