@samitouri / QOS-React / commits / 91f41f5ee3

[hir] Don't track ref value as a valid dep

Sathya Gunasekaran committed May 31, 2023 at 13:54 UTC 91f41f5ee321fe4cebb5f33bf524bbe52fe9b24b
5 files changed +117
compiler/forget/src/ReactiveScopes/PropagateScopeDependencies.ts
+6
@@ -11,6 +11,7 @@ import {
11 IdentifierId,
12 InstructionId,
13 InstructionKind,
14 + isRefValueType,
15 isUseRefType,
16 makeInstructionId,
17 Place,
@@ -323,6 +324,11 @@ class Context {
324 return false;
325 }
326
327 + // ref value is not a valid dep
328 + if (isRefValueType(maybeDependency.identifier)) {
329 + return false;
330 + }
331 +
332 const identifier = maybeDependency.identifier;
333 // If this operand is used in a scope, has a dynamic value, and was defined
334 // before this scope, then its a dependency of the scope.
compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-no-added-to-dep.expect.md new
+48
@@ -0,0 +1,48 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function VideoTab() {
6 + const ref = useRef();
7 + const t = ref.current;
8 + let x = () => {
9 + t;
10 + };
11 +
12 + return <VideoList videos={x} />;
13 +}
14 +
15 +```
16 +
17 +## Code
18 +
19 +```javascript
20 +import { unstable_useMemoCache as useMemoCache } from "react";
21 +function VideoTab() {
22 + const $ = useMemoCache(3);
23 + const ref = useRef();
24 + const t = ref.current;
25 + let t0;
26 + if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
27 + t0 = () => {
28 + t;
29 + };
30 + $[0] = t0;
31 + } else {
32 + t0 = $[0];
33 + }
34 + const x = t0;
35 + const c_1 = $[1] !== x;
36 + let t1;
37 + if (c_1) {
38 + t1 = <VideoList videos={x} />;
39 + $[1] = x;
40 + $[2] = t1;
41 + } else {
42 + t1 = $[2];
43 + }
44 + return t1;
45 +}
46 +
47 +```
48 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-no-added-to-dep.js new
+9
@@ -0,0 +1,9 @@
1 +function VideoTab() {
2 + const ref = useRef();
3 + const t = ref.current;
4 + let x = () => {
5 + t;
6 + };
7 +
8 + return <VideoList videos={x} />;
9 +}
compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-not-added-to-dep-2.expect.md new
+47
@@ -0,0 +1,47 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function Foo({ a }) {
6 + const ref = useRef();
7 + const val = ref.current;
8 + const x = { a, val };
9 +
10 + return <VideoList videos={x} />;
11 +}
12 +
13 +```
14 +
15 +## Code
16 +
17 +```javascript
18 +import { unstable_useMemoCache as useMemoCache } from "react";
19 +function Foo(t21) {
20 + const $ = useMemoCache(4);
21 + const { a } = t21;
22 + const ref = useRef();
23 + const val = ref.current;
24 + const c_0 = $[0] !== a;
25 + let t0;
26 + if (c_0) {
27 + t0 = { a, val };
28 + $[0] = a;
29 + $[1] = t0;
30 + } else {
31 + t0 = $[1];
32 + }
33 + const x = t0;
34 + const c_2 = $[2] !== x;
35 + let t1;
36 + if (c_2) {
37 + t1 = <VideoList videos={x} />;
38 + $[2] = x;
39 + $[3] = t1;
40 + } else {
41 + t1 = $[3];
42 + }
43 + return t1;
44 +}
45 +
46 +```
47 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/ref-current-aliased-not-added-to-dep-2.js new
+7
@@ -0,0 +1,7 @@
1 +function Foo({ a }) {
2 + const ref = useRef();
3 + const val = ref.current;
4 + const x = { a, val };
5 +
6 + return <VideoList videos={x} />;
7 +}