[compiler] Lower ObjectMethod to FunctionExpression HIR for consistent per-function memoization#36225
Open
sleitor wants to merge 1 commit intofacebook:mainfrom
Open
[compiler] Lower ObjectMethod to FunctionExpression HIR for consistent per-function memoization#36225sleitor wants to merge 1 commit intofacebook:mainfrom
sleitor wants to merge 1 commit intofacebook:mainfrom
Conversation
…t per-function memoization
4 tasks
dimaMachina
reviewed
Apr 6, 2026
Contributor
dimaMachina
left a comment
There was a problem hiding this comment.
nice clean fix, cc @josephsavona what do you think about this implementation?
method shorthand was introduced as syntactic sugar for function () {} so its safe treat them same in React compiler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #36151
React Compiler produces inconsistent memoization for objects depending on whether methods use shorthand syntax or function expression syntax:
{ foo() {} }→ whole object memoized as one unit (less optimal){ foo() {} }→ each function memoized separately (same as{ foo: () => {} })The Fix
This PR takes the correct direction as suggested during review of #36224. Rather than merging FunctionExpression scopes into the ObjectExpression scope (which was the wrong approach in #36224), this PR lowers ObjectMethod HIR nodes to FunctionExpression HIR nodes during HIR construction in
BuildHIR.ts.Key changes
BuildHIR.ts:lowerObjectMethod()now returns aFunctionExpressionHIR node (withtype: 'FunctionExpression') instead of anObjectMethodHIR nodetype: 'property'instead oftype: 'method', so codegen emits it as a regular property with a function value ({ foo: function() {} })AlignObjectMethodScopes.ts:findScopesToMerge()is now a no-op since ObjectMethod nodes are no longer emitted. The scope merging was only needed because ObjectMethod nodes needed to be in the same reactive block as their enclosing ObjectExpression for correct codegen. Since lowering to FunctionExpression removes that constraint, no merging is needed.Result
Method shorthands now flow through the same code path as arrow/function-expression properties, getting per-function reactive scopes automatically. This means:
Tests
All existing tests pass (0 failures). 22 snapshot files were updated to reflect the improved memoization output.
Note: Supersedes the wrong-direction approach in #36224 (which is left open for reference).