dillon1000/react
Commit
Browse files [RFC] Refine memoization for Array#map with non-mutating callbacks
Improves memoization for cases such as #2409:
```javascript
const x = [];
useEffect(...);
return <div>{x.map(item => <span>{item}</span>)}</div>;
```
We previously thought that the `x.map(...)` call mutated `x` since its kind was
Mutable. However, in this case we can determine that the map call cannot mutate
`x` (or anything else): the lambda does not mutate any free variables and does
not mutate its arguments.
This PR adds a new flag to function signatures, used for method calls only, that
checks for such cases. The idea is that if the receiver is the only thing that
is mutable — including that there are no args which are function expressions
which mutate their parameters — then we can infer the effect as a read. See
tests which confirm that function expressions which capture or mutate their
params bypass the optimization.Changed paths10 files
First-parent comparisoncompiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts ModifiedM compiler/packages/babel-plugin-react-forget/src/HIR/ObjectShape.ts ModifiedM compiler/packages/babel-plugin-react-forget/src/Inference/InferReferenceEffects.ts ModifiedM compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-map-captures-receiver-noAlias.expect.md ModifiedR compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-missing-memoization-unmodified-array.expect.md →compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-dont-memoize-array-with-capturing-map-after-hook.expect.md RenamedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-dont-memoize-array-with-capturing-map-after-hook.js AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-dont-memoize-array-with-mutable-map-after-hook.expect.md AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-dont-memoize-array-with-mutable-map-after-hook.js AddedA compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-memoize-array-with-immutable-map-after-hook.expect.md AddedR compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-missing-memoization-unmodified-array.js →compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-memoize-array-with-immutable-map-after-hook.js RenamedPatch
Files changed
Rendering syntax-highlighted changes…