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

[Compiler Bug]: math operators prevent caching #32364

Closed
1 of 4 tasks
lstkz opened this issue Feb 12, 2025 · 2 comments
Closed
1 of 4 tasks

[Compiler Bug]: math operators prevent caching #32364

lstkz opened this issue Feb 12, 2025 · 2 comments
Labels
Component: Optimizing Compiler Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug Type: Bug

Comments

@lstkz
Copy link

lstkz commented Feb 12, 2025

What kind of issue is this?

  • React Compiler core (the JS output is incorrect, or your app works incorrectly after optimization)
  • babel-plugin-react-compiler (build issue installing or using the Babel plugin)
  • eslint-plugin-react-compiler (build issue installing or using the eslint plugin)
  • react-compiler-healthcheck (build issue installing or using the healthcheck script)

Link to repro

https://playground.react.dev/#N4Igzg9grgTgxgUxALhAMygOzgFwJYSYAEAYjHgpgCYAyeYOAFMEWuZVWEQL4CURwADrEiAGwQ4iADyIBeIgikAHSmDwA3BIzYVqYXgG5hRaQGpTRkTAmxijYyaIAeKhoB8DxwKndPzgPSu6h4ihsLcINxAA

Repro steps

  let x = expensive(friends);
  x++;

It's not memoized, and expensive is called on every render.


https://playground.react.dev/#N4Igzg9grgTgxgUxALhAMygOzgFwJYSYAEAYjHgpgCYAyeYOAFMEWuZVWADREQ4AWCGEQC+ASiLAAOsSIAbBDiIAPIgF4iCZQAdKYPADcEjNhWpgxAbhlEieNEUZ9BMCdNm3lAai-XZImyIYRVhiRkDbAB4qQwA+CNtJZQCPIkiAehiDeNkrGREQESA

  let x = expensive(friends);
  if (other) {
    x++;
  }

Similar. Not cached.


https://playground.react.dev/#N4Igzg9grgTgxgUxALhAMygOzgFwJYSYAEAYjHgpgCYAyeYOAFMEWuZVWADREQ4AWCGEQC+ASiLAAOsSIAbBDiIAPIgF4iCZQAdKYPADcEjNhWpgxAbhlEieNEUZ9BMCdNm3VGgIw3RfmEVYYkY-WwAeKkMAPjDbSWUROPCAeiiDWNkrGREQESA

  let x = expensive(friends);
  if (other) {
    x = 1
  }

Cached properly.

How often does this bug happen?

Every time

What version of React are you using?

latest from playground

What version of React Compiler are you using?

latest from playground

@lstkz lstkz added Component: Optimizing Compiler Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug Type: Bug labels Feb 12, 2025
@guillaumebrunerie
Copy link

I think this is a duplicate of #29172. The compiler does not cache expensive function calls. It only caches values that risk triggering a useless rerender, and by doing x++ you’re telling the compiler that x is a number which cannot trigger useless rerenders.

@josephsavona
Copy link
Contributor

@guillaumebrunerie yup this is a duplicate, thanks for flagging! Great explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Optimizing Compiler Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug Type: Bug
Projects
None yet
Development

No branches or pull requests

3 participants