Skip to content

Commit

Permalink
Update on "compiler: Known hooks/nonescaping scopes dont count as pru…
Browse files Browse the repository at this point in the history
…ned"

There are two cases where it's legit/intended to remove scopes, and we can inline the scope rather than reify a "pruned" scope:
* Scopes that contain a single instruction with a hook call. The fact that we create a scope in this case at all is just an artifact of it being simpler to do this and remove the scope later rather than try to avoid creating it in the first place. So for these scopes, we can just inline them.
* Scopes that are provably non-escaping. Removing the scope is an optimization, not a case of us having to prune away something that should be there. So again, its fine to inline in this case.

I found this from syncing the stack internally and looking at differences in compiled output. The latter case was most common but the first case is just an obvious improvement.

[ghstack-poisoned]
  • Loading branch information
josephsavona committed Jun 8, 2024
1 parent c59c335 commit 21724a5
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,19 @@ class Transform extends ReactiveFunctionTransform<State> {
outerState.hasHook ||= innerState.hasHook;
if (innerState.hasHook) {
if (scope.instructions.length === 1) {
// This was a scope just for a hook call, which doesn't need memoization.
// flatten it away
/*
* This was a scope just for a hook call, which doesn't need memoization.
* flatten it away
*/
return {
kind: "replace-many",
value: scope.instructions,
};
}
// else this scope had multiple instructions and produced some other value:
// mark it as pruned
/*
* else this scope had multiple instructions and produced some other value:
* mark it as pruned
*/
return {
kind: "replace",
value: {
Expand Down

0 comments on commit 21724a5

Please sign in to comment.