Repository navigation
perf: build era wire schemas lazily through memoized factories - #2476
Merged
Merged
Conversation
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.
Importing the client or server package currently constructs both frozen protocol-revision wire-schema graphs (2025-11-25 and 2026-07-28) at module evaluation, before any message is validated. This PR moves each revision's schema definitions into a
buildSchemas()factory memoized at module level, so the construction cost is paid on first validation instead of at import.Motivation and Context
A consumer that imports
@modelcontextprotocol/clientor@modelcontextprotocol/serverpays for building ~150 zod schemas per protocol revision, twice over (both revisions), per bundled copy — all before it has connected to anything. For short-lived CLI-style consumers this is pure startup overhead on every invocation.The fix:
buildSchemas()factory memoized at module level (theCallToolResultWireSchemawire seam moves verbatim from the 2025 registry into the same factory).schemas.tsbecomes an eager named-export shim over the memo for tests and tooling; the shim is not on any runtime import path and tree-shakes out of the published bundles.This eliminates the per-revision schema-construction cost at import (~2 revisions' worth per bundled copy of core-internal). Together with #2458 and #2459, this closes ~55% of a measured consumer import regression and ~96% of its end-to-end startup regression.
How Has This Been Tested?
typecheck:allandlint:allclean.Breaking Changes
None. No public API changes; the schema definitions are token-identical to before; registry lookups keep returning reference-identical schema objects; the exported method lists are unchanged.
Types of changes
Checklist
Additional context
memo ??= build()), so there is no async seam in any validation path and the first-use cost is paid exactly once per process.schemas.tsshims from withinsrc/wire, making the laziness invariant mechanical there too.Changeset: patch for
client,server, andserver-legacy(their bundles inline core-internal).