diff --git a/.jules/bolt.md b/.jules/bolt.md index d54cf10fc..3678af523 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -61,3 +61,7 @@ ## 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-15 - Replace Array.every for large byte arrays +**Learning:** Using `Array.prototype.every()` on very large arrays (like serialized PDF byte buffers) introduces massive function call overhead for each element, leading to severe main-thread blocking. +**Action:** Use a standard `for` loop with an early return to check large byte arrays, avoiding O(N) callback allocations. diff --git a/apps/desktop/src/features/score/scoreStorage.test.ts b/apps/desktop/src/features/score/scoreStorage.test.ts index 0feec199e..f58e94dc0 100644 --- a/apps/desktop/src/features/score/scoreStorage.test.ts +++ b/apps/desktop/src/features/score/scoreStorage.test.ts @@ -32,4 +32,21 @@ describe("scoreStorage bridge resolution", () => { BRIDGE_UNAVAILABLE_MESSAGE ); }); + + it("correctly reads a score pdf returning a number array", async () => { + const tauriWindow = window as TauriWindow; + tauriWindow.__TAURI_INVOKE__ = vi.fn().mockResolvedValue([1, 2, 3]); + + const result = await readScorePdf("project-1", "score-1"); + expect(result).toEqual(new Uint8Array([1, 2, 3])); + }); + + it("rejects reading a score pdf when returning an array with non-numbers", async () => { + const tauriWindow = window as TauriWindow; + tauriWindow.__TAURI_INVOKE__ = vi.fn().mockResolvedValue([1, 2, "not a number"]); + + 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..7f938fc02 100644 --- a/apps/desktop/src/features/score/scoreStorage.ts +++ b/apps/desktop/src/features/score/scoreStorage.ts @@ -91,8 +91,21 @@ 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)) { + // Performance: Avoid Array.every() and Uint8Array.from() to prevent O(N) callback overhead + // and intermediate array allocations when parsing potentially large PDF byte arrays. + const uint8Arr = new Uint8Array(response.length); + let allNumbers = true; + for (let i = 0; i < response.length; i++) { + if (typeof response[i] !== "number") { + allNumbers = false; + break; + } + uint8Arr[i] = response[i] as number; + } + if (allNumbers) { + return uint8Arr; + } } throw new Error(INVALID_RESPONSE_MESSAGE);