Skip to content

[compiler] Allow more instruction kinds btw merged scopes #29881

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

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,15 @@ class Transform extends ReactiveFunctionTransform<ReactiveScopeDependencies | nu
}
case "instruction": {
switch (instr.instruction.value.kind) {
case "BinaryExpression":
case "ComputedLoad":
case "JSXText":
case "LoadGlobal":
case "LoadLocal":
case "Primitive":
case "PropertyLoad": {
case "PropertyLoad":
case "TemplateLiteral":
case "UnaryExpression": {
/*
* We can merge two scopes if there are intervening instructions, but:
* - Only if the instructions are simple and it's okay to make them
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,16 @@ function Component() {
}
useEffect(t1, t2);
let t3;
let t4;
if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
t3 = () => {
setState(someGlobal.value);
};
$[3] = t3;
} else {
t3 = $[3];
}
let t4;
if ($[4] === Symbol.for("react.memo_cache_sentinel")) {
t4 = [someGlobal];
$[3] = t3;
$[4] = t4;
} else {
t3 = $[3];
t4 = $[4];
}
useEffect(t3, t4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,16 @@ function Component() {
}
useEffect(t1, t2);
let t3;
let t4;
if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
t3 = () => {
setState(someGlobal.value);
};
$[3] = t3;
} else {
t3 = $[3];
}
let t4;
if ($[4] === Symbol.for("react.memo_cache_sentinel")) {
t4 = [someGlobal];
$[3] = t3;
$[4] = t4;
} else {
t3 = $[3];
t4 = $[4];
}
useEffect(t3, t4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,16 @@ function Component() {
}
useEffect(t1, t2);
let t3;
let t4;
if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
t3 = () => {
setState(someGlobal);
};
$[3] = t3;
} else {
t3 = $[3];
}
let t4;
if ($[4] === Symbol.for("react.memo_cache_sentinel")) {
t4 = [someGlobal];
$[3] = t3;
$[4] = t4;
} else {
t3 = $[3];
t4 = $[4];
}
useEffect(t3, t4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,16 @@ function Component() {
}
useEffect(t0, t1);
let t2;
let t3;
if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
t2 = () => {
setState(someGlobal);
};
$[2] = t2;
} else {
t2 = $[2];
}
let t3;
if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
t3 = [someGlobal];
$[2] = t2;
$[3] = t3;
} else {
t2 = $[2];
t3 = $[3];
}
useEffect(t2, t3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,16 @@ function Component() {
}
useEffect(t0, t1);
let t2;
let t3;
if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
t2 = () => {
setState(someGlobal.value);
};
$[2] = t2;
} else {
t2 = $[2];
}
let t3;
if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
t3 = [someGlobal];
$[2] = t2;
$[3] = t3;
} else {
t2 = $[2];
t3 = $[3];
}
useEffect(t2, t3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,17 @@ function component() {
```javascript
import { c as _c } from "react/compiler-runtime";
function component() {
const $ = _c(2);
const $ = _c(1);
const [x, setX] = useState(0);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = (v) => setX(v);
const handler = (v) => setX(v);
t0 = <Foo handler={handler} />;
$[0] = t0;
} else {
t0 = $[0];
}
const handler = t0;
let t1;
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
t1 = <Foo handler={handler} />;
$[1] = t1;
} else {
t1 = $[1];
}
return t1;
return t0;
}

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,65 +36,45 @@ function mayMutate() {}
```javascript
import { c as _c } from "react/compiler-runtime";
function ComponentA(props) {
const $ = _c(6);
let a;
let b;
const $ = _c(2);
let t0;
if ($[0] !== props) {
a = [];
b = [];
const a = [];
const b = [];
if (b) {
a.push(props.p0);
}
if (props.p1) {
b.push(props.p2);
}
$[0] = props;
$[1] = a;
$[2] = b;
} else {
a = $[1];
b = $[2];
}
let t0;
if ($[3] !== a || $[4] !== b) {

t0 = <Foo a={a} b={b} />;
$[3] = a;
$[4] = b;
$[5] = t0;
$[0] = props;
$[1] = t0;
} else {
t0 = $[5];
t0 = $[1];
}
return t0;
}

function ComponentB(props) {
const $ = _c(6);
let a;
let b;
const $ = _c(2);
let t0;
if ($[0] !== props) {
a = [];
b = [];
const a = [];
const b = [];
if (mayMutate(b)) {
a.push(props.p0);
}
if (props.p1) {
b.push(props.p2);
}
$[0] = props;
$[1] = a;
$[2] = b;
} else {
a = $[1];
b = $[2];
}
let t0;
if ($[3] !== a || $[4] !== b) {

t0 = <Foo a={a} b={b} />;
$[3] = a;
$[4] = b;
$[5] = t0;
$[0] = props;
$[1] = t0;
} else {
t0 = $[5];
t0 = $[1];
}
return t0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,19 @@ function Component(props) {
```javascript
import { c as _c } from "react/compiler-runtime";
function Component(props) {
const $ = _c(2);
const $ = _c(1);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = () => {
const onEvent = () => {
console.log(42);
};

t0 = <Foo onEvent={onEvent} />;
$[0] = t0;
} else {
t0 = $[0];
}
const onEvent = t0;
let t1;
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
t1 = <Foo onEvent={onEvent} />;
$[1] = t1;
} else {
t1 = $[1];
}
return t1;
return t0;
}

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,20 @@ import { c as _c } from "react/compiler-runtime";
import { Stringify } from "shared-runtime";

function Component(props) {
const $ = _c(2);
let callback;
const $ = _c(1);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
let x;
x = null;
callback = () => {
const callback = () => {
console.log(x);
};

x = {};
$[0] = callback;
} else {
callback = $[0];
}
let t0;
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
t0 = <Stringify callback={callback} shouldInvokeFns={true} />;
$[1] = t0;
$[0] = t0;
} else {
t0 = $[1];
t0 = $[0];
}
return t0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function Component() {
```javascript
import { c as _c } from "react/compiler-runtime";
function Component() {
const $ = _c(2);
const $ = _c(1);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
let x;
Expand All @@ -27,20 +27,13 @@ function Component() {
x = 9;
};

t0 = bar(foo);
const y = bar(foo);
t0 = <Child y={y} />;
$[0] = t0;
} else {
t0 = $[0];
}
const y = t0;
let t1;
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
t1 = <Child y={y} />;
$[1] = t1;
} else {
t1 = $[1];
}
return t1;
return t0;
}

```
Expand Down
Loading