Skip to content

Commit 649c219

Browse files
committed
[cache] Use WeakMap by default
Allows us to get rid of an indiretion in the Client implementation.
1 parent f140f36 commit 649c219

5 files changed

Lines changed: 11 additions & 20 deletions

File tree

packages/react-reconciler/src/ReactFiberCacheComponent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function createCache(): Cache {
9090
}
9191
const cache: Cache = {
9292
controller: new AbortControllerLocal(),
93-
data: new Map(),
93+
data: new WeakMap(),
9494
refCount: 0,
9595
};
9696

packages/react-reconciler/src/ReactInternalTypes.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,9 @@ export type Dispatcher = {
451451
) => [Awaited<S>, (P) => void, boolean],
452452
};
453453

454+
/**
455+
* resourceType is weakly held i.e. the underlying implementation must be a WeakMap.
456+
*/
454457
export interface AsyncCache {
455458
get(resourceType: Function): mixed;
456459
set(resourceType: Function, value: mixed): AsyncCache;

packages/react-server/src/ReactFlightServer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ export type Request = {
359359
fatalError: mixed,
360360
destination: null | Destination,
361361
bundlerConfig: ClientManifest,
362-
cache: Map<Function, mixed>,
362+
cache: WeakMap<Function, mixed>,
363363
nextChunkId: number,
364364
pendingChunks: number,
365365
hints: Hints,
@@ -470,7 +470,7 @@ function RequestInstance(
470470
this.fatalError = null;
471471
this.destination = null;
472472
this.bundlerConfig = bundlerConfig;
473-
this.cache = new Map();
473+
this.cache = new WeakMap();
474474
this.nextChunkId = 0;
475475
this.pendingChunks = 0;
476476
this.hints = hints;
@@ -989,7 +989,7 @@ export function getHints(request: Request): Hints {
989989
return request.hints;
990990
}
991991

992-
export function getCache(request: Request): Map<Function, mixed> {
992+
export function getCache(request: Request): WeakMap<Function, mixed> {
993993
return request.cache;
994994
}
995995

packages/react-server/src/flight/ReactFlightAsyncDispatcher.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ import {resolveOwner} from './ReactFlightCurrentOwner';
2323

2424
const previousAsyncDispatcher = ReactSharedInternals.A;
2525

26-
function resolveCache(): Map<Function, mixed> {
26+
function resolveCache(): WeakMap<Function, mixed> {
2727
const request = resolveRequest();
2828
if (request) {
2929
return getCache(request);
3030
}
31-
return new Map();
31+
return new WeakMap();
3232
}
3333

3434
function getActiveCache(): AsyncCache {

packages/react/src/ReactCacheImpl.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ type CacheNode<T> =
3939
| UnterminatedCacheNode<T>
4040
| ErroredCacheNode<T>;
4141

42-
function createCacheRoot<T>(): WeakMap<Function | Object, CacheNode<T>> {
43-
return new WeakMap();
44-
}
45-
4642
function createCacheNode<T>(): CacheNode<T> {
4743
return {
4844
s: UNTERMINATED, // status, represents whether the cached computation returned a value or threw an error
@@ -61,19 +57,11 @@ export function cache<A: Iterable<mixed>, T>(fn: (...A) => T): (...A) => T {
6157
return fn.apply(null, arguments);
6258
}
6359
const activeCache = dispatcher.getActiveCache();
64-
let fnMap: WeakMap<any, CacheNode<T>> | void = (activeCache.get(
65-
createCacheRoot,
66-
): any);
67-
if (fnMap === undefined) {
68-
fnMap = createCacheRoot();
69-
// TODO: Warn if undefined?
70-
activeCache.set(createCacheRoot, fnMap);
71-
}
72-
const fnNode = fnMap.get(fn);
60+
const fnNode: CacheNode<T> | void = (activeCache.get(fn): any);
7361
let cacheNode: CacheNode<T>;
7462
if (fnNode === undefined) {
7563
cacheNode = createCacheNode();
76-
fnMap.set(fn, cacheNode);
64+
activeCache.set(fn, cacheNode);
7765
} else {
7866
cacheNode = fnNode;
7967
}

0 commit comments

Comments
 (0)