Skip to content

Commit

Permalink
Reuse active cache
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Sep 4, 2024
1 parent f140f36 commit 35507c5
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,10 @@ if (!__EXPERIMENTAL__) {
gate(flags => flags.enableOwnerStacks)
? [
__DEV__
? '\n' +
//
' in Indirection (at **)\n' +
' in App (at **)'
? '\n in Indirection (at **)' +
'\n in App (at **)' +
'\n in Preview (at **)' +
'\n in PreviewApp (at **)'
: null,
]
: [],
Expand Down
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactInternalTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ export interface AsyncCache {
}

export type AsyncDispatcher = {
getActiveCache: () => AsyncCache,
getActiveCache: () => AsyncCache | null,
// DEV-only (or !disableStringRefs)
getOwner: () => null | Fiber | ReactComponentInfo | ComponentStackNode,
};
2 changes: 1 addition & 1 deletion packages/react-server/src/ReactFizzAsyncDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {disableStringRefs} from 'shared/ReactFeatureFlags';

import {currentTaskInDEV} from './ReactFizzCurrentTask';

function getActiveCache(): AsyncCache {
function getActiveCache(): AsyncCache | null {
throw new Error('Not implemented.');
}

Expand Down
22 changes: 20 additions & 2 deletions packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import type {Chunk, BinaryChunk, Destination} from './ReactServerStreamConfig';

import type {Postpone} from 'react/src/ReactPostpone';
import type {AsyncCache} from 'react-reconciler/src/ReactInternalTypes';

import type {TemporaryReferenceSet} from './ReactFlightServerTemporaryReferences';

import {
disableStringRefs,
enableBinaryFlight,
enablePostpone,
enableHalt,
Expand Down Expand Up @@ -395,8 +397,6 @@ const {
TaintRegistryPendingRequests,
} = ReactSharedInternals;

ReactSharedInternals.A = DefaultAsyncDispatcher;

function throwTaintViolation(message: string) {
// eslint-disable-next-line react-internal/prod-error-codes
throw new Error(message);
Expand Down Expand Up @@ -450,6 +450,24 @@ function RequestInstance(
onAllReady: () => void,
onFatalError: (error: mixed) => void,
) {
const previousAsyncDispatcher = ReactSharedInternals.A;

if (previousAsyncDispatcher !== null) {
const previousActiveCache = previousAsyncDispatcher.getActiveCache();
if (previousActiveCache !== null) {
ReactSharedInternals.A = ({
getActiveCache(): AsyncCache | null {
return previousActiveCache;
},
}: any);
if (__DEV__ || !disableStringRefs) {
ReactSharedInternals.A.getOwner = DefaultAsyncDispatcher.getOwner;
}
}
} else {
ReactSharedInternals.A = DefaultAsyncDispatcher;
}

if (__DEV__) {
// Unlike Fizz or Fiber, we don't reset this and just keep it on permanently.
// This lets it act more like the AsyncDispatcher so that we can get the
Expand Down
41 changes: 2 additions & 39 deletions packages/react-server/src/flight/ReactFlightAsyncDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,17 @@ import type {
} from 'react-reconciler/src/ReactInternalTypes';

import {resolveRequest, getCache} from '../ReactFlightServer';
import ReactSharedInternals from '../ReactSharedInternalsServer';

import {disableStringRefs} from 'shared/ReactFeatureFlags';

import {resolveOwner} from './ReactFlightCurrentOwner';

const previousAsyncDispatcher = ReactSharedInternals.A;

function resolveCache(): Map<Function, mixed> {
function getActiveCache(): AsyncCache | null {
const request = resolveRequest();
if (request) {
return getCache(request);
}
return new Map();
}

function getActiveCache(): AsyncCache {
const outerCache: AsyncCache | null =
previousAsyncDispatcher !== null
? previousAsyncDispatcher.getActiveCache()
: null;

const innerCache = resolveCache();

if (outerCache === null) {
return innerCache;
}

// If both caches are active, reads will prefer the outer cache
// Writes will go into both caches.
const chainedCache: AsyncCache = {
get(resourceType: Function) {
const outerEntry = outerCache.get(resourceType);
if (outerEntry !== undefined) {
return outerEntry;
}
return innerCache.get(resourceType);
},
set(resourceType: Function, value: mixed) {
if (outerCache !== null) {
outerCache.set(resourceType, value);
}
innerCache.set(resourceType, value);
return chainedCache;
},
};

return chainedCache;
return null;
}

export const DefaultAsyncDispatcher: AsyncDispatcher = ({
Expand Down
13 changes: 8 additions & 5 deletions packages/react/src/ReactCacheImpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ export function cache<A: Iterable<mixed>, T>(fn: (...A) => T): (...A) => T {
return fn.apply(null, arguments);
}
const activeCache = dispatcher.getActiveCache();
let fnMap: WeakMap<any, CacheNode<T>> | void = (activeCache.get(
createCacheRoot,
): any);
let fnMap: WeakMap<any, CacheNode<T>> | void =
activeCache !== null
? (activeCache.get(createCacheRoot): any)
: undefined;
if (fnMap === undefined) {
fnMap = createCacheRoot();
// TODO: Warn if undefined?
activeCache.set(createCacheRoot, fnMap);
if (activeCache !== null) {
// TODO: Warn if undefined?
activeCache.set(createCacheRoot, fnMap);
}
}
const fnNode = fnMap.get(fn);
let cacheNode: CacheNode<T>;
Expand Down
9 changes: 6 additions & 3 deletions packages/react/src/ReactHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ export function getCacheForType<T>(resourceType: () => T): T {
return resourceType();
}
const activeCache = dispatcher.getActiveCache();
let entry: T | void = (activeCache.get(resourceType): any);
let entry: T | void =
activeCache !== null ? (activeCache.get(resourceType): any) : undefined;
if (entry === undefined) {
entry = resourceType();
// TODO: Warn if undefined?
activeCache.set(resourceType, entry);
if (activeCache !== null) {
// TODO: Warn if undefined?
activeCache.set(resourceType, entry);
}
}
return entry;
}
Expand Down

0 comments on commit 35507c5

Please sign in to comment.