Skip to content

Commit feefe43

Browse files
authored
Refactor Cache Code (#23393)
This PR refactors the cache code by moving it out of ReactFiberCacheComponent to ReactFiberTransitionPool in anticipation of it being reused by multiple stacks (ie. transition tracing)
1 parent 1780659 commit feefe43

11 files changed

+518
-354
lines changed

packages/react-reconciler/src/ReactFiberBeginWork.new.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,7 @@ import {
229229
getWorkInProgressTransitions,
230230
} from './ReactFiberWorkLoop.new';
231231
import {setWorkInProgressVersion} from './ReactMutableSource.new';
232-
import {
233-
requestCacheFromPool,
234-
pushCacheProvider,
235-
pushRootCachePool,
236-
CacheContext,
237-
getSuspendedCachePool,
238-
pushSpawnedCachePool,
239-
getOffscreenDeferredCachePool,
240-
} from './ReactFiberCacheComponent.new';
232+
import {pushCacheProvider, CacheContext} from './ReactFiberCacheComponent.new';
241233
import {createCapturedValue} from './ReactCapturedValue';
242234
import {createClassErrorUpdate} from './ReactFiberThrow.new';
243235
import {completeSuspendedOffscreenHostContainer} from './ReactFiberCompleteWork.new';
@@ -248,6 +240,13 @@ import {
248240
pushTreeId,
249241
pushMaterializedTreeId,
250242
} from './ReactFiberTreeContext.new';
243+
import {
244+
requestCacheFromPool,
245+
pushRootTransition,
246+
getSuspendedCache,
247+
pushTransition,
248+
getOffscreenDeferredCache,
249+
} from './ReactFiberTransition.new';
251250

252251
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
253252

@@ -652,7 +651,7 @@ function updateOffscreenComponent(
652651
// push the cache pool even though we're going to bail out
653652
// because otherwise there'd be a context mismatch
654653
if (current !== null) {
655-
pushSpawnedCachePool(workInProgress, null);
654+
pushTransition(workInProgress, null);
656655
}
657656
}
658657
pushRenderLanes(workInProgress, renderLanes);
@@ -666,7 +665,7 @@ function updateOffscreenComponent(
666665
nextBaseLanes = mergeLanes(prevBaseLanes, renderLanes);
667666
if (enableCache) {
668667
// Save the cache pool so we can resume later.
669-
spawnedCachePool = getOffscreenDeferredCachePool();
668+
spawnedCachePool = getOffscreenDeferredCache();
670669
}
671670
} else {
672671
nextBaseLanes = renderLanes;
@@ -686,7 +685,7 @@ function updateOffscreenComponent(
686685
// push the cache pool even though we're going to bail out
687686
// because otherwise there'd be a context mismatch
688687
if (current !== null) {
689-
pushSpawnedCachePool(workInProgress, null);
688+
pushTransition(workInProgress, null);
690689
}
691690
}
692691

@@ -724,7 +723,7 @@ function updateOffscreenComponent(
724723
// using the same cache. Unless the parent changed, since that means
725724
// there was a refresh.
726725
const prevCachePool = prevState !== null ? prevState.cachePool : null;
727-
pushSpawnedCachePool(workInProgress, prevCachePool);
726+
pushTransition(workInProgress, prevCachePool);
728727
}
729728

730729
pushRenderLanes(workInProgress, subtreeRenderLanes);
@@ -742,7 +741,7 @@ function updateOffscreenComponent(
742741
// using the same cache. Unless the parent changed, since that means
743742
// there was a refresh.
744743
const prevCachePool = prevState.cachePool;
745-
pushSpawnedCachePool(workInProgress, prevCachePool);
744+
pushTransition(workInProgress, prevCachePool);
746745
}
747746

748747
// Since we're not hidden anymore, reset the state
@@ -758,7 +757,7 @@ function updateOffscreenComponent(
758757
// using the same cache. Unless the parent changed, since that means
759758
// there was a refresh.
760759
if (current !== null) {
761-
pushSpawnedCachePool(workInProgress, null);
760+
pushTransition(workInProgress, null);
762761
}
763762
}
764763
}
@@ -1329,7 +1328,7 @@ function updateHostRoot(current, workInProgress, renderLanes) {
13291328

13301329
if (enableCache) {
13311330
const nextCache: Cache = nextState.cache;
1332-
pushRootCachePool(root);
1331+
pushRootTransition(root);
13331332
pushCacheProvider(workInProgress, nextCache);
13341333
if (nextCache !== prevState.cache) {
13351334
// The root cache refreshed.
@@ -1910,7 +1909,7 @@ const SUSPENDED_MARKER: SuspenseState = {
19101909
function mountSuspenseOffscreenState(renderLanes: Lanes): OffscreenState {
19111910
return {
19121911
baseLanes: renderLanes,
1913-
cachePool: getSuspendedCachePool(),
1912+
cachePool: getSuspendedCache(),
19141913
};
19151914
}
19161915

@@ -1939,7 +1938,7 @@ function updateSuspenseOffscreenState(
19391938
}
19401939
} else {
19411940
// If there's no previous cache pool, grab the current one.
1942-
cachePool = getSuspendedCachePool();
1941+
cachePool = getSuspendedCache();
19431942
}
19441943
}
19451944
return {
@@ -3504,7 +3503,7 @@ function attemptEarlyBailoutIfNoScheduledUpdate(
35043503
if (enableCache) {
35053504
const cache: Cache = current.memoizedState.cache;
35063505
pushCacheProvider(workInProgress, cache);
3507-
pushRootCachePool(root);
3506+
pushRootTransition(root);
35083507
}
35093508
if (enableTransitionTracing) {
35103509
workInProgress.memoizedState.transitions = getWorkInProgressTransitions();

packages/react-reconciler/src/ReactFiberBeginWork.old.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,7 @@ import {
229229
getWorkInProgressTransitions,
230230
} from './ReactFiberWorkLoop.old';
231231
import {setWorkInProgressVersion} from './ReactMutableSource.old';
232-
import {
233-
requestCacheFromPool,
234-
pushCacheProvider,
235-
pushRootCachePool,
236-
CacheContext,
237-
getSuspendedCachePool,
238-
pushSpawnedCachePool,
239-
getOffscreenDeferredCachePool,
240-
} from './ReactFiberCacheComponent.old';
232+
import {pushCacheProvider, CacheContext} from './ReactFiberCacheComponent.old';
241233
import {createCapturedValue} from './ReactCapturedValue';
242234
import {createClassErrorUpdate} from './ReactFiberThrow.old';
243235
import {completeSuspendedOffscreenHostContainer} from './ReactFiberCompleteWork.old';
@@ -248,6 +240,13 @@ import {
248240
pushTreeId,
249241
pushMaterializedTreeId,
250242
} from './ReactFiberTreeContext.old';
243+
import {
244+
requestCacheFromPool,
245+
pushRootTransition,
246+
getSuspendedCache,
247+
pushTransition,
248+
getOffscreenDeferredCache,
249+
} from './ReactFiberTransition.old';
251250

252251
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
253252

@@ -652,7 +651,7 @@ function updateOffscreenComponent(
652651
// push the cache pool even though we're going to bail out
653652
// because otherwise there'd be a context mismatch
654653
if (current !== null) {
655-
pushSpawnedCachePool(workInProgress, null);
654+
pushTransition(workInProgress, null);
656655
}
657656
}
658657
pushRenderLanes(workInProgress, renderLanes);
@@ -666,7 +665,7 @@ function updateOffscreenComponent(
666665
nextBaseLanes = mergeLanes(prevBaseLanes, renderLanes);
667666
if (enableCache) {
668667
// Save the cache pool so we can resume later.
669-
spawnedCachePool = getOffscreenDeferredCachePool();
668+
spawnedCachePool = getOffscreenDeferredCache();
670669
}
671670
} else {
672671
nextBaseLanes = renderLanes;
@@ -686,7 +685,7 @@ function updateOffscreenComponent(
686685
// push the cache pool even though we're going to bail out
687686
// because otherwise there'd be a context mismatch
688687
if (current !== null) {
689-
pushSpawnedCachePool(workInProgress, null);
688+
pushTransition(workInProgress, null);
690689
}
691690
}
692691

@@ -724,7 +723,7 @@ function updateOffscreenComponent(
724723
// using the same cache. Unless the parent changed, since that means
725724
// there was a refresh.
726725
const prevCachePool = prevState !== null ? prevState.cachePool : null;
727-
pushSpawnedCachePool(workInProgress, prevCachePool);
726+
pushTransition(workInProgress, prevCachePool);
728727
}
729728

730729
pushRenderLanes(workInProgress, subtreeRenderLanes);
@@ -742,7 +741,7 @@ function updateOffscreenComponent(
742741
// using the same cache. Unless the parent changed, since that means
743742
// there was a refresh.
744743
const prevCachePool = prevState.cachePool;
745-
pushSpawnedCachePool(workInProgress, prevCachePool);
744+
pushTransition(workInProgress, prevCachePool);
746745
}
747746

748747
// Since we're not hidden anymore, reset the state
@@ -758,7 +757,7 @@ function updateOffscreenComponent(
758757
// using the same cache. Unless the parent changed, since that means
759758
// there was a refresh.
760759
if (current !== null) {
761-
pushSpawnedCachePool(workInProgress, null);
760+
pushTransition(workInProgress, null);
762761
}
763762
}
764763
}
@@ -1329,7 +1328,7 @@ function updateHostRoot(current, workInProgress, renderLanes) {
13291328

13301329
if (enableCache) {
13311330
const nextCache: Cache = nextState.cache;
1332-
pushRootCachePool(root);
1331+
pushRootTransition(root);
13331332
pushCacheProvider(workInProgress, nextCache);
13341333
if (nextCache !== prevState.cache) {
13351334
// The root cache refreshed.
@@ -1910,7 +1909,7 @@ const SUSPENDED_MARKER: SuspenseState = {
19101909
function mountSuspenseOffscreenState(renderLanes: Lanes): OffscreenState {
19111910
return {
19121911
baseLanes: renderLanes,
1913-
cachePool: getSuspendedCachePool(),
1912+
cachePool: getSuspendedCache(),
19141913
};
19151914
}
19161915

@@ -1939,7 +1938,7 @@ function updateSuspenseOffscreenState(
19391938
}
19401939
} else {
19411940
// If there's no previous cache pool, grab the current one.
1942-
cachePool = getSuspendedCachePool();
1941+
cachePool = getSuspendedCache();
19431942
}
19441943
}
19451944
return {
@@ -3504,7 +3503,7 @@ function attemptEarlyBailoutIfNoScheduledUpdate(
35043503
if (enableCache) {
35053504
const cache: Cache = current.memoizedState.cache;
35063505
pushCacheProvider(workInProgress, cache);
3507-
pushRootCachePool(root);
3506+
pushRootTransition(root);
35083507
}
35093508
if (enableTransitionTracing) {
35103509
workInProgress.memoizedState.transitions = getWorkInProgressTransitions();

0 commit comments

Comments
 (0)