react icon indicating copy to clipboard operation
react copied to clipboard

perf: remove allocations in useReducer

Open romgrk opened this issue 2 years ago • 5 comments

Summary

I was reading the source for useReducer and I noticed the returned array is always re-allocated. I figured it could be nice to reuse the value if there has been no change.

One possible concern is if the user is doing unholy things with the returned array, e.g.:

function C() {
  const stateResult = useState(0)
  const [state, setState] = stateResult
  const arrayMineNow = stateResult
  arrayMineNow[0] = 42
  arrayMineNow[1] = 42
  return (<button onClick={() => setState(state + 1)}>{state}</button>)
}

But such abnormal usage could be caught by e.g. sealing the array in dev.

How did you test this change?

yarn test and yarn test --prod

romgrk avatar Sep 16 '23 03:09 romgrk

Hi @romgrk!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

facebook-github-bot avatar Sep 16 '23 03:09 facebook-github-bot

Comparing: a5fc797db14c6e05d4d5c4dbb22a0dd70d41f5d5...96228c8497203c2376b18711327cfe5bff6f2ebe

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js = 166.62 kB 166.62 kB = 52.13 kB 52.13 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 176.05 kB 176.05 kB = 54.97 kB 54.97 kB
facebook-www/ReactDOM-prod.classic.js = 571.73 kB 571.73 kB = 100.64 kB 100.64 kB
facebook-www/ReactDOM-prod.modern.js = 555.46 kB 555.46 kB = 97.75 kB 97.75 kB

Significant size changes

Includes any change greater than 0.2%:

(No significant changes)

Generated by :no_entry_sign: dangerJS against 96228c8497203c2376b18711327cfe5bff6f2ebe

react-sizebot avatar Sep 16 '23 03:09 react-sizebot

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

facebook-github-bot avatar Sep 16 '23 05:09 facebook-github-bot

Ping, if someone is reading PRs. Feel free to decline & close it.

romgrk avatar Feb 07 '24 16:02 romgrk

This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.

github-actions[bot] avatar May 07 '24 17:05 github-actions[bot]

Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you!

github-actions[bot] avatar May 14 '24 18:05 github-actions[bot]