Skip to content

Commit 88af78c

Browse files
committed
perf: memoize sourceIds computation in ContentRenderer
Move the sourceIds computation from an inline prop expression (recomputed on every render) to a Svelte reactive variable that only recomputes when sources or model actually change. Also simplified the reduce+spread+filter deduplication pattern to a straightforward loop with Set-based deduplication.
1 parent ca2aaf0 commit 88af78c

1 file changed

Lines changed: 25 additions & 30 deletions

File tree

src/lib/components/chat/Messages/ContentRenderer.svelte

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@
4242
let contentContainerElement;
4343
let floatingButtonsElement;
4444
45+
// Memoize sourceIds — only recompute when sources or model change
46+
let computedSourceIds = [];
47+
$: {
48+
const result = [];
49+
for (const source of sources ?? []) {
50+
for (let index = 0; index < (source.document ?? []).length; index++) {
51+
if (model?.info?.meta?.capabilities?.citations == false) {
52+
result.push('N/A');
53+
continue;
54+
}
55+
const metadata = source.metadata?.[index];
56+
const id = metadata?.source ?? 'N/A';
57+
if (metadata?.name) {
58+
result.push(metadata.name);
59+
} else if (id.startsWith('http://') || id.startsWith('https://')) {
60+
result.push(id);
61+
} else {
62+
result.push(source?.source?.name ?? id);
63+
}
64+
}
65+
}
66+
computedSourceIds = [...new Set(result)];
67+
}
68+
4569
const updateButtonPosition = (event) => {
4670
const buttonsContainerElement = document.getElementById(`floating-buttons-${id}`);
4771
if (
@@ -142,36 +166,7 @@
142166
{done}
143167
{editCodeBlock}
144168
{topPadding}
145-
sourceIds={(sources ?? []).reduce((acc, source) => {
146-
let ids = [];
147-
source.document.forEach((document, index) => {
148-
if (model?.info?.meta?.capabilities?.citations == false) {
149-
ids.push('N/A');
150-
return ids;
151-
}
152-
153-
const metadata = source.metadata?.[index];
154-
const id = metadata?.source ?? 'N/A';
155-
156-
if (metadata?.name) {
157-
ids.push(metadata.name);
158-
return ids;
159-
}
160-
161-
if (id.startsWith('http://') || id.startsWith('https://')) {
162-
ids.push(id);
163-
} else {
164-
ids.push(source?.source?.name ?? id);
165-
}
166-
167-
return ids;
168-
});
169-
170-
acc = [...acc, ...ids];
171-
172-
// remove duplicates
173-
return acc.filter((item, index) => acc.indexOf(item) === index);
174-
}, [])}
169+
sourceIds={computedSourceIds}
175170
{onSourceClick}
176171
{onTaskClick}
177172
{onSave}

0 commit comments

Comments
 (0)