perf(cache-storage): one keys() and two body copies per cache lookup (02-07)
A lookup listed the cache names twice and copied the body four times (read buffer, digest input, stored source, stream). The name list is now shared and only the read buffer and the per-stream copy remain; WebCrypto copies its input itself. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
d76653bfcd
commit
2cbf500fcc
@@ -26,7 +26,7 @@ import {
|
||||
*/
|
||||
|
||||
export type PublicCacheCryptoFacade = Readonly<{
|
||||
digestSha256(bytes: Uint8Array): Promise<ArrayBuffer>;
|
||||
digestSha256(bytes: Uint8Array<ArrayBuffer>): Promise<ArrayBuffer>;
|
||||
}>;
|
||||
|
||||
export type NormalizedAsset = Readonly<{
|
||||
@@ -619,7 +619,7 @@ export function asFailure(
|
||||
|
||||
export async function sha256Hex(
|
||||
crypto: PublicCacheCryptoFacade,
|
||||
bytes: Uint8Array,
|
||||
bytes: Uint8Array<ArrayBuffer>,
|
||||
): Promise<string> {
|
||||
const digest = await crypto.digestSha256(bytes);
|
||||
let hex = "";
|
||||
|
||||
@@ -106,7 +106,7 @@ export async function readBodyBounded(
|
||||
expectedByteLength: number,
|
||||
maxEntryBytes: number,
|
||||
signal: AbortSignal | undefined,
|
||||
): Promise<Uint8Array> {
|
||||
): Promise<Uint8Array<ArrayBuffer>> {
|
||||
if (expectedByteLength > maxEntryBytes) {
|
||||
throw new CacheValidationFailure("LIMIT_EXCEEDED");
|
||||
}
|
||||
|
||||
@@ -177,12 +177,11 @@ function snapshotCacheCrypto(crypto: Crypto): PublicCacheCryptoFacade {
|
||||
throw new TypeError("Public cache crypto capability is invalid.");
|
||||
}
|
||||
return Object.freeze({
|
||||
async digestSha256(bytes: Uint8Array): Promise<ArrayBuffer> {
|
||||
return await digest.call(
|
||||
subtle,
|
||||
"SHA-256",
|
||||
Uint8Array.from(bytes),
|
||||
);
|
||||
// WebCrypto copies its input when called (02-07), so no copy here.
|
||||
async digestSha256(
|
||||
bytes: Uint8Array<ArrayBuffer>,
|
||||
): Promise<ArrayBuffer> {
|
||||
return await digest.call(subtle, "SHA-256", bytes);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -242,9 +241,13 @@ export function createPublicResponseCacheAdapter(
|
||||
});
|
||||
|
||||
try {
|
||||
// 02-07. One keys() per lookup, shared by the pointer read and the
|
||||
// eviction check below.
|
||||
let names = await dependencies.cacheStorage.keys();
|
||||
const pointerResult = await readActivePointer(
|
||||
dependencies.cacheStorage,
|
||||
policy,
|
||||
names,
|
||||
);
|
||||
if (!pointerResult.ok) {
|
||||
return observeFailure(pointerResult, dependencies.observer);
|
||||
@@ -257,7 +260,11 @@ export function createPublicResponseCacheAdapter(
|
||||
return browserDataSuccess(null);
|
||||
}
|
||||
const pointer = pointerResult.value;
|
||||
const names = await dependencies.cacheStorage.keys();
|
||||
if (!names.includes(pointer.candidateCacheName)) {
|
||||
// The name list predates the pointer read, which an activation may
|
||||
// have moved to a newer cache; list again before reporting eviction.
|
||||
names = await dependencies.cacheStorage.keys();
|
||||
}
|
||||
if (!names.includes(pointer.candidateCacheName)) {
|
||||
return observeFailure(
|
||||
browserDataFailure("STORAGE_EVICTED", "CACHE_LOOKUP", {
|
||||
@@ -995,10 +1002,11 @@ async function readMarker(
|
||||
async function readActivePointer(
|
||||
cacheStorage: PublicCacheStorageFacade,
|
||||
policy: PublicCacheRuntimePolicy,
|
||||
names?: readonly string[],
|
||||
): Promise<BrowserDataResult<ActiveReleasePointer | null>> {
|
||||
try {
|
||||
const name = controlCacheName(policy);
|
||||
if (!(await cacheStorage.keys()).includes(name)) {
|
||||
if (!(names ?? (await cacheStorage.keys())).includes(name)) {
|
||||
return browserDataSuccess(null);
|
||||
}
|
||||
const cache = await cacheStorage.open(name);
|
||||
@@ -1016,8 +1024,12 @@ async function readActivePointer(
|
||||
}
|
||||
}
|
||||
|
||||
function byteSourceFrom(bytes: Uint8Array): ByteSource {
|
||||
const stored = Uint8Array.from(bytes);
|
||||
/**
|
||||
* 02-07. Keeps the verified read buffer, which nothing else references, and
|
||||
* gives each stream its own copy so a consumer that mutates a chunk cannot
|
||||
* change the body a later stream serves.
|
||||
*/
|
||||
function byteSourceFrom(stored: Uint8Array): ByteSource {
|
||||
return Object.freeze({
|
||||
byteLength: stored.byteLength,
|
||||
async *stream(signal: AbortSignal) {
|
||||
@@ -1058,7 +1070,7 @@ async function verifyReleaseCandidate(
|
||||
assets: readonly NormalizedAsset[],
|
||||
policy: PublicCacheRuntimePolicy,
|
||||
crypto: Readonly<{
|
||||
digestSha256(bytes: Uint8Array): Promise<ArrayBuffer>;
|
||||
digestSha256(bytes: Uint8Array<ArrayBuffer>): Promise<ArrayBuffer>;
|
||||
}>,
|
||||
signal: AbortSignal | undefined,
|
||||
): Promise<ReleaseCandidateVerdict> {
|
||||
|
||||
Reference in New Issue
Block a user