Skip to content

perf(documents): bound delimited table expansion by output budget - #420

Open
Afloat16 wants to merge 1 commit into
simplexai-labs:mainfrom
Afloat16:afloat/delimited-output-budget
Open

Afloat16 wants to merge 1 commit into
simplexai-labs:mainfrom
Afloat16:afloat/delimited-output-budget

Conversation

@Afloat16

@Afloat16 Afloat16 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Check a lower bound on the delimited table size before padding sparse rows, then render one row at a time while counting canonical UTF-8 output bytes. This prevents a small sparse CSV or TSV from allocating its complete dense cell matrix before discovering that the Markdown result exceeds the caller's budget.

The exact byte count includes the final newline and NFC normalization. Existing Markdown escaping, padding and accepted output are preserved.

How it was verified

  • Base: 77abdcb. Node 26.5.0, pnpm 11.7.0.

  • Same-condition local benchmark: 6,999-byte sparse inputs, 32 KiB output budget; 3 warmups, 7 samples of 3 calls, GC before each sample, median milliseconds per call.

  • 1,000 columns × 1,000 rows: 213.007 ms before, 0.240 ms after.

  • 4,000 columns × 500 rows: 414.846 ms before, 0.253 ms after.

  • Both versions return DOCUMENT_MARKDOWN_TOO_LARGE. These measurements exercise a custom output budget and do not measure end-to-end upload latency.

  • 50 focused table-budget, literal-table and normalizer tests pass, including exact NFC/multibyte byte limits and the final newline.

  • Node-services typecheck, changed-file ESLint and diff checks pass. The full owning-app fast gate now passes; see the exact-head validation below.

  • Command: pnpm --filter @linger/node-services exec vitest run tests/document-normalizer/document-table-budget.test.ts tests/document-normalizer/document-table-literals.test.ts tests/document-normalizer/document-normalizer.test.ts --maxWorkers=1.

  • Focused tests for the changed behavior

  • make check BASE=origin/<target-branch> or the fast checks of every touched app

  • No contract regeneration required

Boundary

The nearest normalizer documentation describes bounded expansion. Input parsing remains bounded by the existing source-size limit; this change specifically bounds generated table construction. No dependency, network request or file walk is added.

Validation follow-up

On 3395075c98f9ddc88d15f152f574b3f6887c4072, pnpm --filter @linger/node-services check passes with Node 26.5.0 and pnpm 11.7.0: the feature catalog/typecheck and full ESLint gates pass, followed by 269 test files passed, 8 skipped; 2,715 tests passed, 51 skipped. No additional test exclusions, source changes, or lockfile changes were needed.

This establishes the owning app's full fast gate. Unrelated full-repository and optional integration/acceptance suites were not run. The previously recorded performance measurements remain the benchmark evidence; this validation did not rerun those benchmarks.

@Afloat16
Afloat16 marked this pull request as ready for review November 25, 2026 06:11

@linger-review linger-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve (0 findings)

Scope: base main, merge base 77abdcb3; 1 commit and 3 files reviewed at head 3395075c. I loaded the coderbook (sections 2, 3, 5), ponytail-review (sections 1–3) and karpathy-guidelines (sections 2–3) skills. This review ran in its own session.

Findings

None.

Verified as fine

  • The lower bound never rejects a table that fits. For width columns, a rendered row is at least 3*width + 1 characters plus its newline: "| ", separators " | ", " |". That is more than width, so (rows.length + 1) * width > maxOutputBytes (document-normalizer-core.ts:222) only rejects tables that the old code would also reject at canonicalizeMarkdown (document-normalizer-core.ts:143-146).
  • The per-row byte count matches the final canonical size exactly.
    • escapeMarkdownTableCell turns every \r\n, \r and \n in a cell into a space (document-office-extract.ts:67-68), so the CRLF rewrite in canonicalizeMarkdown has nothing to change.
    • Every line starts and ends with | (a stable starter for NFC), so NFC applied line by line equals NFC applied to the joined text.
    • TextDecoder already strips a leading BOM (decodeUtf8Text).
    • The last line ends in |, so +1 per line equals the single \n that canonicalizeMarkdown appends after it trims trailing newlines.
    • So the early exit and the final check agree byte for byte, and the boundary test (budget and budget - 1) covers this.
  • Accepted output is unchanged. Cells are still padded to width and escaped the same way, the separator row comes right after the header, and the lines are joined with \n as before. canonicalizeMarkdown still runs last and is still the authoritative size check (document-normalizer-core.ts:130). The escaping/padding test pins the exact old output.
  • Callers are unaffected. delimitedToMarkdown is private. Archive entries call normalizeDocumentContent without a budget, so they use the 32 MiB default (document-normalizer.ts:9), exactly as before. The worker and the in-process path (document-normalizer-worker.ts:39, document-normalizer.ts:216) keep their signatures.
  • Memory is bounded at the production budget. Once the lower bound passes, rows * width is at most the budget. The per-line check stops before lines grows past the budget.
  • Docs and conventions. The README in src/document-normalizer/ now describes the bounded expansion. There are no new dependencies and no env or contract changes. Added lines and the commit message contain no CJK characters.
  • Test limitation (not a defect). The sparse-table test also returns DOCUMENT_MARKDOWN_TOO_LARGE on the base commit; the gain is in time and allocation, not in the result. That is reasonable for a perf change where a timing assertion would be flaky.

Process notes

Single commit; no merge commits or fixup commits.

CI

  • The checks that ran all passed: repository-gate, changes and check (web). Every other app job was skipped by the path plan.
  • Per .github/workflows/ci.yml, the node-services suite does not run in CI, so I ran it locally:
    • vitest run tests/document-normalizer/document-table-budget.test.ts tests/document-normalizer/document-table-literals.test.ts tests/document-normalizer/document-normalizer.test.ts: 3 files, 50 tests passed.
    • tsc --noEmit for apps/node-services: passed.
  • I did not re-run the full pnpm --filter @linger/node-services check. The PR body reports it passing on this head.

Automated review by Claude Code


Automated review of 3395075 by Claude Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant