Skip to content

Commit

Permalink
Refactor Cache Code (#23393)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
lunaruan authored Mar 1, 2022
1 parent 1780659 commit feefe43
Show file tree
Hide file tree
Showing 11 changed files with 518 additions and 354 deletions.
37 changes: 18 additions & 19 deletions packages/react-reconciler/src/ReactFiberBeginWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,7 @@ import {
getWorkInProgressTransitions,
} from './ReactFiberWorkLoop.new';
import {setWorkInProgressVersion} from './ReactMutableSource.new';
import {
requestCacheFromPool,
pushCacheProvider,
pushRootCachePool,
CacheContext,
getSuspendedCachePool,
pushSpawnedCachePool,
getOffscreenDeferredCachePool,
} from './ReactFiberCacheComponent.new';
import {pushCacheProvider, CacheContext} from './ReactFiberCacheComponent.new';
import {createCapturedValue} from './ReactCapturedValue';
import {createClassErrorUpdate} from './ReactFiberThrow.new';
import {completeSuspendedOffscreenHostContainer} from './ReactFiberCompleteWork.new';
Expand All @@ -248,6 +240,13 @@ import {
pushTreeId,
pushMaterializedTreeId,
} from './ReactFiberTreeContext.new';
import {
requestCacheFromPool,
pushRootTransition,
getSuspendedCache,
pushTransition,
getOffscreenDeferredCache,
} from './ReactFiberTransition.new';

const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;

Expand Down Expand Up @@ -652,7 +651,7 @@ function updateOffscreenComponent(
// push the cache pool even though we're going to bail out
// because otherwise there'd be a context mismatch
if (current !== null) {
pushSpawnedCachePool(workInProgress, null);
pushTransition(workInProgress, null);
}
}
pushRenderLanes(workInProgress, renderLanes);
Expand All @@ -666,7 +665,7 @@ function updateOffscreenComponent(
nextBaseLanes = mergeLanes(prevBaseLanes, renderLanes);
if (enableCache) {
// Save the cache pool so we can resume later.
spawnedCachePool = getOffscreenDeferredCachePool();
spawnedCachePool = getOffscreenDeferredCache();
}
} else {
nextBaseLanes = renderLanes;
Expand All @@ -686,7 +685,7 @@ function updateOffscreenComponent(
// push the cache pool even though we're going to bail out
// because otherwise there'd be a context mismatch
if (current !== null) {
pushSpawnedCachePool(workInProgress, null);
pushTransition(workInProgress, null);
}
}

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

pushRenderLanes(workInProgress, subtreeRenderLanes);
Expand All @@ -742,7 +741,7 @@ function updateOffscreenComponent(
// using the same cache. Unless the parent changed, since that means
// there was a refresh.
const prevCachePool = prevState.cachePool;
pushSpawnedCachePool(workInProgress, prevCachePool);
pushTransition(workInProgress, prevCachePool);
}

// Since we're not hidden anymore, reset the state
Expand All @@ -758,7 +757,7 @@ function updateOffscreenComponent(
// using the same cache. Unless the parent changed, since that means
// there was a refresh.
if (current !== null) {
pushSpawnedCachePool(workInProgress, null);
pushTransition(workInProgress, null);
}
}
}
Expand Down Expand Up @@ -1329,7 +1328,7 @@ function updateHostRoot(current, workInProgress, renderLanes) {

if (enableCache) {
const nextCache: Cache = nextState.cache;
pushRootCachePool(root);
pushRootTransition(root);
pushCacheProvider(workInProgress, nextCache);
if (nextCache !== prevState.cache) {
// The root cache refreshed.
Expand Down Expand Up @@ -1910,7 +1909,7 @@ const SUSPENDED_MARKER: SuspenseState = {
function mountSuspenseOffscreenState(renderLanes: Lanes): OffscreenState {
return {
baseLanes: renderLanes,
cachePool: getSuspendedCachePool(),
cachePool: getSuspendedCache(),
};
}

Expand Down Expand Up @@ -1939,7 +1938,7 @@ function updateSuspenseOffscreenState(
}
} else {
// If there's no previous cache pool, grab the current one.
cachePool = getSuspendedCachePool();
cachePool = getSuspendedCache();
}
}
return {
Expand Down Expand Up @@ -3504,7 +3503,7 @@ function attemptEarlyBailoutIfNoScheduledUpdate(
if (enableCache) {
const cache: Cache = current.memoizedState.cache;
pushCacheProvider(workInProgress, cache);
pushRootCachePool(root);
pushRootTransition(root);
}
if (enableTransitionTracing) {
workInProgress.memoizedState.transitions = getWorkInProgressTransitions();
Expand Down
37 changes: 18 additions & 19 deletions packages/react-reconciler/src/ReactFiberBeginWork.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,7 @@ import {
getWorkInProgressTransitions,
} from './ReactFiberWorkLoop.old';
import {setWorkInProgressVersion} from './ReactMutableSource.old';
import {
requestCacheFromPool,
pushCacheProvider,
pushRootCachePool,
CacheContext,
getSuspendedCachePool,
pushSpawnedCachePool,
getOffscreenDeferredCachePool,
} from './ReactFiberCacheComponent.old';
import {pushCacheProvider, CacheContext} from './ReactFiberCacheComponent.old';
import {createCapturedValue} from './ReactCapturedValue';
import {createClassErrorUpdate} from './ReactFiberThrow.old';
import {completeSuspendedOffscreenHostContainer} from './ReactFiberCompleteWork.old';
Expand All @@ -248,6 +240,13 @@ import {
pushTreeId,
pushMaterializedTreeId,
} from './ReactFiberTreeContext.old';
import {
requestCacheFromPool,
pushRootTransition,
getSuspendedCache,
pushTransition,
getOffscreenDeferredCache,
} from './ReactFiberTransition.old';

const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;

Expand Down Expand Up @@ -652,7 +651,7 @@ function updateOffscreenComponent(
// push the cache pool even though we're going to bail out
// because otherwise there'd be a context mismatch
if (current !== null) {
pushSpawnedCachePool(workInProgress, null);
pushTransition(workInProgress, null);
}
}
pushRenderLanes(workInProgress, renderLanes);
Expand All @@ -666,7 +665,7 @@ function updateOffscreenComponent(
nextBaseLanes = mergeLanes(prevBaseLanes, renderLanes);
if (enableCache) {
// Save the cache pool so we can resume later.
spawnedCachePool = getOffscreenDeferredCachePool();
spawnedCachePool = getOffscreenDeferredCache();
}
} else {
nextBaseLanes = renderLanes;
Expand All @@ -686,7 +685,7 @@ function updateOffscreenComponent(
// push the cache pool even though we're going to bail out
// because otherwise there'd be a context mismatch
if (current !== null) {
pushSpawnedCachePool(workInProgress, null);
pushTransition(workInProgress, null);
}
}

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

pushRenderLanes(workInProgress, subtreeRenderLanes);
Expand All @@ -742,7 +741,7 @@ function updateOffscreenComponent(
// using the same cache. Unless the parent changed, since that means
// there was a refresh.
const prevCachePool = prevState.cachePool;
pushSpawnedCachePool(workInProgress, prevCachePool);
pushTransition(workInProgress, prevCachePool);
}

// Since we're not hidden anymore, reset the state
Expand All @@ -758,7 +757,7 @@ function updateOffscreenComponent(
// using the same cache. Unless the parent changed, since that means
// there was a refresh.
if (current !== null) {
pushSpawnedCachePool(workInProgress, null);
pushTransition(workInProgress, null);
}
}
}
Expand Down Expand Up @@ -1329,7 +1328,7 @@ function updateHostRoot(current, workInProgress, renderLanes) {

if (enableCache) {
const nextCache: Cache = nextState.cache;
pushRootCachePool(root);
pushRootTransition(root);
pushCacheProvider(workInProgress, nextCache);
if (nextCache !== prevState.cache) {
// The root cache refreshed.
Expand Down Expand Up @@ -1910,7 +1909,7 @@ const SUSPENDED_MARKER: SuspenseState = {
function mountSuspenseOffscreenState(renderLanes: Lanes): OffscreenState {
return {
baseLanes: renderLanes,
cachePool: getSuspendedCachePool(),
cachePool: getSuspendedCache(),
};
}

Expand Down Expand Up @@ -1939,7 +1938,7 @@ function updateSuspenseOffscreenState(
}
} else {
// If there's no previous cache pool, grab the current one.
cachePool = getSuspendedCachePool();
cachePool = getSuspendedCache();
}
}
return {
Expand Down Expand Up @@ -3504,7 +3503,7 @@ function attemptEarlyBailoutIfNoScheduledUpdate(
if (enableCache) {
const cache: Cache = current.memoizedState.cache;
pushCacheProvider(workInProgress, cache);
pushRootCachePool(root);
pushRootTransition(root);
}
if (enableTransitionTracing) {
workInProgress.memoizedState.transitions = getWorkInProgressTransitions();
Expand Down
Loading

0 comments on commit feefe43

Please sign in to comment.