From 9e412a0b9fb958599e35345207e7d0d01180b6e8 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 15 Dec 2025 03:05:37 +0000 Subject: [PATCH] perf: Remove unused O(N^2) topic lookup in TranscriptViewer render loop - Removes `getSegmentTopic` function which performed nested iterations over topics and segments. - Removes the unused call to this function inside the transcript render loop. - This eliminates an O(N * T * S) complexity overhead during every render of the transcript list (where N=transcript segments, T=topics, S=segments in topic). --- .jules/bolt.md | 3 +++ components/transcript-viewer.tsx | 15 --------------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index e69de29..f563a15 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2025-05-23 - Removed Dead O(N*M) Loop in Render Cycle +**Learning:** React render loops are critical paths. Calling a function that performs nested iterations (like finding a topic for a segment) inside `array.map()` for every item creates a multiplicative performance cost (O(N*M)). If the result is unused, it's pure waste. +**Action:** Always verify if the return value of a calculation inside a render loop is actually used. If not, delete it. diff --git a/components/transcript-viewer.tsx b/components/transcript-viewer.tsx index 59ba877..ee9a95c 100644 --- a/components/transcript-viewer.tsx +++ b/components/transcript-viewer.tsx @@ -340,20 +340,6 @@ export function TranscriptViewer({ } }, [handleUserScroll]); - const getSegmentTopic = (segment: TranscriptSegment): { topic: Topic; index: number } | null => { - for (let i = 0; i < topics.length; i++) { - const topic = topics[i]; - const hasSegment = topic.segments.some( - (topicSeg) => segment.start >= topicSeg.start && segment.start < topicSeg.end - ); - if (hasSegment) { - return { topic, index: i }; - } - } - return null; - }; - - const getHighlightedText = (segment: TranscriptSegment, segmentIndex: number): { highlightedParts: Array<{ text: string; highlighted: boolean; isCitation?: boolean; isSearchMatch?: boolean; isCurrentSearchMatch?: boolean }> } | null => { // Priority: Search > Citation/Topic @@ -826,7 +812,6 @@ export function TranscriptViewer({ return transcript.map((segment, index) => { const highlightedText = getHighlightedText(segment, index); const isCurrent = index === currentSegmentIndex; - getSegmentTopic(segment); const hasHighlight = highlightedText !== null; const translation = translationsCache.get(index);