Skip to content

Commit 8d5f9bc

Browse files
committed
[compiler] Cleanup for @enablePreserveExistingMemoizationGuarantees (facebook#34346)
I tried turning on `@enablePreserveExistingMemoizationGuarantees` by default and cleaned up a couple small things: * We emit freeze calls for StartMemoize deps but these had ValueReason.Other so the message wasn't great. We now treat these like other hook arguments. * PruneNonEscapingScopes was being too aggressive in this mode and memoizing even loads of globals. Switching to MemoizationLevel.Conditional ensures we build a graph that connects through to primitive-returning function calls, but doesn't unnecessarily force memoization otherwise. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34346). * facebook#34347 * __->__ facebook#34346 DiffTrain build for [2710795](facebook@2710795)
1 parent 80612bd commit 8d5f9bc

35 files changed

+5253
-2787
lines changed

compiled/eslint-plugin-react-hooks/index.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40774,7 +40774,7 @@ function computeSignatureForInstruction(context, env, instr) {
4077440774
effects.push({
4077540775
kind: 'Freeze',
4077640776
value: operand,
40777-
reason: ValueReason.Other,
40777+
reason: ValueReason.HookCaptured,
4077840778
});
4077940779
}
4078040780
}
@@ -41508,7 +41508,8 @@ class CollectDependenciesVisitor extends ReactiveFunctionVisitor {
4150841508
this.state = state;
4150941509
this.options = {
4151041510
memoizeJsxElements: !this.env.config.enableForest,
41511-
forceMemoizePrimitives: this.env.config.enableForest,
41511+
forceMemoizePrimitives: this.env.config.enableForest ||
41512+
this.env.config.enablePreserveExistingMemoizationGuarantees,
4151241513
};
4151341514
}
4151441515
computeMemoizationInputs(value, lvalue) {
@@ -41596,9 +41597,14 @@ class CollectDependenciesVisitor extends ReactiveFunctionVisitor {
4159641597
case 'JSXText':
4159741598
case 'BinaryExpression':
4159841599
case 'UnaryExpression': {
41599-
const level = options.forceMemoizePrimitives
41600-
? MemoizationLevel.Memoized
41601-
: MemoizationLevel.Never;
41600+
if (options.forceMemoizePrimitives) {
41601+
const level = MemoizationLevel.Conditional;
41602+
return {
41603+
lvalues: lvalue !== null ? [{ place: lvalue, level }] : [],
41604+
rvalues: [...eachReactiveValueOperand(value)],
41605+
};
41606+
}
41607+
const level = MemoizationLevel.Never;
4160241608
return {
4160341609
lvalues: lvalue !== null ? [{ place: lvalue, level }] : [],
4160441610
rvalues: [],
@@ -41729,9 +41735,7 @@ class CollectDependenciesVisitor extends ReactiveFunctionVisitor {
4172941735
}
4173041736
case 'ComputedLoad':
4173141737
case 'PropertyLoad': {
41732-
const level = options.forceMemoizePrimitives
41733-
? MemoizationLevel.Memoized
41734-
: MemoizationLevel.Conditional;
41738+
const level = MemoizationLevel.Conditional;
4173541739
return {
4173641740
lvalues: lvalue !== null ? [{ place: lvalue, level }] : [],
4173741741
rvalues: [value.object],
@@ -45881,7 +45885,7 @@ function alignMethodCallScopes(fn) {
4588145885
}
4588245886

4588345887
function alignReactiveScopesToBlockScopesHIR(fn) {
45884-
var _a, _b, _c, _d, _e;
45888+
var _a, _b, _c, _d, _e, _f, _g;
4588545889
const activeBlockFallthroughRanges = [];
4588645890
const activeScopes = new Set();
4588745891
const seen = new Set();
@@ -45953,6 +45957,20 @@ function alignReactiveScopesToBlockScopesHIR(fn) {
4595345957
valueBlockNodes.set(fallthrough, node);
4595445958
}
4595545959
}
45960+
else if (terminal.kind === 'goto') {
45961+
const start = activeBlockFallthroughRanges.find(range => range.fallthrough === terminal.block);
45962+
if (start != null && start !== activeBlockFallthroughRanges.at(-1)) {
45963+
const fallthroughBlock = fn.body.blocks.get(start.fallthrough);
45964+
const firstId = (_g = (_f = fallthroughBlock.instructions[0]) === null || _f === void 0 ? void 0 : _f.id) !== null && _g !== void 0 ? _g : fallthroughBlock.terminal.id;
45965+
for (const scope of activeScopes) {
45966+
if (scope.range.end <= terminal.id) {
45967+
continue;
45968+
}
45969+
scope.range.start = makeInstructionId(Math.min(start.range.start, scope.range.start));
45970+
scope.range.end = makeInstructionId(Math.max(firstId, scope.range.end));
45971+
}
45972+
}
45973+
}
4595645974
mapTerminalSuccessors(terminal, successor => {
4595745975
var _a, _b;
4595845976
if (valueBlockNodes.has(successor)) {

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8e60cb7ed55a3dce35bd809b4cf1ad803c59abfd
1+
2710795a1ed339764d2fa76b6d7bc94dded6ee60
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8e60cb7ed55a3dce35bd809b4cf1ad803c59abfd
1+
2710795a1ed339764d2fa76b6d7bc94dded6ee60

compiled/facebook-www/React-dev.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,7 @@ __DEV__ &&
14181418
exports.useTransition = function () {
14191419
return resolveDispatcher().useTransition();
14201420
};
1421-
exports.version = "19.2.0-www-classic-8e60cb7e-20250902";
1421+
exports.version = "19.2.0-www-classic-2710795a-20250903";
14221422
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
14231423
"function" ===
14241424
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-dev.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,7 @@ __DEV__ &&
14181418
exports.useTransition = function () {
14191419
return resolveDispatcher().useTransition();
14201420
};
1421-
exports.version = "19.2.0-www-modern-8e60cb7e-20250902";
1421+
exports.version = "19.2.0-www-modern-2710795a-20250903";
14221422
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
14231423
"function" ===
14241424
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-prod.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,4 +600,4 @@ exports.useSyncExternalStore = function (
600600
exports.useTransition = function () {
601601
return ReactSharedInternals.H.useTransition();
602602
};
603-
exports.version = "19.2.0-www-classic-8e60cb7e-20250902";
603+
exports.version = "19.2.0-www-classic-2710795a-20250903";

compiled/facebook-www/React-prod.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,4 +600,4 @@ exports.useSyncExternalStore = function (
600600
exports.useTransition = function () {
601601
return ReactSharedInternals.H.useTransition();
602602
};
603-
exports.version = "19.2.0-www-modern-8e60cb7e-20250902";
603+
exports.version = "19.2.0-www-modern-2710795a-20250903";

compiled/facebook-www/React-profiling.classic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ exports.useSyncExternalStore = function (
604604
exports.useTransition = function () {
605605
return ReactSharedInternals.H.useTransition();
606606
};
607-
exports.version = "19.2.0-www-classic-8e60cb7e-20250902";
607+
exports.version = "19.2.0-www-classic-2710795a-20250903";
608608
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
609609
"function" ===
610610
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/React-profiling.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ exports.useSyncExternalStore = function (
604604
exports.useTransition = function () {
605605
return ReactSharedInternals.H.useTransition();
606606
};
607-
exports.version = "19.2.0-www-modern-8e60cb7e-20250902";
607+
exports.version = "19.2.0-www-modern-2710795a-20250903";
608608
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
609609
"function" ===
610610
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

0 commit comments

Comments
 (0)