Skip to content

fix: index the bundle inventory so bundling scales linearly - #443

Merged
jonluca merged 2 commits into
APIDevTools:mainfrom
RClaudiuM:fix/442-bundle-inventory-index
Nov 24, 2026
Merged

jonluca merged 2 commits into
APIDevTools:mainfrom
RClaudiuM:fix/442-bundle-inventory-index

Conversation

@RClaudiuM

Copy link
Copy Markdown
Contributor

Fixes #442

What was wrong

inventory$Ref calls findInInventory for every $ref it visits, and that function walked the flat inventory array from the start looking for an entry with the same parent and key. With N refs that is about N^2/2 comparisons, so bundle() got disproportionately slow on schemas with many $refs while dereference() stayed linear.

What changed

  • lib/bundle.ts: entries are now added through addToInventory, which also records them in a parent -> key -> entry index (kept in a WeakMap keyed by the inventory array, so the crawl/inventory$Ref signatures do not change). findInInventory is a map lookup, and removeFromInventory also drops the index entry.
  • Entries are still pushed to and spliced out of the array exactly as before, so the inventory order that remap sorts from is unchanged.

How it was tested

New test/specs/bundle-regressions/many-refs.spec.ts bundles a schema with 60000 internal $refs, checks the output refs and asserts it finishes in under 3 seconds.

  • Without the fix: fails (expected 7396 to be less than 3000).
  • With the fix: passes in about 700 ms.

The time bound is deliberately loose (about 4x headroom over the fixed run, 10x under the old one) to keep it from being flaky.

Also run: pnpm exec vitest run (744 passed, 6 skipped), pnpm lint, pnpm typecheck, oxfmt --check on the changed files.

RClaudiuM and others added 2 commits November 24, 2026 14:37
findInInventory scanned the whole inventory array for every $ref visited, so bundle() cost grew quadratically with the number of $refs. Keep a parent -> key index next to the array and look entries up in it. Entries are still pushed and removed in the same order, so the inventory ordering is unchanged.

Fixes APIDevTools#442
@jonluca
jonluca merged commit 29bbc92 into APIDevTools:main Nov 24, 2026
13 checks passed
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 45456706179

Coverage increased (+0.05%) to 90.145%

Details

  • Coverage increased (+0.05%) from the base build.
  • Patch coverage: 1 uncovered change across 1 file (13 of 14 lines covered, 92.86%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
lib/bundle.ts 14 13 92.86%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 1985
Covered Lines: 1831
Line Coverage: 92.24%
Relevant Branches: 1729
Covered Branches: 1517
Branch Coverage: 87.74%
Branches in Coverage %: Yes
Coverage Strength: 187214.29 hits per line

💛 - Coveralls

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.

bundle() time grows quadratically with the number of $refs

3 participants