Commit 6a34f35
committed
[compiler] Optimize props spread for common cases (#34900)
As part of the new inference model we updated to (correctly) treat
destructuring spread as creating a new mutable object. This had the
unfortunate side-effect of reducing precision on destructuring of props,
though:
```js
function Component({x, ...rest}) {
const z = rest.z;
identity(z);
return <Stringify x={x} z={z} />;
}
```
Memoized as the following, where we don't realize that `z` is actually
frozen:
```js
function Component(t0) {
const $ = _c(6);
let x;
let z;
if ($[0] !== t0) {
const { x: t1, ...rest } = t0;
x = t1;
z = rest.z;
identity(z);
...
```
#34341 was our first thought of how to do this (thanks @poteto for
exploring this idea!). But during review it became clear that it was a
bit more complicated than I had thought. So this PR explores a more
conservative alternative. The idea is:
* Track known sources of frozen values: component props, hook params,
and hook return values.
* Find all object spreads where the rvalue is a known frozen value.
* Look at how such objects are used, and if they are only used to access
properties (PropertyLoad/Destructure), pass to hooks, or pass to jsx
then we can be very confident the object is not mutated. We consider any
such objects to be frozen, even though technically spread creates a new
object.
See new fixtures for more examples.
---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34900).
* __->__ #34900
* #34887
DiffTrain build for [c35f6a3](c35f6a3)1 parent 7eb4ebd commit 6a34f35
File tree
26 files changed
+317
-95
lines changed- compiled-rn
- facebook-fbsource/xplat/js
- RKJSModules/vendor/react
- react-dom/cjs
- react-test-renderer/cjs
- react/cjs
- react-native-github/Libraries/Renderer
- implementations
- tools/eslint-plugin-react-hooks
- cjs
26 files changed
+317
-95
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
404 | 404 | | |
405 | 405 | | |
406 | 406 | | |
407 | | - | |
| 407 | + | |
408 | 408 | | |
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
203 | 203 | | |
204 | 204 | | |
205 | 205 | | |
206 | | - | |
| 206 | + | |
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
203 | 203 | | |
204 | 204 | | |
205 | 205 | | |
206 | | - | |
| 206 | + | |
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
30117 | 30117 | | |
30118 | 30118 | | |
30119 | 30119 | | |
30120 | | - | |
| 30120 | + | |
30121 | 30121 | | |
30122 | 30122 | | |
30123 | 30123 | | |
30124 | | - | |
| 30124 | + | |
30125 | 30125 | | |
30126 | 30126 | | |
30127 | 30127 | | |
| |||
30158 | 30158 | | |
30159 | 30159 | | |
30160 | 30160 | | |
30161 | | - | |
| 30161 | + | |
30162 | 30162 | | |
30163 | 30163 | | |
30164 | | - | |
| 30164 | + | |
30165 | 30165 | | |
30166 | 30166 | | |
30167 | 30167 | | |
| |||
30311 | 30311 | | |
30312 | 30312 | | |
30313 | 30313 | | |
30314 | | - | |
| 30314 | + | |
30315 | 30315 | | |
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
17581 | 17581 | | |
17582 | 17582 | | |
17583 | 17583 | | |
17584 | | - | |
| 17584 | + | |
17585 | 17585 | | |
17586 | 17586 | | |
17587 | 17587 | | |
17588 | 17588 | | |
17589 | 17589 | | |
17590 | 17590 | | |
17591 | | - | |
| 17591 | + | |
17592 | 17592 | | |
17593 | 17593 | | |
17594 | 17594 | | |
| |||
17610 | 17610 | | |
17611 | 17611 | | |
17612 | 17612 | | |
17613 | | - | |
| 17613 | + | |
17614 | 17614 | | |
17615 | 17615 | | |
17616 | | - | |
| 17616 | + | |
17617 | 17617 | | |
17618 | 17618 | | |
17619 | 17619 | | |
| |||
17720 | 17720 | | |
17721 | 17721 | | |
17722 | 17722 | | |
17723 | | - | |
| 17723 | + | |
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
19703 | 19703 | | |
19704 | 19704 | | |
19705 | 19705 | | |
19706 | | - | |
| 19706 | + | |
19707 | 19707 | | |
19708 | 19708 | | |
19709 | 19709 | | |
19710 | 19710 | | |
19711 | 19711 | | |
19712 | 19712 | | |
19713 | | - | |
| 19713 | + | |
19714 | 19714 | | |
19715 | 19715 | | |
19716 | 19716 | | |
| |||
19732 | 19732 | | |
19733 | 19733 | | |
19734 | 19734 | | |
19735 | | - | |
| 19735 | + | |
19736 | 19736 | | |
19737 | 19737 | | |
19738 | | - | |
| 19738 | + | |
19739 | 19739 | | |
19740 | 19740 | | |
19741 | 19741 | | |
| |||
19858 | 19858 | | |
19859 | 19859 | | |
19860 | 19860 | | |
19861 | | - | |
| 19861 | + | |
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
30173 | 30173 | | |
30174 | 30174 | | |
30175 | 30175 | | |
30176 | | - | |
| 30176 | + | |
30177 | 30177 | | |
30178 | 30178 | | |
30179 | 30179 | | |
30180 | | - | |
| 30180 | + | |
30181 | 30181 | | |
30182 | 30182 | | |
30183 | 30183 | | |
| |||
30214 | 30214 | | |
30215 | 30215 | | |
30216 | 30216 | | |
30217 | | - | |
| 30217 | + | |
30218 | 30218 | | |
30219 | 30219 | | |
30220 | | - | |
| 30220 | + | |
30221 | 30221 | | |
30222 | 30222 | | |
30223 | 30223 | | |
| |||
30683 | 30683 | | |
30684 | 30684 | | |
30685 | 30685 | | |
30686 | | - | |
| 30686 | + | |
30687 | 30687 | | |
30688 | 30688 | | |
30689 | 30689 | | |
| |||
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
17592 | 17592 | | |
17593 | 17593 | | |
17594 | 17594 | | |
17595 | | - | |
| 17595 | + | |
17596 | 17596 | | |
17597 | 17597 | | |
17598 | 17598 | | |
17599 | 17599 | | |
17600 | 17600 | | |
17601 | 17601 | | |
17602 | | - | |
| 17602 | + | |
17603 | 17603 | | |
17604 | 17604 | | |
17605 | 17605 | | |
| |||
17621 | 17621 | | |
17622 | 17622 | | |
17623 | 17623 | | |
17624 | | - | |
| 17624 | + | |
17625 | 17625 | | |
17626 | 17626 | | |
17627 | | - | |
| 17627 | + | |
17628 | 17628 | | |
17629 | 17629 | | |
17630 | 17630 | | |
| |||
17884 | 17884 | | |
17885 | 17885 | | |
17886 | 17886 | | |
17887 | | - | |
| 17887 | + | |
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
19718 | 19718 | | |
19719 | 19719 | | |
19720 | 19720 | | |
19721 | | - | |
| 19721 | + | |
19722 | 19722 | | |
19723 | 19723 | | |
19724 | 19724 | | |
19725 | 19725 | | |
19726 | 19726 | | |
19727 | 19727 | | |
19728 | | - | |
| 19728 | + | |
19729 | 19729 | | |
19730 | 19730 | | |
19731 | 19731 | | |
| |||
19747 | 19747 | | |
19748 | 19748 | | |
19749 | 19749 | | |
19750 | | - | |
| 19750 | + | |
19751 | 19751 | | |
19752 | 19752 | | |
19753 | | - | |
| 19753 | + | |
19754 | 19754 | | |
19755 | 19755 | | |
19756 | 19756 | | |
| |||
20026 | 20026 | | |
20027 | 20027 | | |
20028 | 20028 | | |
20029 | | - | |
| 20029 | + | |
20030 | 20030 | | |
20031 | 20031 | | |
20032 | 20032 | | |
| |||
0 commit comments