Skip to content

Memoize schemas in DynamicSchemaIndex compilation - #2025

Open
kubukoz wants to merge 2 commits into
series/0.19from
dynamic-memoize-schemas
Open

kubukoz wants to merge 2 commits into
series/0.19from
dynamic-memoize-schemas

Conversation

@kubukoz

@kubukoz kubukoz commented Nov 18, 2026

Copy link
Copy Markdown
Member

Problem

DynamicSchemaIndex.load compiles each shape into an Eval[Schema], but these weren't memoized: each entry in schemaMap was a lSchema.map(...) chain, and references to other shapes went through Eval.defer(schemaMap(id)). So every reference to a shape re-ran the whole computation of its target, all the way down (only recursive shapes cut this, thanks to their Eval.later).

In models where shapes are referenced from multiple places, this is exponential in the depth of the sharing. For a chain of structures S0 { a: S1, b: S1 }, S1 { a: S2, b: S2 }, ..., load time doubled with each extra level:

Depth Before After
10 17ms 14ms
16 128ms 11ms
18 474ms 7ms
20 2051ms 8ms

Fix

.memoize the schema stored for each shape, so it's computed once and shared by all the shapes referencing it. This only adds caching; the evaluation order is unchanged, so recursive shapes still go through the lazy recursive(...) schema as before. Everything is still forced during load on a single thread.

Real-world numbers

On a large production model (~235k shapes in the Smithy model, ~25k schemas and ~640 services in the resulting index), warm runs of DynamicSchemaIndex.load:

Before After
default ~7.2s ~0.45s
applySchemaRefinements = true ~9.7s ~0.52s

Tests

SchemaSharingSpec checks, by reference equality, that schemas of shapes referenced from several places are shared (fails without the fix), plus a depth-40 diamond chain as a backstop against the exponential behaviour (would hang without the fix).

🤖 Generated with Claude Code

kubukoz and others added 2 commits November 18, 2026 12:18
Each shape's schema was an un-memoized Eval chain, so every reference to
a shape re-ran the whole computation of its target. In models where
shapes are referenced from several places, this made loading exponential
in the depth of the sharing. Schemas are now computed once per shape and
shared.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kubukoz
kubukoz marked this pull request as ready for review November 18, 2026 11:43
@mergify

mergify Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request

This branch has not been deployed

No deployments
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.

1 participant