GeezerVison is showing github.com — Initial implementation of cache cleanup (#22510) · react/react@fa9bea0
File treeExpand file treeCollapse file tree Open diff view settings react-devtools-shared/src/__tests__/__snapshots__
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-shared/src/__tests__/__snapshots__/profilingCache-test.js.snap +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -73,7 +73,7 @@ Object { | 73 | 73 | }, | 74 | 74 | }, | 75 | 75 | "duration": 15, | 76 | | - "effectDuration": null, | | 76 | + "effectDuration": 0, | 77 | 77 | "fiberActualDurations": Map { | 78 | 78 | 1 => 15, | 79 | 79 | 2 => 15, | @@ -86,7 +86,7 @@ Object { | 86 | 86 | 3 => 3, | 87 | 87 | 4 => 2, | 88 | 88 | }, | 89 | | - "passiveEffectDuration": null, | | 89 | + "passiveEffectDuration": 0, | 90 | 90 | "priorityLevel": "Immediate", | 91 | 91 | "timestamp": 15, | 92 | 92 | "updaters": Array [ | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/server/ReactPartialRendererHooks.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -216,6 +216,10 @@ export function resetHooksState(): void { | 216 | 216 | workInProgressHook = null; | 217 | 217 | } | 218 | 218 |
| | 219 | +function getCacheSignal() { | | 220 | + throw new Error('Not implemented.'); | | 221 | +} | | 222 | + | 219 | 223 | function getCacheForType<T>(resourceType: () => T): T { | 220 | 224 | throw new Error('Not implemented.'); | 221 | 225 | } | @@ -551,6 +555,7 @@ export const Dispatcher: DispatcherType = { | 551 | 555 | }; | 552 | 556 |
| 553 | 557 | if (enableCache) { | | 558 | + Dispatcher.getCacheSignal = getCacheSignal; | 554 | 559 | Dispatcher.getCacheForType = getCacheForType; | 555 | 560 | Dispatcher.useCacheRefresh = useCacheRefresh; | 556 | 561 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCacheComponent.new.js +80-5Lines changed: 80 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,8 +18,13 @@ import {REACT_CONTEXT_TYPE} from 'shared/ReactSymbols'; | 18 | 18 | import {isPrimaryRenderer} from './ReactFiberHostConfig'; | 19 | 19 | import {createCursor, push, pop} from './ReactFiberStack.new'; | 20 | 20 | import {pushProvider, popProvider} from './ReactFiberNewContext.new'; | | 21 | +import * as Scheduler from 'scheduler'; | 21 | 22 |
| 22 | | -export type Cache = Map<() => mixed, mixed>; | | 23 | +export type Cache = {| | | 24 | + controller: AbortController, | | 25 | + data: Map<() => mixed, mixed>, | | 26 | + refCount: number, | | 27 | +|}; | 23 | 28 |
| 24 | 29 | export type CacheComponentState = {| | 25 | 30 | +parent: Cache, | @@ -31,6 +36,13 @@ export type SpawnedCachePool = {| | 31 | 36 | +pool: Cache, | 32 | 37 | |}; | 33 | 38 |
| | 39 | +// Intentionally not named imports because Rollup would | | 40 | +// use dynamic dispatch for CommonJS interop named imports. | | 41 | +const { | | 42 | + unstable_scheduleCallback: scheduleCallback, | | 43 | + unstable_NormalPriority: NormalPriority, | | 44 | +} = Scheduler; | | 45 | + | 34 | 46 | export const CacheContext: ReactContext<Cache> = enableCache | 35 | 47 | ? { | 36 | 48 | $$typeof: REACT_CONTEXT_TYPE, | @@ -57,6 +69,58 @@ let pooledCache: Cache | null = null; | 57 | 69 | // cache from the render that suspended. | 58 | 70 | const prevFreshCacheOnStack: StackCursor<Cache | null> = createCursor(null); | 59 | 71 |
| | 72 | +// Creates a new empty Cache instance with a ref-count of 0. The caller is responsible | | 73 | +// for retaining the cache once it is in use (retainCache), and releasing the cache | | 74 | +// once it is no longer needed (releaseCache). | | 75 | +export function createCache(): Cache { | | 76 | + if (!enableCache) { | | 77 | + return (null: any); | | 78 | + } | | 79 | + const cache: Cache = { | | 80 | + controller: new AbortController(), | | 81 | + data: new Map(), | | 82 | + refCount: 0, | | 83 | + }; | | 84 | + | | 85 | + return cache; | | 86 | +} | | 87 | + | | 88 | +export function retainCache(cache: Cache) { | | 89 | + if (!enableCache) { | | 90 | + return; | | 91 | + } | | 92 | + if (__DEV__) { | | 93 | + if (cache.controller.signal.aborted) { | | 94 | + console.warn( | | 95 | + 'A cache instance was retained after it was already freed. ' + | | 96 | + 'This likely indicates a bug in React.', | | 97 | + ); | | 98 | + } | | 99 | + } | | 100 | + cache.refCount++; | | 101 | +} | | 102 | + | | 103 | +// Cleanup a cache instance, potentially freeing it if there are no more references | | 104 | +export function releaseCache(cache: Cache) { | | 105 | + if (!enableCache) { | | 106 | + return; | | 107 | + } | | 108 | + cache.refCount--; | | 109 | + if (__DEV__) { | | 110 | + if (cache.refCount < 0) { | | 111 | + console.warn( | | 112 | + 'A cache instance was released after it was already freed. ' + | | 113 | + 'This likely indicates a bug in React.', | | 114 | + ); | | 115 | + } | | 116 | + } | | 117 | + if (cache.refCount === 0) { | | 118 | + scheduleCallback(NormalPriority, () => { | | 119 | + cache.controller.abort(); | | 120 | + }); | | 121 | + } | | 122 | +} | | 123 | + | 60 | 124 | export function pushCacheProvider(workInProgress: Fiber, cache: Cache) { | 61 | 125 | if (!enableCache) { | 62 | 126 | return; | @@ -78,8 +142,14 @@ export function requestCacheFromPool(renderLanes: Lanes): Cache { | 78 | 142 | if (pooledCache !== null) { | 79 | 143 | return pooledCache; | 80 | 144 | } | 81 | | - // Create a fresh cache. | 82 | | - pooledCache = new Map(); | | 145 | + // Create a fresh cache. The pooled cache must be owned - it is freed | | 146 | + // in releaseRootPooledCache() - but the cache instance handed out | | 147 | + // is retained/released in the commit phase of the component that | | 148 | + // references is (ie the host root, cache boundary, suspense component) | | 149 | + // Ie, pooledCache is conceptually an Option<Arc<Cache>> (owned), | | 150 | + // whereas the return value of this function is a &Arc<Cache> (borrowed). | | 151 | + pooledCache = createCache(); | | 152 | + retainCache(pooledCache); | 83 | 153 | return pooledCache; | 84 | 154 | } | 85 | 155 |
| @@ -91,7 +161,13 @@ export function pushRootCachePool(root: FiberRoot) { | 91 | 161 | // from `root.pooledCache`. If it's currently `null`, we will lazily | 92 | 162 | // initialize it the first type it's requested. However, we only mutate | 93 | 163 | // the root itself during the complete/unwind phase of the HostRoot. | 94 | | - pooledCache = root.pooledCache; | | 164 | + const rootCache = root.pooledCache; | | 165 | + if (rootCache != null) { | | 166 | + pooledCache = rootCache; | | 167 | + root.pooledCache = null; | | 168 | + } else { | | 169 | + pooledCache = null; | | 170 | + } | 95 | 171 | } | 96 | 172 |
| 97 | 173 | export function popRootCachePool(root: FiberRoot, renderLanes: Lanes) { | @@ -157,7 +233,6 @@ export function getSuspendedCachePool(): SpawnedCachePool | null { | 157 | 233 | if (!enableCache) { | 158 | 234 | return null; | 159 | 235 | } | 160 | | - | 161 | 236 | // We check the cache on the stack first, since that's the one any new Caches | 162 | 237 | // would have accessed. | 163 | 238 | let pool = pooledCache; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCacheComponent.old.js +80-5Lines changed: 80 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,8 +18,13 @@ import {REACT_CONTEXT_TYPE} from 'shared/ReactSymbols'; | 18 | 18 | import {isPrimaryRenderer} from './ReactFiberHostConfig'; | 19 | 19 | import {createCursor, push, pop} from './ReactFiberStack.old'; | 20 | 20 | import {pushProvider, popProvider} from './ReactFiberNewContext.old'; | | 21 | +import * as Scheduler from 'scheduler'; | 21 | 22 |
| 22 | | -export type Cache = Map<() => mixed, mixed>; | | 23 | +export type Cache = {| | | 24 | + controller: AbortController, | | 25 | + data: Map<() => mixed, mixed>, | | 26 | + refCount: number, | | 27 | +|}; | 23 | 28 |
| 24 | 29 | export type CacheComponentState = {| | 25 | 30 | +parent: Cache, | @@ -31,6 +36,13 @@ export type SpawnedCachePool = {| | 31 | 36 | +pool: Cache, | 32 | 37 | |}; | 33 | 38 |
| | 39 | +// Intentionally not named imports because Rollup would | | 40 | +// use dynamic dispatch for CommonJS interop named imports. | | 41 | +const { | | 42 | + unstable_scheduleCallback: scheduleCallback, | | 43 | + unstable_NormalPriority: NormalPriority, | | 44 | +} = Scheduler; | | 45 | + | 34 | 46 | export const CacheContext: ReactContext<Cache> = enableCache | 35 | 47 | ? { | 36 | 48 | $$typeof: REACT_CONTEXT_TYPE, | @@ -57,6 +69,58 @@ let pooledCache: Cache | null = null; | 57 | 69 | // cache from the render that suspended. | 58 | 70 | const prevFreshCacheOnStack: StackCursor<Cache | null> = createCursor(null); | 59 | 71 |
| | 72 | +// Creates a new empty Cache instance with a ref-count of 0. The caller is responsible | | 73 | +// for retaining the cache once it is in use (retainCache), and releasing the cache | | 74 | +// once it is no longer needed (releaseCache). | | 75 | +export function createCache(): Cache { | | 76 | + if (!enableCache) { | | 77 | + return (null: any); | | 78 | + } | | 79 | + const cache: Cache = { | | 80 | + controller: new AbortController(), | | 81 | + data: new Map(), | | 82 | + refCount: 0, | | 83 | + }; | | 84 | + | | 85 | + return cache; | | 86 | +} | | 87 | + | | 88 | +export function retainCache(cache: Cache) { | | 89 | + if (!enableCache) { | | 90 | + return; | | 91 | + } | | 92 | + if (__DEV__) { | | 93 | + if (cache.controller.signal.aborted) { | | 94 | + console.warn( | | 95 | + 'A cache instance was retained after it was already freed. ' + | | 96 | + 'This likely indicates a bug in React.', | | 97 | + ); | | 98 | + } | | 99 | + } | | 100 | + cache.refCount++; | | 101 | +} | | 102 | + | | 103 | +// Cleanup a cache instance, potentially freeing it if there are no more references | | 104 | +export function releaseCache(cache: Cache) { | | 105 | + if (!enableCache) { | | 106 | + return; | | 107 | + } | | 108 | + cache.refCount--; | | 109 | + if (__DEV__) { | | 110 | + if (cache.refCount < 0) { | | 111 | + console.warn( | | 112 | + 'A cache instance was released after it was already freed. ' + | | 113 | + 'This likely indicates a bug in React.', | | 114 | + ); | | 115 | + } | | 116 | + } | | 117 | + if (cache.refCount === 0) { | | 118 | + scheduleCallback(NormalPriority, () => { | | 119 | + cache.controller.abort(); | | 120 | + }); | | 121 | + } | | 122 | +} | | 123 | + | 60 | 124 | export function pushCacheProvider(workInProgress: Fiber, cache: Cache) { | 61 | 125 | if (!enableCache) { | 62 | 126 | return; | @@ -78,8 +142,14 @@ export function requestCacheFromPool(renderLanes: Lanes): Cache { | 78 | 142 | if (pooledCache !== null) { | 79 | 143 | return pooledCache; | 80 | 144 | } | 81 | | - // Create a fresh cache. | 82 | | - pooledCache = new Map(); | | 145 | + // Create a fresh cache. The pooled cache must be owned - it is freed | | 146 | + // in releaseRootPooledCache() - but the cache instance handed out | | 147 | + // is retained/released in the commit phase of the component that | | 148 | + // references is (ie the host root, cache boundary, suspense component) | | 149 | + // Ie, pooledCache is conceptually an Option<Arc<Cache>> (owned), | | 150 | + // whereas the return value of this function is a &Arc<Cache> (borrowed). | | 151 | + pooledCache = createCache(); | | 152 | + retainCache(pooledCache); | 83 | 153 | return pooledCache; | 84 | 154 | } | 85 | 155 |
| @@ -91,7 +161,13 @@ export function pushRootCachePool(root: FiberRoot) { | 91 | 161 | // from `root.pooledCache`. If it's currently `null`, we will lazily | 92 | 162 | // initialize it the first type it's requested. However, we only mutate | 93 | 163 | // the root itself during the complete/unwind phase of the HostRoot. | 94 | | - pooledCache = root.pooledCache; | | 164 | + const rootCache = root.pooledCache; | | 165 | + if (rootCache != null) { | | 166 | + pooledCache = rootCache; | | 167 | + root.pooledCache = null; | | 168 | + } else { | | 169 | + pooledCache = null; | | 170 | + } | 95 | 171 | } | 96 | 172 |
| 97 | 173 | export function popRootCachePool(root: FiberRoot, renderLanes: Lanes) { | @@ -157,7 +233,6 @@ export function getSuspendedCachePool(): SpawnedCachePool | null { | 157 | 233 | if (!enableCache) { | 158 | 234 | return null; | 159 | 235 | } | 160 | | - | 161 | 236 | // We check the cache on the stack first, since that's the one any new Caches | 162 | 237 | // would have accessed. | 163 | 238 | let pool = pooledCache; | |
|