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:
DongHyeonka
2026-09-26 00:26:17 +09:00
co-authored by Claude Opus 5.5
parent d76653bfcd
commit 2cbf500fcc
4 changed files with 102 additions and 15 deletions
@@ -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> {
+76 -1
View File
@@ -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<Uint8Array[]> => {
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",