From a5d45fad4ded005ffe788b27c728101b0c0b2a8c Mon Sep 17 00:00:00 2001 From: Roman Claudiu Date: Tue, 24 Nov 2026 14:37:33 +0200 Subject: [PATCH 1/2] fix: index the bundle inventory so bundling scales linearly 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 #442 --- lib/bundle.ts | 32 +++++++++++++------ .../bundle-regressions/many-refs.spec.ts | 32 +++++++++++++++++++ 2 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 test/specs/bundle-regressions/many-refs.spec.ts diff --git a/lib/bundle.ts b/lib/bundle.ts index 4d4e214e..e9043cca 100644 --- a/lib/bundle.ts +++ b/lib/bundle.ts @@ -308,7 +308,7 @@ function inventory$Ref } } - inventory.push({ + addToInventory(inventory, { $ref, // The JSON Reference (e.g. {$ref: string}) parent: $refParent, // The object that contains this $ref pointer key: $refKey, // The key in `parent` that is the $ref pointer @@ -634,20 +634,34 @@ function preserveRecursiveTargets( } /** - * TODO + * Index of the inventory, keyed by `$ref` parent and then by key, so that an already + * inventoried $ref can be found without scanning the whole inventory. */ -function findInInventory(inventory: InventoryEntry[], $refParent: any, $refKey: any) { - for (const existingEntry of inventory) { - if (existingEntry && existingEntry.parent === $refParent && existingEntry.key === $refKey) { - return existingEntry; - } +const inventoryIndexes = new WeakMap>>(); + +function addToInventory(inventory: InventoryEntry[], entry: InventoryEntry) { + let index = inventoryIndexes.get(inventory); + if (!index) { + index = new Map(); + inventoryIndexes.set(inventory, index); + } + let keys = index.get(entry.parent); + if (!keys) { + keys = new Map(); + index.set(entry.parent, keys); } - return undefined; + keys.set(entry.key, entry); + inventory.push(entry); +} + +function findInInventory(inventory: InventoryEntry[], $refParent: any, $refKey: any) { + return inventoryIndexes.get(inventory)?.get($refParent)?.get($refKey); } -function removeFromInventory(inventory: InventoryEntry[], entry: any) { +function removeFromInventory(inventory: InventoryEntry[], entry: InventoryEntry) { const index = inventory.indexOf(entry); inventory.splice(index, 1); + inventoryIndexes.get(inventory)?.get(entry.parent)?.delete(entry.key); } /** diff --git a/test/specs/bundle-regressions/many-refs.spec.ts b/test/specs/bundle-regressions/many-refs.spec.ts new file mode 100644 index 00000000..9a3fcdfd --- /dev/null +++ b/test/specs/bundle-regressions/many-refs.spec.ts @@ -0,0 +1,32 @@ +import { describe, expect, it } from "vitest"; +import $RefParser from "../../../lib/index.js"; + +// Every $ref visited while bundling used to trigger a linear scan of the inventory +// (see issue #442), so bundling time grew quadratically with the number of $refs. +describe("bundle() with many $refs", () => { + const count = 60000; + + function makeSchema(n: number) { + const definitions: Record = {}; + for (let i = 0; i < n; i++) { + definitions["d" + i] = { type: "string" }; + } + return { + items: Array.from({ length: n }, (_, i) => ({ $ref: "#/definitions/d" + i })), + definitions, + }; + } + + it("stays roughly linear in the number of $refs", async () => { + const schema = makeSchema(count); + const start = performance.now(); + const bundled = (await $RefParser.bundle(schema)) as any; + const elapsed = performance.now() - start; + + expect(bundled.items).toHaveLength(count); + expect(bundled.items[0]).toEqual({ $ref: "#/definitions/d0" }); + expect(bundled.items[count - 1]).toEqual({ $ref: "#/definitions/d" + (count - 1) }); + // The quadratic scan takes several seconds for this input; a linear pass takes well under one. + expect(elapsed).toBeLessThan(3000); + }, 60000); +}); From 9b73ccd606c37384e5a797e1c54422f496706a3a Mon Sep 17 00:00:00 2001 From: JonLuca DeCaro Date: Tue, 24 Nov 2026 12:10:50 -0600 Subject: [PATCH 2/2] test: move bundle timing out of the required suite --- test/benchmarks/bundle-many-refs.mjs | 18 +++++++++ .../bundle-regressions/many-refs.spec.ts | 37 +++++-------------- 2 files changed, 28 insertions(+), 27 deletions(-) create mode 100644 test/benchmarks/bundle-many-refs.mjs diff --git a/test/benchmarks/bundle-many-refs.mjs b/test/benchmarks/bundle-many-refs.mjs new file mode 100644 index 00000000..bcbe1372 --- /dev/null +++ b/test/benchmarks/bundle-many-refs.mjs @@ -0,0 +1,18 @@ +// Run pnpm build, then node test/benchmarks/bundle-many-refs.mjs. +import $RefParser from "../../dist/lib/index.js"; + +for (const count of [8000, 16000, 32000]) { + const keys = Array.from({ length: count }, (_, i) => "d" + i); + const definitions = Object.fromEntries(keys.map((key) => [key, { type: "string" }])); + const schema = { definitions, items: keys.map((key) => ({ $ref: "#/definitions/" + key })) }; + + await $RefParser.bundle(schema); + const samples = []; + for (let i = 0; i < 3; i++) { + const start = performance.now(); + await $RefParser.bundle(schema); + samples.push(performance.now() - start); + } + const median = samples.sort((a, b) => a - b)[1]; + console.log(`${count} internal references: ${median.toFixed(1)} ms (median of 3 runs)`); +} diff --git a/test/specs/bundle-regressions/many-refs.spec.ts b/test/specs/bundle-regressions/many-refs.spec.ts index 9a3fcdfd..a0af05e9 100644 --- a/test/specs/bundle-regressions/many-refs.spec.ts +++ b/test/specs/bundle-regressions/many-refs.spec.ts @@ -1,32 +1,15 @@ -import { describe, expect, it } from "vitest"; +import { expect, it } from "vitest"; import $RefParser from "../../../lib/index.js"; -// Every $ref visited while bundling used to trigger a linear scan of the inventory -// (see issue #442), so bundling time grew quadratically with the number of $refs. -describe("bundle() with many $refs", () => { - const count = 60000; +it("remaps every reference in a large array to its own definition", async () => { + const keys = Array.from({ length: 2048 }, (_, i) => "d" + i); + const schema = { + definitions: Object.fromEntries(keys.map((key) => [key, { type: "string" }])), + alias: { $ref: "#/definitions" }, + items: keys.map((key) => ({ $ref: "#/alias/" + key })), + }; - function makeSchema(n: number) { - const definitions: Record = {}; - for (let i = 0; i < n; i++) { - definitions["d" + i] = { type: "string" }; - } - return { - items: Array.from({ length: n }, (_, i) => ({ $ref: "#/definitions/d" + i })), - definitions, - }; - } + const bundled = await $RefParser.bundle(schema); - it("stays roughly linear in the number of $refs", async () => { - const schema = makeSchema(count); - const start = performance.now(); - const bundled = (await $RefParser.bundle(schema)) as any; - const elapsed = performance.now() - start; - - expect(bundled.items).toHaveLength(count); - expect(bundled.items[0]).toEqual({ $ref: "#/definitions/d0" }); - expect(bundled.items[count - 1]).toEqual({ $ref: "#/definitions/d" + (count - 1) }); - // The quadratic scan takes several seconds for this input; a linear pass takes well under one. - expect(elapsed).toBeLessThan(3000); - }, 60000); + expect(bundled.items).toEqual(keys.map((key) => ({ $ref: "#/definitions/" + key }))); });