perf: reduce CPU usage in event replay, projections, and sidebar #3825
Closedt3dotgg(GitHub profile, opens in a new tab)wants to merge 4 commits into
main fromt3code/performance-fixesopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/web/src/components/ChatMarkdown.tsx line 295OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -281,9 +284,12 @@281284 langs: [language as SupportedLanguages],282285 preferredHighlighter: "shiki-js",283286 }).catch((err) => {284- highlighterPromiseCache.delete(language);285287 if (language === "text") {286288 // "text" itself failed — Shiki cannot initialize at all, surface the error289+ highlighterPromiseCache.delete(language);287290 throw err;288291 }289- // Language not supported by Shiki — fall back to "text"292+ // Language not supported by Shiki — remember that and fall back to "text",293+ // keeping the resolved promise cached so the grammar load isn't retried on294+ // every render of a block with that language.295+ fallbackHighlightLanguages.add(language);apps/web/src/components/Sidebar.tsx lines 263 to 263OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -248,3 +249,15 @@248249 return null;249250 }250251252+// Self-contained so hover changes re-render only this component, never the253+// sidebar tree. Delegated `pointerover` avoids per-row handler props.254+function SidebarHoverThreadPrewarmer() {255+ const [prewarmThreadKey, setPrewarmThreadKey] = useState<string | null>(null);256+257+ useEffect(() => {258+ const controller = createSidebarHoverPrewarmController({259+ delayMs: SIDEBAR_THREAD_HOVER_PREWARM_DELAY_MS,260+ onPrewarmTargetChange: setPrewarmThreadKey,261+ });262+ const onPointerOver = (event: globalThis.PointerEvent) => {263+ const target = event.target instanceof HTMLElement ? event.target : null;apps/server/src/orchestration/Layers/ProjectionPipeline.ts line 761Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -716,0 +740,21 @@740+ const latestUserMessageAt =741+ event.payload.role === "user" &&742+ (existingRow.value.latestUserMessageAt === null ||743+ event.payload.createdAt > existingRow.value.latestUserMessageAt)744+ ? event.payload.createdAt745+ : existingRow.value.latestUserMessageAt;746+ yield* projectionThreadRepository.upsert({747+ ...existingRow.value,748+ latestUserMessageAt,749+ // Streaming assistant chunks arrive many times per second and750+ // change no shell-visible field; leaving updatedAt untouched lets751+ // the shell stream skip per-chunk upsert fan-out (toShellStreamEvent).752+ updatedAt:753+ event.payload.streaming && event.payload.role === "assistant"754+ ? existingRow.value.updatedAt755+ : event.occurredAt,756+ });757+ return;758+ }759+760+ case "thread.activity-appended": {
t3dotgg(GitHub profile, opens in a new tab) changed the title
- apps/web/src/components/Sidebar.tsx line 270Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -248,3 +249,19 @@248249 return null;249250 }250251252+// Self-contained so hover changes re-render only this component, never the253+// sidebar tree. Delegated `pointerover` avoids per-row handler props.254+function SidebarHoverThreadPrewarmer() {255+ const [prewarmThreadKey, setPrewarmThreadKey] = useState<string | null>(null);256+257+ useEffect(() => {258+ const controller = createSidebarHoverPrewarmController({259+ delayMs: SIDEBAR_THREAD_HOVER_PREWARM_DELAY_MS,260+ onPrewarmTargetChange: setPrewarmThreadKey,261+ });262+ const onPointerOver = (event: globalThis.PointerEvent) => {263+ const target = event.target instanceof HTMLElement ? event.target : null;264+ controller.hover(265+ target?.closest("[data-thread-prewarm-key]")?.getAttribute("data-thread-prewarm-key") ??266+ null,267+ );apps/web/src/components/ChatMarkdown.tsx line 703OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -684,12 +690,14 @@684690 }: UncachedShikiCodeBlockProps) {685691 const highlighter = use(getHighlighterPromise(language));686692 const highlightedHtml = useMemo(() => {693+ const effectiveLanguage = fallbackHighlightLanguages.has(language) ? "text" : language;687694 try {688- return highlighter.codeToHtml(code, { lang: language, theme: themeName });695+ return highlighter.codeToHtml(code, { lang: effectiveLanguage, theme: themeName });689696 } catch (error) {690697 // Log highlighting failures for debugging while falling back to plain text691698 console.warn(692699 `Code highlighting failed for language "${language}", falling back to plain text.`,693700 error instanceof Error ? error.message : error,694701 );695- // If highlighting fails for this language, render as plain text702+ // Remember the failure so subsequent renders skip the throwing path.703+ fallbackHighlightLanguages.add(language);
t3dotgg(GitHub profile, opens in a new tab) changed the title
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID:
f245e5af-6999-463f-850c-e9751674655dYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment `@coderabbitai help` to get the list of available commands.