Skip to content

Speed up dataType validation ~12x by collapsing default type branches - #18

Merged
rom1504 merged 1 commit into
ProtoDef-io:masterfrom
u9g:fast-datatype
Aug 16, 2026
Merged

Speed up dataType validation ~12x by collapsing default type branches#18
rom1504 merged 1 commit into
ProtoDef-io:masterfrom
u9g:fast-datatype

Conversation

@u9g

@u9g u9g commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Problem

dataType was built as a oneOf with one branch per known type name. A modern Minecraft protocol registers 200–600 type names per namespace, and Ajv evaluates every oneOf branch for each validated instance, so validation was quadratic in the number of registered types:

  • validating one modern protocol: ~1s
  • validating all 111 minecraft-data protocols: ~105s (this dominates the minecraft-data test suite)

Change

  • All default-schema branches are identical apart from the name, so they collapse into two discriminating branches: a bare-name enum (plus native) and an [name, data] tuple form. Types registered with a real schema (the built-in numeric/structures/etc. types and user-added ones like entityMetadataItem) keep their individual $ref branch, so their constraints are fully preserved.
  • dataType is now rebuilt lazily, so registering N types (as validateProtocol does per namespace) compiles it once instead of N times.

Verification

  • Ran old vs new over all 111 protocol.json files in minecraft-data (pc + bedrock): identical accept/reject verdicts on every one (0 mismatches).
  • Negative controls (unknown type refs, malformed [type, data] arrays, unknown names in nested namespaces) reject identically in both.
  • Timing across all 111 protocols: 104.8s → 8.8s (~12x). The minecraft-data npm test suite drops from ~3min to ~25s with this change alone.

No public API changes.

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.
@rom1504

rom1504 commented Aug 16, 2026

Copy link
Copy Markdown
Member

Cool thanks

@rom1504
rom1504 merged commit 30c2075 into ProtoDef-io:master Aug 16, 2026
1 check passed
@u9g
u9g deleted the fast-datatype branch August 17, 2026 04:36
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.

2 participants