From 2cbf500fcc0ccda0f76df9a8292c4fcb923499a5 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Sat, 26 Sep 2026 00:26:17 +0900 Subject: [PATCH] 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 --- .../cache-storage/public-cache-codec.ts | 4 +- .../public-cache-response-validation.ts | 2 +- .../public-response-cache-adapter.ts | 34 +++++--- tests/unit/public-response-cache.test.ts | 77 ++++++++++++++++++- 4 files changed, 102 insertions(+), 15 deletions(-) diff --git a/src/adapters/cache-storage/public-cache-codec.ts b/src/adapters/cache-storage/public-cache-codec.ts index 6482c0f3..000cf490 100644 --- a/src/adapters/cache-storage/public-cache-codec.ts +++ b/src/adapters/cache-storage/public-cache-codec.ts @@ -26,7 +26,7 @@ import { */ export type PublicCacheCryptoFacade = Readonly<{ - digestSha256(bytes: Uint8Array): Promise; + digestSha256(bytes: Uint8Array): Promise; }>; export type NormalizedAsset = Readonly<{ @@ -619,7 +619,7 @@ export function asFailure( export async function sha256Hex( crypto: PublicCacheCryptoFacade, - bytes: Uint8Array, + bytes: Uint8Array, ): Promise { const digest = await crypto.digestSha256(bytes); let hex = ""; diff --git a/src/adapters/cache-storage/public-cache-response-validation.ts b/src/adapters/cache-storage/public-cache-response-validation.ts index 383081c0..a8bdccc3 100644 --- a/src/adapters/cache-storage/public-cache-response-validation.ts +++ b/src/adapters/cache-storage/public-cache-response-validation.ts @@ -106,7 +106,7 @@ export async function readBodyBounded( expectedByteLength: number, maxEntryBytes: number, signal: AbortSignal | undefined, -): Promise { +): Promise> { if (expectedByteLength > maxEntryBytes) { throw new CacheValidationFailure("LIMIT_EXCEEDED"); } diff --git a/src/adapters/cache-storage/public-response-cache-adapter.ts b/src/adapters/cache-storage/public-response-cache-adapter.ts index 743ee6c2..380228af 100644 --- a/src/adapters/cache-storage/public-response-cache-adapter.ts +++ b/src/adapters/cache-storage/public-response-cache-adapter.ts @@ -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 { - 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, + ): Promise { + 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> { 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; + digestSha256(bytes: Uint8Array): Promise; }>, signal: AbortSignal | undefined, ): Promise { diff --git a/tests/unit/public-response-cache.test.ts b/tests/unit/public-response-cache.test.ts index 7204eba6..78c462db 100644 --- a/tests/unit/public-response-cache.test.ts +++ b/tests/unit/public-response-cache.test.ts @@ -1,9 +1,10 @@ -import { describe, expect, it } from "vitest"; +import { describe, expect, it, vi } from "vitest"; import type { PublicCacheAsset, PublicCacheReleaseManifest, } from "../../src/application/ports/browser-file-storage/cache-storage-ports.ts"; +import type { ByteSource } from "../../src/application/ports/browser-file-storage/shared.ts"; import { assertPublicCachePolicy, createDefaultPublicCachePolicy, @@ -1517,6 +1518,80 @@ describe("public response Cache Storage adapter", () => { expect(await cacheStorage.keys()).toEqual([]); }); + it("spends one keys() and at most two body copies per lookup (02-07)", async () => { + const policy = createDefaultPublicCachePolicy( + "https://assets.example.test", + ); + const bytes = new Uint8Array([6, 6, 6, 6, 6]); + const asset: PublicCacheAsset = { + absoluteUrl: "https://assets.example.test/cost.js", + expectedByteLength: bytes.byteLength, + expectedContentType: "application/javascript", + integrity: { + algorithm: "SHA-256", + digestHex: await digestHex(bytes), + }, + }; + const cacheStorage = new MemoryCacheStorage(); + let keysCalls = 0; + const listKeys = cacheStorage.keys.bind(cacheStorage); + cacheStorage.keys = async () => { + keysCalls += 1; + return await listKeys(); + }; + const adapter = createPublicResponseCacheAdapter({ + cacheStorage: cacheStorage as unknown as CacheStorage, + crypto: globalThis.crypto, + mutationLock: immediateLock, + policy, + fetcher: async () => + new Response(Uint8Array.from(bytes), { + headers: { + "cache-control": "public", + "content-type": "application/javascript", + }, + }), + }); + const manifest = await manifestFor("cost-release", [asset], policy); + expect(await adapter.admin.stageRelease(manifest)).toMatchObject({ + ok: true, + }); + expect( + await adapter.admin.activateRelease( + manifest.releaseRegistryId, + manifest.manifestDigestHex, + ), + ).toMatchObject({ ok: true }); + const read = async (body: ByteSource): Promise => { + const chunks: Uint8Array[] = []; + for await (const chunk of body.stream(new AbortController().signal)) { + if (chunk.ok) chunks.push(chunk.value); + } + return chunks; + }; + + keysCalls = 0; + const copy = vi.spyOn(Uint8Array, "from"); + const match = await adapter.responses.matchActiveExact({ + absoluteUrl: asset.absoluteUrl, + }); + if (!match.ok || !match.value) throw new Error("lookup"); + const [chunk] = await read(match.value.body); + const bodyCopies = copy.mock.calls.filter( + ([source]) => + source instanceof Uint8Array && + source.byteLength === bytes.byteLength, + ).length; + copy.mockRestore(); + + expect(keysCalls).toBe(1); + // The bounded read buffer plus one copy for the stream consumer. + expect(bodyCopies).toBe(1); + // That copy is the consumer's own: changing it leaves the body intact. + chunk!.fill(0); + expect(await read(match.value.body)).toEqual([bytes]); + }); + it("reuses a verified release marker across lookups (Q-6.2-6)", async () => { const policy = createDefaultPublicCachePolicy( "https://assets.example.test",