Fix O(n^2) transitive interface check in Schema.from_definition#5678
Merged
rmosolgo merged 1 commit intoJul 24, 2026
Merged
Conversation
238f193 added a transitive-interface validation that scans document.definitions linearly for every interface name of every object/interface type. On large schemas this dominates build time: a 9MB SDL (10.7k types) went from ~750ms to ~1.5s. Build a name=>definition hash once (first occurrence wins, matching the previous .find semantics for extension nodes) and skip the check for types with no interfaces. Schema.from_definition on the 9MB SDL: 1598ms -> 758ms. Assisted-By: devx/57cd31bb-9c77-4723-8862-7b08599a5d4f
Owner
|
Awesome, thanks for this improvement! |
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.
238f193 validates each object/interface type by calling
document.definitions.find { ... }for every interface name of every type — a linear scan inside the definitions loop, soSchema.from_definitionbecame O(n²) in the number of definitions. On a large real-world SDL (9MB, ~10,700 types), build time roughly doubled between v2.5.11 and v2.6.6;git bisectpoints at 238f193 as the sole cause.Fix
Build a
name => definitionhash once (lazily, only if some type declares interfaces) and look up in O(1). Two details preserve the exact previous behavior:by_name[d.name] ||= d: first occurrence wins, matchingArray#findwhen a name appears as both a definition and a type-extension node (theType extensionsspecs fail with last-wins).Same validation, same error message; all
build_from_definitionspecs pass, including the ones added in 238f193.Benchmark
N types implementing an interface that appears after them in the document (e.g. any alphabetically-ordered schema — if the interface comes first,
findreturns early and hides the cost):The 9MB real-world schema: 1,598ms → 758ms. With this PR the check measures within noise of removing it entirely (700ms vs 708ms).