diff --git a/.jules/bolt.md b/.jules/bolt.md index d54cf10fc..e9018ad96 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -61,3 +61,8 @@ ## 2026-07-13 - Array.from mapping optimization **Learning:** Using `Array.from({ length: N }).map(...)` creates an intermediate array of `undefined` values which requires memory allocation and garbage collection, adding O(N) unnecessary overhead in frequently re-rendered UI components. **Action:** Use `Array.from({ length: N }, (_, index) => ...)` to map elements directly during array creation, avoiding intermediate allocations. + +## 2026-07-20 - Avoid Array.every() for large byte array validation + +**Learning:** Validating large payloads over the IPC bridge with `.every()` creates significant O(N) intermediate callback allocations and Garbage Collection overhead on the critical path. +**Action:** Replace `.every()` with a standard `for...of` loop with an early `break` for O(1) memory and substantially faster checks for malformed payload chunks. diff --git a/apps/desktop/src/features/score/scoreStorage.test.ts b/apps/desktop/src/features/score/scoreStorage.test.ts index 0feec199e..123c654eb 100644 --- a/apps/desktop/src/features/score/scoreStorage.test.ts +++ b/apps/desktop/src/features/score/scoreStorage.test.ts @@ -32,4 +32,18 @@ describe("scoreStorage bridge resolution", () => { BRIDGE_UNAVAILABLE_MESSAGE ); }); + + it("fails when the response array contains non-number elements", async () => { + const tauriWindow = window as TauriWindow; + tauriWindow.__TAURI_INVOKE__ = async (command: string) => { + if (command === "read_score_pdf") { + return [1, 2, "not a number", 4]; + } + return null; + }; + + await expect(readScorePdf("project-1", "score-1")).rejects.toThrow( + "Invalid score bridge response" + ); + }); }); diff --git a/apps/desktop/src/features/score/scoreStorage.ts b/apps/desktop/src/features/score/scoreStorage.ts index 492f12591..c5feba9a8 100644 --- a/apps/desktop/src/features/score/scoreStorage.ts +++ b/apps/desktop/src/features/score/scoreStorage.ts @@ -91,8 +91,17 @@ export async function readScorePdf(projectId: string, scoreId: string): Promise< if (response instanceof ArrayBuffer) { return new Uint8Array(response); } - if (Array.isArray(response) && response.every((byte) => typeof byte === "number")) { - return Uint8Array.from(response as number[]); + if (Array.isArray(response)) { + let isValid = true; + for (const byte of response) { + if (typeof byte !== "number") { + isValid = false; + break; + } + } + if (isValid) { + return Uint8Array.from(response as number[]); + } } throw new Error(INVALID_RESPONSE_MESSAGE);