Skip to content

Commit e06c72f

Browse files
authored
[flags] Cleanup enableCache (facebook#31778)
This is landed everywhere
1 parent 2d32056 commit e06c72f

22 files changed

+282
-458
lines changed

packages/react-dom/src/__tests__/ReactCompositeComponent-test.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ describe('ReactCompositeComponent', () => {
537537
});
538538

539539
it('should cleanup even if render() fatals', async () => {
540-
const dispatcherEnabled = __DEV__ || gate(flags => flags.enableCache);
541540
const ownerEnabled = __DEV__;
542541

543542
let stashedDispatcher;
@@ -551,7 +550,7 @@ describe('ReactCompositeComponent', () => {
551550
}
552551

553552
const instance = <BadComponent />;
554-
expect(ReactSharedInternals.A).toBe(dispatcherEnabled ? null : undefined);
553+
expect(ReactSharedInternals.A).toBe(null);
555554

556555
const root = ReactDOMClient.createRoot(document.createElement('div'));
557556
await expect(async () => {
@@ -560,15 +559,11 @@ describe('ReactCompositeComponent', () => {
560559
});
561560
}).rejects.toThrow();
562561

563-
expect(ReactSharedInternals.A).toBe(dispatcherEnabled ? null : undefined);
564-
if (dispatcherEnabled) {
565-
if (ownerEnabled) {
566-
expect(stashedDispatcher.getOwner()).toBe(null);
567-
} else {
568-
expect(stashedDispatcher.getOwner).toBe(undefined);
569-
}
562+
expect(ReactSharedInternals.A).toBe(null);
563+
if (ownerEnabled) {
564+
expect(stashedDispatcher.getOwner()).toBe(null);
570565
} else {
571-
expect(stashedDispatcher).toBe(undefined);
566+
expect(stashedDispatcher.getOwner).toBe(undefined);
572567
}
573568
});
574569

packages/react-markup/src/__tests__/ReactMarkupServer-test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ if (!__EXPERIMENTAL__) {
209209
);
210210
});
211211

212-
// @gate enableCache
213212
it('supports cache', async () => {
214213
let counter = 0;
215214
const getCount = React.cache(() => {

packages/react-reconciler/src/ReactFiberAsyncDispatcher.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@
1010
import type {AsyncDispatcher, Fiber} from './ReactInternalTypes';
1111
import type {Cache} from './ReactFiberCacheComponent';
1212

13-
import {enableCache} from 'shared/ReactFeatureFlags';
1413
import {readContext} from './ReactFiberNewContext';
1514
import {CacheContext} from './ReactFiberCacheComponent';
1615

1716
import {current as currentOwner} from './ReactCurrentFiber';
1817

1918
function getCacheForType<T>(resourceType: () => T): T {
20-
if (!enableCache) {
21-
throw new Error('Not implemented.');
22-
}
2319
const cache: Cache = readContext(CacheContext);
2420
let cacheForType: T | void = (cache.data.get(resourceType): any);
2521
if (cacheForType === undefined) {

packages/react-reconciler/src/ReactFiberBeginWork.js

Lines changed: 49 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ import {
100100
enableProfilerCommitHooks,
101101
enableProfilerTimer,
102102
enableScopeAPI,
103-
enableCache,
104103
enableLazyContextPropagation,
105104
enableSchedulingProfiler,
106105
enableTransitionTracing,
@@ -714,12 +713,10 @@ function updateOffscreenComponent(
714713
cachePool: null,
715714
};
716715
workInProgress.memoizedState = nextState;
717-
if (enableCache) {
718-
// push the cache pool even though we're going to bail out
719-
// because otherwise there'd be a context mismatch
720-
if (current !== null) {
721-
pushTransition(workInProgress, null, null);
722-
}
716+
// push the cache pool even though we're going to bail out
717+
// because otherwise there'd be a context mismatch
718+
if (current !== null) {
719+
pushTransition(workInProgress, null, null);
723720
}
724721
reuseHiddenContextOnStack(workInProgress);
725722
pushOffscreenSuspenseHandler(workInProgress);
@@ -753,7 +750,7 @@ function updateOffscreenComponent(
753750
cachePool: null,
754751
};
755752
workInProgress.memoizedState = nextState;
756-
if (enableCache && current !== null) {
753+
if (current !== null) {
757754
// If the render that spawned this one accessed the cache pool, resume
758755
// using the same cache. Unless the parent changed, since that means
759756
// there was a refresh.
@@ -776,12 +773,10 @@ function updateOffscreenComponent(
776773
if (prevState !== null) {
777774
// We're going from hidden -> visible.
778775
let prevCachePool = null;
779-
if (enableCache) {
780-
// If the render that spawned this one accessed the cache pool, resume
781-
// using the same cache. Unless the parent changed, since that means
782-
// there was a refresh.
783-
prevCachePool = prevState.cachePool;
784-
}
776+
// If the render that spawned this one accessed the cache pool, resume
777+
// using the same cache. Unless the parent changed, since that means
778+
// there was a refresh.
779+
prevCachePool = prevState.cachePool;
785780

786781
let transitions = null;
787782
if (enableTransitionTracing) {
@@ -806,13 +801,11 @@ function updateOffscreenComponent(
806801
// special to do. Need to push to the stack regardless, though, to avoid
807802
// a push/pop misalignment.
808803

809-
if (enableCache) {
810-
// If the render that spawned this one accessed the cache pool, resume
811-
// using the same cache. Unless the parent changed, since that means
812-
// there was a refresh.
813-
if (current !== null) {
814-
pushTransition(workInProgress, null, null);
815-
}
804+
// If the render that spawned this one accessed the cache pool, resume
805+
// using the same cache. Unless the parent changed, since that means
806+
// there was a refresh.
807+
if (current !== null) {
808+
pushTransition(workInProgress, null, null);
816809
}
817810

818811
// We're about to bail out, but we need to push this to the stack anyway
@@ -835,15 +828,13 @@ function deferHiddenOffscreenComponent(
835828
const nextState: OffscreenState = {
836829
baseLanes: nextBaseLanes,
837830
// Save the cache pool so we can resume later.
838-
cachePool: enableCache ? getOffscreenDeferredCache() : null,
831+
cachePool: getOffscreenDeferredCache(),
839832
};
840833
workInProgress.memoizedState = nextState;
841-
if (enableCache) {
842-
// push the cache pool even though we're going to bail out
843-
// because otherwise there'd be a context mismatch
844-
if (current !== null) {
845-
pushTransition(workInProgress, null, null);
846-
}
834+
// push the cache pool even though we're going to bail out
835+
// because otherwise there'd be a context mismatch
836+
if (current !== null) {
837+
pushTransition(workInProgress, null, null);
847838
}
848839

849840
// We're about to bail out, but we need to push this to the stack anyway
@@ -876,10 +867,6 @@ function updateCacheComponent(
876867
workInProgress: Fiber,
877868
renderLanes: Lanes,
878869
) {
879-
if (!enableCache) {
880-
return null;
881-
}
882-
883870
prepareToReadContext(workInProgress, renderLanes);
884871
const parentCache = readContext(CacheContext);
885872

@@ -1480,13 +1467,11 @@ function updateHostRoot(
14801467
pushRootMarkerInstance(workInProgress);
14811468
}
14821469

1483-
if (enableCache) {
1484-
const nextCache: Cache = nextState.cache;
1485-
pushCacheProvider(workInProgress, nextCache);
1486-
if (nextCache !== prevState.cache) {
1487-
// The root cache refreshed.
1488-
propagateContextChange(workInProgress, CacheContext, renderLanes);
1489-
}
1470+
const nextCache: Cache = nextState.cache;
1471+
pushCacheProvider(workInProgress, nextCache);
1472+
if (nextCache !== prevState.cache) {
1473+
// The root cache refreshed.
1474+
propagateContextChange(workInProgress, CacheContext, renderLanes);
14901475
}
14911476

14921477
// This would ideally go inside processUpdateQueue, but because it suspends,
@@ -1990,28 +1975,26 @@ function updateSuspenseOffscreenState(
19901975
renderLanes: Lanes,
19911976
): OffscreenState {
19921977
let cachePool: SpawnedCachePool | null = null;
1993-
if (enableCache) {
1994-
const prevCachePool: SpawnedCachePool | null = prevOffscreenState.cachePool;
1995-
if (prevCachePool !== null) {
1996-
const parentCache = isPrimaryRenderer
1997-
? CacheContext._currentValue
1998-
: CacheContext._currentValue2;
1999-
if (prevCachePool.parent !== parentCache) {
2000-
// Detected a refresh in the parent. This overrides any previously
2001-
// suspended cache.
2002-
cachePool = {
2003-
parent: parentCache,
2004-
pool: parentCache,
2005-
};
2006-
} else {
2007-
// We can reuse the cache from last time. The only thing that would have
2008-
// overridden it is a parent refresh, which we checked for above.
2009-
cachePool = prevCachePool;
2010-
}
1978+
const prevCachePool: SpawnedCachePool | null = prevOffscreenState.cachePool;
1979+
if (prevCachePool !== null) {
1980+
const parentCache = isPrimaryRenderer
1981+
? CacheContext._currentValue
1982+
: CacheContext._currentValue2;
1983+
if (prevCachePool.parent !== parentCache) {
1984+
// Detected a refresh in the parent. This overrides any previously
1985+
// suspended cache.
1986+
cachePool = {
1987+
parent: parentCache,
1988+
pool: parentCache,
1989+
};
20111990
} else {
2012-
// If there's no previous cache pool, grab the current one.
2013-
cachePool = getSuspendedCache();
1991+
// We can reuse the cache from last time. The only thing that would have
1992+
// overridden it is a parent refresh, which we checked for above.
1993+
cachePool = prevCachePool;
20141994
}
1995+
} else {
1996+
// If there's no previous cache pool, grab the current one.
1997+
cachePool = getSuspendedCache();
20151998
}
20161999
return {
20172000
baseLanes: mergeLanes(prevOffscreenState.baseLanes, renderLanes),
@@ -3599,7 +3582,7 @@ function attemptEarlyBailoutIfNoScheduledUpdate(
35993582
// the begin phase. There's still some bookkeeping we that needs to be done
36003583
// in this optimized path, mostly pushing stuff onto the stack.
36013584
switch (workInProgress.tag) {
3602-
case HostRoot:
3585+
case HostRoot: {
36033586
pushHostRootContext(workInProgress);
36043587
const root: FiberRoot = workInProgress.stateNode;
36053588
pushRootTransition(workInProgress, root, renderLanes);
@@ -3608,12 +3591,11 @@ function attemptEarlyBailoutIfNoScheduledUpdate(
36083591
pushRootMarkerInstance(workInProgress);
36093592
}
36103593

3611-
if (enableCache) {
3612-
const cache: Cache = current.memoizedState.cache;
3613-
pushCacheProvider(workInProgress, cache);
3614-
}
3594+
const cache: Cache = current.memoizedState.cache;
3595+
pushCacheProvider(workInProgress, cache);
36153596
resetHydrationState();
36163597
break;
3598+
}
36173599
case HostSingleton:
36183600
case HostComponent:
36193601
pushHostContext(workInProgress);
@@ -3795,10 +3777,8 @@ function attemptEarlyBailoutIfNoScheduledUpdate(
37953777
return updateOffscreenComponent(current, workInProgress, renderLanes);
37963778
}
37973779
case CacheComponent: {
3798-
if (enableCache) {
3799-
const cache: Cache = current.memoizedState.cache;
3800-
pushCacheProvider(workInProgress, cache);
3801-
}
3780+
const cache: Cache = current.memoizedState.cache;
3781+
pushCacheProvider(workInProgress, cache);
38023782
break;
38033783
}
38043784
case TracingMarkerComponent: {
@@ -4085,10 +4065,7 @@ function beginWork(
40854065
break;
40864066
}
40874067
case CacheComponent: {
4088-
if (enableCache) {
4089-
return updateCacheComponent(current, workInProgress, renderLanes);
4090-
}
4091-
break;
4068+
return updateCacheComponent(current, workInProgress, renderLanes);
40924069
}
40934070
case TracingMarkerComponent: {
40944071
if (enableTransitionTracing) {

packages/react-reconciler/src/ReactFiberCacheComponent.js

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010
import type {ReactContext} from 'shared/ReactTypes';
1111
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
1212

13-
import {enableCache} from 'shared/ReactFeatureFlags';
1413
import {REACT_CONTEXT_TYPE} from 'shared/ReactSymbols';
1514

1615
import {pushProvider, popProvider} from './ReactFiberNewContext';
1716
import * as Scheduler from 'scheduler';
1817

1918
// In environments without AbortController (e.g. tests)
2019
// replace it with a lightweight shim that only has the features we use.
21-
const AbortControllerLocal: typeof AbortController = enableCache
22-
? typeof AbortController !== 'undefined'
20+
const AbortControllerLocal: typeof AbortController =
21+
typeof AbortController !== 'undefined'
2322
? AbortController
2423
: // $FlowFixMe[missing-this-annot]
2524
// $FlowFixMe[prop-missing]
@@ -36,9 +35,7 @@ const AbortControllerLocal: typeof AbortController = enableCache
3635
signal.aborted = true;
3736
listeners.forEach(listener => listener());
3837
};
39-
}
40-
: // $FlowFixMe[incompatible-type]
41-
null;
38+
};
4239

4340
export type Cache = {
4441
controller: AbortController,
@@ -63,20 +60,18 @@ const {
6360
unstable_NormalPriority: NormalPriority,
6461
} = Scheduler;
6562

66-
export const CacheContext: ReactContext<Cache> = enableCache
67-
? {
68-
$$typeof: REACT_CONTEXT_TYPE,
69-
// We don't use Consumer/Provider for Cache components. So we'll cheat.
70-
Consumer: (null: any),
71-
Provider: (null: any),
72-
// We'll initialize these at the root.
73-
_currentValue: (null: any),
74-
_currentValue2: (null: any),
75-
_threadCount: 0,
76-
}
77-
: (null: any);
63+
export const CacheContext: ReactContext<Cache> = {
64+
$$typeof: REACT_CONTEXT_TYPE,
65+
// We don't use Consumer/Provider for Cache components. So we'll cheat.
66+
Consumer: (null: any),
67+
Provider: (null: any),
68+
// We'll initialize these at the root.
69+
_currentValue: (null: any),
70+
_currentValue2: (null: any),
71+
_threadCount: 0,
72+
};
7873

79-
if (__DEV__ && enableCache) {
74+
if (__DEV__) {
8075
CacheContext._currentRenderer = null;
8176
CacheContext._currentRenderer2 = null;
8277
}
@@ -85,22 +80,14 @@ if (__DEV__ && enableCache) {
8580
// for retaining the cache once it is in use (retainCache), and releasing the cache
8681
// once it is no longer needed (releaseCache).
8782
export function createCache(): Cache {
88-
if (!enableCache) {
89-
return (null: any);
90-
}
91-
const cache: Cache = {
83+
return {
9284
controller: new AbortControllerLocal(),
9385
data: new Map(),
9486
refCount: 0,
9587
};
96-
97-
return cache;
9888
}
9989

10090
export function retainCache(cache: Cache) {
101-
if (!enableCache) {
102-
return;
103-
}
10491
if (__DEV__) {
10592
if (cache.controller.signal.aborted) {
10693
console.warn(
@@ -114,9 +101,6 @@ export function retainCache(cache: Cache) {
114101

115102
// Cleanup a cache instance, potentially freeing it if there are no more references
116103
export function releaseCache(cache: Cache) {
117-
if (!enableCache) {
118-
return;
119-
}
120104
cache.refCount--;
121105
if (__DEV__) {
122106
if (cache.refCount < 0) {
@@ -134,15 +118,9 @@ export function releaseCache(cache: Cache) {
134118
}
135119

136120
export function pushCacheProvider(workInProgress: Fiber, cache: Cache) {
137-
if (!enableCache) {
138-
return;
139-
}
140121
pushProvider(workInProgress, CacheContext, cache);
141122
}
142123

143124
export function popCacheProvider(workInProgress: Fiber, cache: Cache) {
144-
if (!enableCache) {
145-
return;
146-
}
147125
popProvider(CacheContext, workInProgress);
148126
}

0 commit comments

Comments
 (0)