perf(og): stream-parse <head> instead of buffering full 2 MB body - #1611
Open
Sravanjangam wants to merge 1 commit into
Open
perf(og): stream-parse <head> instead of buffering full 2 MB body#1611Sravanjangam wants to merge 1 commit into
Sravanjangam wants to merge 1 commit into
Conversation
readBoundedText buffered the entire response up to MAX_HTML_BYTES (2 MB) just to regex-scan it for OG meta tags that live in the first ~30 KB. At link-preview scale the peak RSS per request was ~6-8 MB and the first byte of any preview waited on a full-body fetch. readHeadText reads chunks into a small growing buffer, scans for </head> after each chunk, and cancels the reader as soon as it's seen. The cap drops to 64 KB; OG tags live entirely within that bound in practice. The 6 regex patterns in processHtml are unchanged and now run over a ~30 KB string instead of 2 MB. Fixes supermemoryai#1610.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1610.
Problem
apps/web/app/api/og/route.tsruns a regex-driven OG scraper on a 2 MB buffer when only the document<head>(~30 KB in practice) is actually needed.readBoundedTextalready streams the response, but it buffers every chunk into an array, merges them into a singleUint8Array, and thenTextDecoders the whole thing before returning. The function's own comment says "OG parsing only needs<head>, so cap the read rather than buffering the whole body" — but the implementation does the opposite.How it effects
Per
/api/ogrequest on the current code:chunks: Uint8Array[]+ 2 MB merged buffer + 2–4 MB UTF-16 string afterTextDecoder).</head>is discarded.At link-preview scale (a chat app unfurling 20 URLs in one message) peak RSS in the worker approaches 150–200 MB and the slowest fetch gates every other preview. The 2 MB cap is a correctness cap that doubles as the performance baseline, so making it a head-only cap of 64 KB is a free win.
Solution
Replace
readBoundedTextwithreadHeadText:</head>(case-insensitive) after every chunk.</head>is seen.null(→ existing 413 path) if the cap is hit before</head>closes.The 6 regex patterns in
processHtmlare unchanged — they just run over a 30–40 KB string instead of 2 MB, which is the actual win. The two call sites (main response and the single-level-redirect path) were updated to use the new function name; their error handling and status codes are unchanged.Tests
Apps/web has no test runner wired in (per recent maintainer preference), so the verification is a manual smoke harness run with
bun:Cases cover the happy path, realistic chunked delivery, mid-tag chunk boundaries, case-insensitive matching, the cap-exceeded path, and the empty-body edge case. The harness is in
/tmp/prs/og-head-smoke.mjs(not part of this PR) and can be re-run any time.Environment
extractImageUrlunused-function warning onmainis unchanged — baseline parity, not a regression from this diff)tsc --noEmitclean for the touched filefix/og-streaming-head-only, commitb95eebe2680e