Skip to content

Commit e9485df

Browse files
only21milcodexmaria-rcks
authored
perf(composer): reuse persisted image bytes when attachments change (pingdotgg#22340)
Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 12d1e4c commit e9485df

3 files changed

Lines changed: 139 additions & 26 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ import { DEFAULT_COMPOSER_PLACEHOLDER } from "../../composerPlaceholder";
7979
import {
8080
deriveComposerSendState,
8181
getAntigravitySendBlockReason,
82-
readFileAsDataUrl,
8382
resolveComposerInteractionMode,
8483
threadShellHasStarted,
8584
} from "../ChatView.logic";
8685
import {
8786
dataTransferHasComposerMention,
8887
makeComposerMentionDragHandlers,
8988
} from "./composerMentionDrag";
89+
import { serializeComposerImages } from "./composerImagePersistence";
9090
import {
9191
isInsideCollapsedComposerControls,
9292
isInsideRestingComposerControlScope,
@@ -96,7 +96,6 @@ import {
9696
type ComposerFileAttachment,
9797
type ComposerImageAttachment,
9898
type DraftId,
99-
type PersistedComposerImageAttachment,
10099
composerFileNeedsReattach,
101100
composerDraftHasUserContent,
102101
composerTargetKey,
@@ -1522,31 +1521,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
15221521
getComposerDraft(attachmentDraftTarget)?.persistedAttachments ?? [];
15231522
try {
15241523
const currentPersistedAttachments = getPersistedAttachmentsForThread();
1525-
const existingPersistedById = new Map(
1526-
currentPersistedAttachments.map((attachment) => [attachment.id, attachment]),
1524+
const serialized = await serializeComposerImages(
1525+
composerImages,
1526+
currentPersistedAttachments,
15271527
);
1528-
const stagedAttachmentById = new Map<string, PersistedComposerImageAttachment>();
1529-
await Promise.all(
1530-
composerImages.map(async (image) => {
1531-
try {
1532-
const dataUrl = await readFileAsDataUrl(image.file);
1533-
stagedAttachmentById.set(image.id, {
1534-
id: image.id,
1535-
name: image.name,
1536-
mimeType: image.mimeType,
1537-
sizeBytes: image.sizeBytes,
1538-
dataUrl,
1539-
...(image.source ? { source: image.source } : {}),
1540-
});
1541-
} catch {
1542-
const existingPersisted = existingPersistedById.get(image.id);
1543-
if (existingPersisted) {
1544-
stagedAttachmentById.set(image.id, existingPersisted);
1545-
}
1546-
}
1547-
}),
1548-
);
1549-
const serialized = Array.from(stagedAttachmentById.values());
15501528
if (cancelled) return;
15511529
syncComposerDraftPersistedAttachments(attachmentDraftTarget, serialized);
15521530
} catch {
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
2+
3+
import type { ComposerImageAttachment } from "../../composerDraftStore";
4+
import { readFileAsDataUrl } from "../../lib/imageCompression";
5+
import { serializeComposerImages } from "./composerImagePersistence";
6+
7+
vi.mock("../../lib/imageCompression", () => ({ readFileAsDataUrl: vi.fn() }));
8+
9+
function image(id: string): ComposerImageAttachment {
10+
const file = new File([id], `${id}.png`, { type: "image/png" });
11+
return {
12+
type: "image",
13+
id,
14+
name: file.name,
15+
mimeType: file.type,
16+
sizeBytes: file.size,
17+
previewUrl: `blob:${id}`,
18+
file,
19+
};
20+
}
21+
22+
describe("serializeComposerImages", () => {
23+
beforeEach(() => {
24+
vi.mocked(readFileAsDataUrl).mockReset();
25+
});
26+
27+
it("reads only new images and drops removed images without rereading retained bytes", async () => {
28+
const a = image("a");
29+
const b = image("b");
30+
vi.mocked(readFileAsDataUrl).mockImplementation(async (file) =>
31+
file === a.file ? "data:image/png;base64,YQ==" : "data:image/png;base64,Yg==",
32+
);
33+
34+
const first = await serializeComposerImages([a], []);
35+
expect(first).toEqual([
36+
{
37+
id: "a",
38+
name: "a.png",
39+
mimeType: "image/png",
40+
sizeBytes: 1,
41+
dataUrl: "data:image/png;base64,YQ==",
42+
},
43+
]);
44+
expect(readFileAsDataUrl).toHaveBeenCalledTimes(1);
45+
46+
const second = await serializeComposerImages([a, b], first);
47+
expect(second).toEqual([
48+
first[0],
49+
{
50+
id: "b",
51+
name: "b.png",
52+
mimeType: "image/png",
53+
sizeBytes: 1,
54+
dataUrl: "data:image/png;base64,Yg==",
55+
},
56+
]);
57+
expect(readFileAsDataUrl).toHaveBeenCalledTimes(2);
58+
expect(readFileAsDataUrl).toHaveBeenNthCalledWith(2, b.file);
59+
60+
expect(await serializeComposerImages([a], second)).toEqual(first);
61+
expect(await serializeComposerImages([], second)).toEqual([]);
62+
expect(readFileAsDataUrl).toHaveBeenCalledTimes(2);
63+
});
64+
65+
it("preserves cached bytes without trying to read an unavailable file", async () => {
66+
const a = image("a");
67+
const persisted = {
68+
id: a.id,
69+
name: a.name,
70+
mimeType: a.mimeType,
71+
sizeBytes: a.sizeBytes,
72+
dataUrl: "data:image/png;base64,YQ==",
73+
};
74+
vi.mocked(readFileAsDataUrl).mockRejectedValue(new Error("File is unavailable"));
75+
76+
expect(await serializeComposerImages([a], [persisted])).toEqual([persisted]);
77+
expect(readFileAsDataUrl).not.toHaveBeenCalled();
78+
});
79+
80+
it("omits failed uncached reads while keeping successfully serialized images", async () => {
81+
const a = image("a");
82+
const b = image("b");
83+
vi.mocked(readFileAsDataUrl)
84+
.mockRejectedValueOnce(new Error("File is unavailable"))
85+
.mockResolvedValueOnce("data:image/png;base64,Yg==");
86+
87+
expect(await serializeComposerImages([a, b], [])).toEqual([
88+
{
89+
id: "b",
90+
name: "b.png",
91+
mimeType: "image/png",
92+
sizeBytes: 1,
93+
dataUrl: "data:image/png;base64,Yg==",
94+
},
95+
]);
96+
expect(readFileAsDataUrl).toHaveBeenCalledTimes(2);
97+
});
98+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type {
2+
ComposerImageAttachment,
3+
PersistedComposerImageAttachment,
4+
} from "../../composerDraftStore";
5+
import { readFileAsDataUrl } from "../../lib/imageCompression";
6+
7+
export async function serializeComposerImages(
8+
images: ReadonlyArray<ComposerImageAttachment>,
9+
persistedAttachments: ReadonlyArray<PersistedComposerImageAttachment>,
10+
): Promise<PersistedComposerImageAttachment[]> {
11+
const existingPersistedById = new Map(
12+
persistedAttachments.map((attachment) => [attachment.id, attachment]),
13+
);
14+
const stagedAttachmentById = new Map<string, PersistedComposerImageAttachment>();
15+
await Promise.all(
16+
images.map(async (image) => {
17+
try {
18+
const dataUrl =
19+
existingPersistedById.get(image.id)?.dataUrl ?? (await readFileAsDataUrl(image.file));
20+
stagedAttachmentById.set(image.id, {
21+
id: image.id,
22+
name: image.name,
23+
mimeType: image.mimeType,
24+
sizeBytes: image.sizeBytes,
25+
dataUrl,
26+
...(image.source ? { source: image.source } : {}),
27+
});
28+
} catch {
29+
const existingPersisted = existingPersistedById.get(image.id);
30+
if (existingPersisted) {
31+
stagedAttachmentById.set(image.id, existingPersisted);
32+
}
33+
}
34+
}),
35+
);
36+
return Array.from(stagedAttachmentById.values());
37+
}

0 commit comments

Comments
 (0)