Unverified Commit d6ccb2b7 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Avoid microtaskiness when lazily fetching from cache handlers (#77843)

parent 32870862
...@@ -9,6 +9,7 @@ import type { CacheLife } from '../use-cache/cache-life' ...@@ -9,6 +9,7 @@ import type { CacheLife } from '../use-cache/cache-life'
// Share the instance module in the next-shared layer // Share the instance module in the next-shared layer
import { workAsyncStorageInstance } from './work-async-storage-instance' with { 'turbopack-transition': 'next-shared' } import { workAsyncStorageInstance } from './work-async-storage-instance' with { 'turbopack-transition': 'next-shared' }
import type { LazyResult } from '../lib/lazy-result'
export interface WorkStore { export interface WorkStore {
readonly isStaticGeneration: boolean readonly isStaticGeneration: boolean
...@@ -74,11 +75,11 @@ export interface WorkStore { ...@@ -74,11 +75,11 @@ export interface WorkStore {
readonly previouslyRevalidatedTags: readonly string[] readonly previouslyRevalidatedTags: readonly string[]
/** /**
* This map contains promise-like values so that we can evaluate them lazily * This map contains lazy results so that we can evaluate them when the first
* when a cache entry is read. It allows us to skip refreshing tags if no * cache entry is read. It allows us to skip refreshing tags if no caches are
* caches are read at all. * read at all.
*/ */
readonly refreshTagsByCacheKind: Map<string, PromiseLike<void>> readonly refreshTagsByCacheKind: Map<string, LazyResult<void>>
fetchMetrics?: FetchMetrics fetchMetrics?: FetchMetrics
......
...@@ -10,7 +10,7 @@ import type { CacheLife } from '../use-cache/cache-life' ...@@ -10,7 +10,7 @@ import type { CacheLife } from '../use-cache/cache-life'
import { AfterContext } from '../after/after-context' import { AfterContext } from '../after/after-context'
import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths' import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths'
import { createLazyResult } from '../lib/lazy-result' import { createLazyResult, type LazyResult } from '../lib/lazy-result'
import { getCacheHandlerEntries } from '../use-cache/handlers' import { getCacheHandlerEntries } from '../use-cache/handlers'
export type WorkStoreContext = { export type WorkStoreContext = {
...@@ -156,11 +156,11 @@ function createAfterContext(renderOpts: RequestLifecycleOpts): AfterContext { ...@@ -156,11 +156,11 @@ function createAfterContext(renderOpts: RequestLifecycleOpts): AfterContext {
} }
/** /**
* Creates a map with promise-like objects, that refresh tags for the given * Creates a map with lazy results that refresh tags for the respective cache
* cache kind when they're awaited for the first time. * kind when they're awaited for the first time.
*/ */
function createRefreshTagsByCacheKind(): Map<string, PromiseLike<void>> { function createRefreshTagsByCacheKind(): Map<string, LazyResult<void>> {
const refreshTagsByCacheKind = new Map<string, PromiseLike<void>>() const refreshTagsByCacheKind = new Map<string, LazyResult<void>>()
const cacheHandlers = getCacheHandlerEntries() const cacheHandlers = getCacheHandlerEntries()
if (cacheHandlers) { if (cacheHandlers) {
......
import { NEXT_CACHE_IMPLICIT_TAG_ID } from '../../lib/constants' import { NEXT_CACHE_IMPLICIT_TAG_ID } from '../../lib/constants'
import type { FallbackRouteParams } from '../request/fallback-params' import type { FallbackRouteParams } from '../request/fallback-params'
import { getCacheHandlers } from '../use-cache/handlers' import { getCacheHandlerEntries } from '../use-cache/handlers'
import { createLazyResult } from './lazy-result' import { createLazyResult, type LazyResult } from './lazy-result'
export interface ImplicitTags { export interface ImplicitTags {
/** /**
...@@ -9,14 +9,17 @@ export interface ImplicitTags { ...@@ -9,14 +9,17 @@ export interface ImplicitTags {
* handler in `get` calls. * handler in `get` calls.
*/ */
readonly tags: string[] readonly tags: string[]
/** /**
* Modern cache handlers don't receive implicit tags. Instead, the implicit * Modern cache handlers don't receive implicit tags. Instead, the implicit
* tags' expiration is stored in the work unit store, and used to compare with * tags' expirations are stored in the work unit store, and used to compare
* a cache entry's timestamp. Note: This is a promise-like value so that we * with a cache entry's timestamp.
* can evaluate it lazily when a cache entry is read. It allows us to skip *
* fetching the expiration value if no caches are read at all. * Note: This map contains lazy results so that we can evaluate them when the
* first cache entry is read. It allows us to skip fetching the expiration
* values if no caches are read at all.
*/ */
readonly expiration: PromiseLike<number> readonly expirationsByCacheKind: Map<string, LazyResult<number>>
} }
const getDerivedTags = (pathname: string): string[] => { const getDerivedTags = (pathname: string): string[] => {
...@@ -44,34 +47,28 @@ const getDerivedTags = (pathname: string): string[] => { ...@@ -44,34 +47,28 @@ const getDerivedTags = (pathname: string): string[] => {
return derivedTags return derivedTags
} }
async function getImplicitTagsExpiration(tags: string[]): Promise<number> { /**
// We're starting off with assuming that implicit tags are not expired, so we * Creates a map with lazy results that fetch the expiration value for the given
// use an artificial timestamp of 0. * tags and respective cache kind when they're awaited for the first time.
let expiration = 0 */
function createTagsExpirationsByCacheKind(
const cacheHandlers = getCacheHandlers() tags: string[]
): Map<string, LazyResult<number>> {
const expirationsByCacheKind = new Map<string, LazyResult<number>>()
const cacheHandlers = getCacheHandlerEntries()
if (cacheHandlers) { if (cacheHandlers) {
const expirations = await Promise.all( for (const [kind, cacheHandler] of cacheHandlers) {
[...cacheHandlers].map(async (handler) => { if ('getExpiration' in cacheHandler) {
if ('getExpiration' in handler) { expirationsByCacheKind.set(
return handler.getExpiration(...tags) kind,
} createLazyResult(async () => cacheHandler.getExpiration(...tags))
)
// Use 0 as fallback of legacy cache handlers. We don't need to track }
// the expiration of implicit tags for those, because they're passed }
// into the `get()` method and are checked internally by the cache
// handler.
return 0
})
)
// We use the most recent expiration from all cache handlers, i.e. the
// largest timestamp. Semantically, they should all be the same though.
expiration = Math.max(...expirations)
} }
return expiration return expirationsByCacheKind
} }
export async function getImplicitTags( export async function getImplicitTags(
...@@ -82,7 +79,6 @@ export async function getImplicitTags( ...@@ -82,7 +79,6 @@ export async function getImplicitTags(
}, },
fallbackRouteParams: null | FallbackRouteParams fallbackRouteParams: null | FallbackRouteParams
): Promise<ImplicitTags> { ): Promise<ImplicitTags> {
// TODO: Cache the result
const tags: string[] = [] const tags: string[] = []
const hasFallbackRouteParams = const hasFallbackRouteParams =
fallbackRouteParams && fallbackRouteParams.size > 0 fallbackRouteParams && fallbackRouteParams.size > 0
...@@ -101,9 +97,8 @@ export async function getImplicitTags( ...@@ -101,9 +97,8 @@ export async function getImplicitTags(
tags.push(tag) tags.push(tag)
} }
const expiration = createLazyResult(async () => return {
getImplicitTagsExpiration(tags) tags,
) expirationsByCacheKind: createTagsExpirationsByCacheKind(tags),
}
return { tags, expiration }
} }
export type LazyResult<TValue> = PromiseLike<TValue> & { value?: TValue }
export type ResolvedLazyResult<TValue> = PromiseLike<TValue> & { value: TValue }
/** /**
* Calls the given async function only when the returned promise-like object is * Calls the given async function only when the returned promise-like object is
* awaited. * awaited. Afterwards, it provides the resolved value synchronously as `value`
* property.
*/ */
export function createLazyResult<TResult>( export function createLazyResult<TValue>(
fn: () => Promise<TResult> fn: () => Promise<TValue>
): PromiseLike<TResult> { ): LazyResult<TValue> {
let pendingResult: Promise<TResult> | undefined let pendingResult: Promise<TValue> | undefined
return { const result: LazyResult<TValue> = {
then(onfulfilled, onrejected) { then(onfulfilled, onrejected) {
if (!pendingResult) { if (!pendingResult) {
pendingResult = fn() pendingResult = fn()
} }
pendingResult
.then((value) => {
result.value = value
})
.catch(() => {
// The externally awaited result will be rejected via `onrejected`. We
// don't need to handle it here. But we do want to avoid an unhandled
// rejection.
})
return pendingResult.then(onfulfilled, onrejected) return pendingResult.then(onfulfilled, onrejected)
}, },
} }
return result
}
export function isResolvedLazyResult<TValue>(
result: LazyResult<TValue>
): result is ResolvedLazyResult<TValue> {
return result.hasOwnProperty('value')
} }
...@@ -51,8 +51,7 @@ import { ...@@ -51,8 +51,7 @@ import {
} from '../request/search-params' } from '../request/search-params'
import type { Params } from '../request/params' import type { Params } from '../request/params'
import React from 'react' import React from 'react'
import type { ImplicitTags } from '../lib/implicit-tags' import { createLazyResult, isResolvedLazyResult } from '../lib/lazy-result'
import { createLazyResult } from '../lib/lazy-result'
type CacheKeyParts = type CacheKeyParts =
| [buildId: string, id: string, args: unknown[]] | [buildId: string, id: string, args: unknown[]]
...@@ -702,15 +701,13 @@ export function cache( ...@@ -702,15 +701,13 @@ export function cache(
cacheSignal.beginRead() cacheSignal.beginRead()
} }
const implicitTags = workUnitStore?.implicitTags const lazyRefreshTags = workStore.refreshTagsByCacheKind.get(kind)
const forceRevalidate = shouldForceRevalidate(workStore, workUnitStore)
// Lazily refresh the tags for the cache handler that's associated with if (lazyRefreshTags && !isResolvedLazyResult(lazyRefreshTags)) {
// this cache function. This is only done once per request and cache await lazyRefreshTags
// handler, when it's awaited for the first time. }
await workStore.refreshTagsByCacheKind.get(kind)
let entry = forceRevalidate let entry = shouldForceRevalidate(workStore, workUnitStore)
? undefined ? undefined
: 'getExpiration' in cacheHandler : 'getExpiration' in cacheHandler
? await cacheHandler.get(serializedCacheKey) ? await cacheHandler.get(serializedCacheKey)
...@@ -719,15 +716,37 @@ export function cache( ...@@ -719,15 +716,37 @@ export function cache(
// cache handlers (see below). // cache handlers (see below).
await cacheHandler.get( await cacheHandler.get(
serializedCacheKey, serializedCacheKey,
implicitTags?.tags ?? [] workUnitStore?.implicitTags?.tags ?? []
) )
if ( if (entry) {
entry && const implicitTags = workUnitStore?.implicitTags?.tags ?? []
(await shouldDiscardCacheEntry(entry, workStore, implicitTags)) let implicitTagsExpiration = 0
) {
debug?.('discarding stale entry', serializedCacheKey) if (workUnitStore?.implicitTags) {
entry = undefined const lazyExpiration =
workUnitStore.implicitTags.expirationsByCacheKind.get(kind)
if (lazyExpiration) {
if (isResolvedLazyResult(lazyExpiration)) {
implicitTagsExpiration = lazyExpiration.value
} else {
implicitTagsExpiration = await lazyExpiration
}
}
}
if (
shouldDiscardCacheEntry(
entry,
workStore,
implicitTags,
implicitTagsExpiration
)
) {
debug?.('discarding stale entry', serializedCacheKey)
entry = undefined
}
} }
const currentTime = performance.timeOrigin + performance.now() const currentTime = performance.timeOrigin + performance.now()
...@@ -951,38 +970,35 @@ function shouldForceRevalidate( ...@@ -951,38 +970,35 @@ function shouldForceRevalidate(
return false return false
} }
async function shouldDiscardCacheEntry( function shouldDiscardCacheEntry(
entry: CacheEntry, entry: CacheEntry,
workStore: WorkStore, workStore: WorkStore,
implicitTags: ImplicitTags | undefined implicitTags: string[],
): Promise<boolean> { implicitTagsExpiration: number
): boolean {
// If the cache entry contains revalidated tags that the cache handler might // If the cache entry contains revalidated tags that the cache handler might
// not know about yet, we need to discard it. // not know about yet, we need to discard it.
if (entry.tags.some((tag) => isRecentlyRevalidatedTag(tag, workStore))) { if (entry.tags.some((tag) => isRecentlyRevalidatedTag(tag, workStore))) {
return true return true
} }
if (implicitTags) { // If the cache entry was created before any of the implicit tags were
// If the cache entry was created before any of the implicit tags were // revalidated last, we also need to discard it.
// revalidated last, we also need to discard it. if (entry.timestamp <= implicitTagsExpiration) {
if (entry.timestamp <= (await implicitTags.expiration)) { debug?.(
debug?.( 'entry was created at',
'entry was created at', entry.timestamp,
entry.timestamp, 'before implicit tags were revalidated at',
'before implicit tags were revalidated at', implicitTagsExpiration
implicitTags.expiration )
)
return true return true
} }
// Finally, if any of the implicit tags have been revalidated recently, we // Finally, if any of the implicit tags have been revalidated recently, we
// also need to discard the cache entry. // also need to discard the cache entry.
if ( if (implicitTags.some((tag) => isRecentlyRevalidatedTag(tag, workStore))) {
implicitTags.tags.some((tag) => isRecentlyRevalidatedTag(tag, workStore)) return true
) {
return true
}
} }
return false return false
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment