fix(server): optimize transcript body construction for performance - #8109
fix(server): optimize transcript body construction for performance#8109bankosegger wants to merge 2 commits into
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
ApprovabilityVerdict: Approved at Macroscope's review found this PR approvable — This is a focused performance refactor that replaces repeated O(n²) transcript assembly with running length accounting while preserving the existing output construction and selection flow. It touches only one file and introduces no schema, feature, security, or deployment changes. You can add or adjust custom eligibility rules. Learn more. |
What Changed
PERFORMANCE
Rewrote
buildBootstrapInputinapps/web/src/historyBootstrap.tsto find the largest fitting suffix of transcript blocks using running length totals instead of rebuilding the array and rejoining the full string on every iteration.Why
The original loop was O(n²): each of the n messages triggered a full array copy, reverse, and string join just to check the candidate length against the budget. On a long thread (thousands of messages), this scaled badly, measured ~33s for a 20,000-message history vs ~4ms after the fix. Since joined-string length doesn't depend on block order, the fitting count can be computed with O(1) work per block, then the actual string is built once at the end. Verified against the original implementation with 20,000 randomized trials (varying block count/content, prompt length, and budget, including edge cases), output is byte-identical in every case.
UI Changes
N/A pure logic change, no UI surface currently calls this function.
Checklist
Note
Low Risk
Internal string-budget algorithm only; intended byte-identical output with no API or security surface change.
Overview
buildBootstrapInputinhistoryBootstrap.tsnow picks how many newest transcript blocks fit under the char budget without rebuilding arrays and rejoining the full transcript on every block.The inclusion loop precomputes
fixedLength(preamble, headers, latest prompt) and tracksjoinedLength/includedCountso each step only adds block length and omitted-summary length to the running total. After the loop, itslices once to get the included blocks; the existingwhilepath still builds the final transcript string and callsfinalizeWithPrompt.Same fitting logic as before (joined length is order-independent for the count step), aimed at avoiding O(n²) work on very long histories.
Reviewed by Cursor Bugbot for commit 7579aad. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Optimize
buildBootstrapInputtranscript body construction to avoid O(n²) array rebuildsReworks the block-inclusion loop in historyBootstrap.ts to precompute constant header and prompt sizes (
fixedLength) and use a runningjoinedLengthaccumulator withincludedCountto determine how many newest-first blocks fit within the budget. This replaces repeated array rebuilds and full-string joins on each iteration with a single slice ofnewestFirstBlocksafter the count is computed. The subsequent transcript body construction loop remains unchanged.Macroscope summarized 7579aad.