perf(registry): memoize semver version resolution - #1568
Open
ricardo-devis-agullo wants to merge 1 commit into
Open
perf(registry): memoize semver version resolution#1568ricardo-devis-agullo wants to merge 1 commit into
ricardo-devis-agullo wants to merge 1 commit into
Conversation
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.
Problem: repository.getComponent (domain/repository.ts:305) calls versionHandler.getAvailableVersion on every render; unversioned requests re-run semver.maxSatisfying over all versions and ranged requests re-match everything.
Fix: memoize resolved versions in a module-level WeakMap keyed by the versions-array reference, each entry a Map<requestedVersion, resolvedVersion> (misses/undefined cached too). The registry replaces (never mutates) versions arrays on publish/poll refresh, so the array reference is the invalidation token: new snapshots re-resolve, stale entries are GC'd with the old array, no manual invalidation. Falsy requests (undefined, '') share one entry since they take the same branch. Fail-open: non-array versions input bypasses the cache and delegates to the real resolver; cache read/write is guarded so it can never break resolution. Touches only domain/version-handler.ts (repository.ts needs no change; routes/index.ts, middleware/*, compression/adapter files untouched).
Verify: new spec test/unit/registry-domain-version-handler-cache.js covers range semantics (exact, range, unversioned-max, prerelease-only, mixed, unsatisfiable), proves via sinon callThrough stubs that semver.maxSatisfying is called once per (array, requested) pair (incl. negative caching) and re-resolves on a new array reference, and checks fail-open (frozen arrays, nullish requests, exotic versions input). Full packages/oc suite (test-silent): 1110 passing, 0 failing.