Skip to content

🌲 fix: Bound Scraped Page Reference Matcher Memory - #739

Open
lia-by-librechat[bot] wants to merge 1 commit into
mainfrom
lia/search-content-compact-matcher
Open

lia-by-librechat[bot] wants to merge 1 commit into
mainfrom
lia/search-content-compact-matcher

Conversation

@lia-by-librechat

Copy link
Copy Markdown
Contributor

Stacked on #721. Closes its open P2 finding: #721 (comment)

Change

  • Storage. buildMatcher allocated an array per trie node and a map entry per edge. The automaton now lives in five typed arrays, 18 bytes per node, no object per node. Nodes are numbered breadth first from the sorted URLs, so a node's children are one sorted run (firstChild[node] to firstChild[node + 1]) found by binary search.
  • Cap. MAX_MATCHER_NODES = 2^21 (36 MiB). The exact node count comes from the sorted URLs before anything is allocated. Above the cap processContent returns the markdown unchanged for the whole page and still returns links, images and videos. No partial rewrite.
  • Output within the cap is unchanged: longest closed reference per opening parenthesis, left to right, parentheses in URLs, titles, image over link, numbering.

Measured

buildMatcher on 6,000 distinct 270-character URLs (1.62 MB, 1,482,688 nodes), Node 24.16, one run:

#721 head d7b136f this PR
Heap growth during build, before GC 541 MB 3 MB
Retained after GC 116 MB heap 25.5 MB typed arrays
RSS growth 504 MB 27 MB
Build time 1.4 s 0.08 s

At the cap: 2,095,524 nodes, 36.0 MiB, 0.15 s. The Wikipedia fixture needs 19,379 nodes.

processContent on the existing scenarios, best of 3: 2,000 lengths 458 → 314 ms, 6,000 links 227 → 139 ms, 200 suffix URLs 112 → 103 ms, Wikipedia 48 → 30 ms.

55,000 random pages (nested, suffix and prefix URLs, parentheses, quotes, titles, non-ASCII, all four types) gave identical output and node counts against d7b136f. That script is not committed.

Tests

src/tools/search/content.test.ts, existing tests unchanged:

  • 6,000 long non-shared URLs: exact node count, 18 * nodes + 4 buffer bytes, full rewrite.
  • Over the cap: markdown identical, links, images and videos still extracted.
  • Exactly MAX_MATCHER_NODES builds, one more does not.

Checks

  • npx jest src/tools/search: 16 suites, 276 tests passed
  • npx tsc --noEmit: 0 errors
  • npx eslint on the two files: 0 problems
  • node scripts/sort-imports.ts --check on the two files: sorted

Offered for @berry-13 to merge into #721 or cherry-pick.

@lia-by-librechat

Copy link
Copy Markdown
Contributor Author

Head be58a9a27b5608f7932cd2a1b7eb72e0f42c4334 on base d7b136f84a2db2ec3a38c5c7fc172c768222c4af (#721 head). One commit: the reference matcher in typed arrays (18 bytes per node), MAX_MATCHER_NODES with the unchanged-markdown fallback, and three tests in content.test.ts.

@danny-avila
danny-avila added this pull request to stack #740 November 23, 2026 16:02
Base automatically changed from berry-13/fix-search-content-links to main November 23, 2026 16:11
The reference matcher allocated an array per trie node and a map entry per
edge, so 6,000 distinct 270-character URLs (1.48 M nodes) grew the heap by
541 MB while building and kept 116 MB.

- Hold the automaton in typed arrays, 18 bytes per node: nodes are numbered
  breadth first from the sorted URLs, so a node's children are one sorted run
  found by binary search.
- Cap it at MAX_MATCHER_NODES (2^21, 36 MiB). The exact node count comes from
  the sorted URLs before anything is allocated; above the cap the markdown is
  returned unchanged and the references are still extracted.
@danny-avila
danny-avila force-pushed the lia/search-content-compact-matcher branch from be58a9a to 6c5e39c Compare November 23, 2026 16:11
@danny-avila

Copy link
Copy Markdown
Collaborator

@codex review the latest head, final review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T16:13:22.207358Z 6c5e39c Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 6c5e39c9c7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

This branch has not been deployed

No deployments
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.

2 participants