Repository navigation
fix: index the bundle inventory so bundling scales linearly - #443
Merged
jonluca merged 2 commits intoNov 24, 2026
Merged
Conversation
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
Coverage Report for CI Build 45456706179Coverage increased (+0.05%) to 90.145%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats💛 - Coveralls |
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 #442
What was wrong
inventory$RefcallsfindInInventoryfor every$refit visits, and that function walked the flatinventoryarray from the start looking for an entry with the same parent and key. With N refs that is about N^2/2 comparisons, sobundle()got disproportionately slow on schemas with many$refs whiledereference()stayed linear.What changed
lib/bundle.ts: entries are now added throughaddToInventory, which also records them in aparent -> key -> entryindex (kept in aWeakMapkeyed by the inventory array, so thecrawl/inventory$Refsignatures do not change).findInInventoryis a map lookup, andremoveFromInventoryalso drops the index entry.remapsorts from is unchanged.How it was tested
New
test/specs/bundle-regressions/many-refs.spec.tsbundles a schema with 60000 internal$refs, checks the output refs and asserts it finishes in under 3 seconds.expected 7396 to be less than 3000).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 --checkon the changed files.