{{ message }}
perf(pdf-server): share cache across server instances and dedupe form parsing#637
Open
perf(pdf-server): share cache across server instances and dedupe form parsing#637
Conversation
… parsing In stateless HTTP deployments createServer() is called per request, so the per-instance pdfCache and the 4 MB viewer HTML were discarded after every call. Hoist both to module scope. Also refactor extractFormSchema/extractFormFieldInfo to accept an already-parsed PDFDocumentProxy so display_pdf downloads and parses the PDF once instead of twice.
The module-level sharedPdfCache could grow unbounded within the 60s lifetime window under a burst of distinct URLs. Track running total bytes and evict least-recently-used entries on insert when it would exceed CACHE_MAX_TOTAL_BYTES (256MB). getCacheEntry now bumps the accessed entry to the end of insertion order so eviction targets the LRU entry rather than the oldest insert. createPdfCache takes an optional maxTotalBytes for testability.
fd26573 to
4702eb6
Compare
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.

Summary
Three perf fixes for the pdf-server when run behind a stateless HTTP transport (where
createServer()is invoked on every request):sharedPdfCache—createPdfCache()is now called once at module load and reused by every server instance, instead of being created (and immediately discarded) per request. This letsread_pdf_byteschunk reads hit the cache across requests, avoiding repeated full-body downloads when the upstream origin doesn't honor Range.mcp-app.html— the ~4 MB viewer HTML is read from disk once (cachedAppHtml ??= ...) instead of on every resource read.display_pdf—extractFormSchemaandextractFormFieldInfonow accept an already-parsedPDFDocumentProxy.display_pdffetches the bytes once, callspdfjs.getDocument()once, passes the doc to both extractors, anddestroy()s it in afinally. Previously each extractor independently downloaded the full PDF and parsed it.Behavior note
Both extractors now run inside a single try block, so if schema extraction throws, field-info extraction is also skipped (previously each had its own try/catch). Risk is low: the only throw point inside
extractFormSchema(getFieldObjects) is already caught internally and returns an empty schema rather than propagating.Tests
bun test examples/pdf-server— 177 pass, 0 fail, 1 pre-existing skip. Build (tsc + vite + bun bundle) and prettier check both clean.