Speed up dataType validation ~12x by collapsing default type branches - #18
Merged
Conversation
dataType was a oneOf with one branch per known type name, so Ajv had to try every branch for every type instance validated. All default-schema branches are identical apart from the name, so collapse them into two discriminating branches (bare name / [name, data] pair); types with a real schema keep their individual $ref branch. dataType is also rebuilt lazily now, so registering N types compiles it once instead of N times. Verified against all 111 minecraft-data protocols: identical accept/reject verdicts, 104.8s -> 8.8s.
Member
|
Cool thanks |
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
dataTypewas built as aoneOfwith one branch per known type name. A modern Minecraft protocol registers 200–600 type names per namespace, and Ajv evaluates everyoneOfbranch for each validated instance, so validation was quadratic in the number of registered types:Change
enum(plusnative) and an[name, data]tuple form. Types registered with a real schema (the built-in numeric/structures/etc. types and user-added ones likeentityMetadataItem) keep their individual$refbranch, so their constraints are fully preserved.dataTypeis now rebuilt lazily, so registering N types (asvalidateProtocoldoes per namespace) compiles it once instead of N times.Verification
[type, data]arrays, unknown names in nested namespaces) reject identically in both.npm testsuite drops from ~3min to ~25s with this change alone.No public API changes.