-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
[compiler] First cut at dep inference #31386
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { ArrayExpression, Effect, Environment, FunctionExpression, GeneratedSource, HIRFunction, IdentifierId, Instruction, isUseEffectHookType, makeInstructionId } from "../HIR"; | ||
import { createTemporaryPlace } from "../HIR/HIRBuilder"; | ||
|
||
export function inferEffectDependencies( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docblock |
||
env: Environment, | ||
fn: HIRFunction, | ||
): void { | ||
const fnExpressions = new Map<IdentifierId, FunctionExpression>(); | ||
for (const [, block] of fn.body.blocks) { | ||
for (const instr of block.instructions) { | ||
const {value, lvalue} = instr; | ||
if ( | ||
value.kind === 'FunctionExpression' | ||
) { | ||
fnExpressions.set(lvalue.identifier.id, value) | ||
} | ||
} | ||
} | ||
|
||
for (const [, block] of fn.body.blocks) { | ||
Comment on lines
+18
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: combine these loops, you're guaranteed to see a function expression before its usage |
||
let newInstructions = [...block.instructions]; | ||
let addedInstrs = 0; | ||
Comment on lines
+21
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's do the pattern used elsewhere where we only allocate a new array if there's actually something to change. most blocks won't have effects. |
||
for (const [idx, instr] of block.instructions.entries()) { | ||
const {value} = instr; | ||
|
||
/* | ||
* This check is not final. Right now we only look for useEffects without a dependency array. | ||
* This is likely not how we will ship this feature, but it is good enough for us to make progress | ||
* on the implementation and test it. | ||
*/ | ||
if ( | ||
value.kind === 'CallExpression' && | ||
isUseEffectHookType(value.callee.identifier) && | ||
value.args[0].kind === 'Identifier' && | ||
value.args.length === 1 | ||
Comment on lines
+34
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is TypeScript's unsoundness peeking through (ie it assumes array indexing is non-nullable) - these checks need to be reordered, the first could fail if there are no args |
||
) { | ||
const fnExpr = fnExpressions.get(value.args[0].identifier.id); | ||
if (fnExpr != null) { | ||
const deps: ArrayExpression = { | ||
kind: "ArrayExpression", | ||
elements: [...fnExpr.loweredFunc.dependencies], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we probably want to filter non-reactive deps here |
||
loc: GeneratedSource | ||
}; | ||
const depsPlace = createTemporaryPlace(env, GeneratedSource); | ||
depsPlace.effect = Effect.Read; | ||
const newInstruction: Instruction = { | ||
id: makeInstructionId(0), | ||
loc: GeneratedSource, | ||
lvalue: depsPlace, | ||
value: deps, | ||
}; | ||
newInstructions.splice(idx + addedInstrs, 0, newInstruction); | ||
addedInstrs++; | ||
value.args[1] = depsPlace; | ||
} | ||
} | ||
} | ||
block.instructions = newInstructions; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @inferEffectDependencies | ||
const nonreactive = 0; | ||
|
||
function Component({foo, bar}) { | ||
useEffect(() => { | ||
console.log(foo); | ||
console.log(bar); | ||
console.log(nonreactive); | ||
}); | ||
|
||
useEffect(() => { | ||
console.log(foo); | ||
console.log(bar?.baz); | ||
console.log(bar.qux); | ||
}); | ||
|
||
function f() { | ||
console.log(foo); | ||
} | ||
|
||
useEffect(f); | ||
|
||
} | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; // @inferEffectDependencies | ||
const nonreactive = 0; | ||
|
||
function Component(t0) { | ||
const $ = _c(8); | ||
const { foo, bar } = t0; | ||
let t1; | ||
if ($[0] !== foo || $[1] !== bar) { | ||
t1 = () => { | ||
console.log(foo); | ||
console.log(bar); | ||
console.log(nonreactive); | ||
}; | ||
$[0] = foo; | ||
$[1] = bar; | ||
$[2] = t1; | ||
} else { | ||
t1 = $[2]; | ||
} | ||
useEffect(t1, [foo, bar]); | ||
let t2; | ||
if ($[3] !== foo || $[4] !== bar) { | ||
t2 = () => { | ||
console.log(foo); | ||
console.log(bar?.baz); | ||
console.log(bar.qux); | ||
}; | ||
$[3] = foo; | ||
$[4] = bar; | ||
$[5] = t2; | ||
} else { | ||
t2 = $[5]; | ||
} | ||
useEffect(t2, [foo, bar, bar.qux]); | ||
let t3; | ||
if ($[6] !== foo) { | ||
t3 = function f() { | ||
console.log(foo); | ||
}; | ||
$[6] = foo; | ||
$[7] = t3; | ||
} else { | ||
t3 = $[7]; | ||
} | ||
const f = t3; | ||
|
||
useEffect(f); | ||
} | ||
|
||
``` | ||
|
||
### Eval output | ||
(kind: exception) Fixture not implemented |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// @inferEffectDependencies | ||
const nonreactive = 0; | ||
|
||
function Component({foo, bar}) { | ||
useEffect(() => { | ||
console.log(foo); | ||
console.log(bar); | ||
console.log(nonreactive); | ||
}); | ||
|
||
useEffect(() => { | ||
console.log(foo); | ||
console.log(bar?.baz); | ||
console.log(bar.qux); | ||
}); | ||
Comment on lines
+11
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a reference to a global, and a local variable with a non-reactive value? they should be part of the function deps but not added as effect deps |
||
|
||
function f() { | ||
console.log(foo); | ||
} | ||
|
||
// No inferred dep array, the argument is not a lambda | ||
useEffect(f); | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we haven't been doing "experimental" etc for feature flags. If it's off by default, it's experimental.