Skip to content

Reduce abstract schema validation overhead and allocations - #10158

Merged
glen-84 merged 2 commits into
mainfrom
gai/abstract-schema-validation-performance
Jul 28, 2026
Merged

glen-84 merged 2 commits into
mainfrom
gai/abstract-schema-validation-performance

Conversation

@glen-84

@glen-84 glen-84 commented Jul 28, 2026

Copy link
Copy Markdown
Member

Summary

  • SchemaValidator resolves the handler set for each event type once per validator, instead of type-testing every rule against every published event. Validating the GitHub public schema publishes 49,899 events, so the old linear scan performed roughly 898,000 interface type tests per validation.
  • Validation events are now readonly record struct, so publishing an event no longer allocates. This requires IValidationEventHandler<TEvent> to drop its in variance, which value types cannot satisfy. Both changes are breaking for anyone implementing custom rules outside this repository.
  • ValidNameRule compares the __ prefix ordinally rather than culture-sensitively, and ValidImplementationsRule builds its log-entry lists only when it has something to report and resolves implemented arguments by name instead of building a dictionary per field.
  • Against the GitHub public schema (1,813 types), validation goes from 4,013 μs and 2,109 KB to 1,347 μs and 520 KB, which is below the schema-build validator's 736 KB.

Test plan

  • dotnet test src/HotChocolate/Core/test/Types.Validation.Tests — 129 passing
  • dotnet test src/HotChocolate/Fusion/test/Fusion.Composition.Tests — 712 passing, exercising the mutable-schema consumer
  • BenchmarkDotNet comparison against the GitHub public schema, with per-rule and rule-count ablation runs to attribute each change

Copilot AI review requested due to automatic review settings July 28, 2026 12:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes HotChocolate schema validation by reducing per-event rule scanning and eliminating validation-event allocations, aiming to lower both runtime and GC pressure during schema builds.

Changes:

  • Cache the handler set per validation event type per SchemaValidator instance to avoid repeated is/OfType scanning for every published event.
  • Convert validation events from sealed record to readonly record struct and adjust IValidationEventHandler<TEvent> to remove contravariance to support struct events.
  • Micro-optimizations in validation rules (ordinal __ prefix check; reduce allocations in interface implementation validation).

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/HotChocolate/Core/src/Types.Validation/SchemaValidator.cs Caches per-event handler arrays to avoid re-scanning all rules for each published validation event.
src/HotChocolate/Core/src/Types.Validation/Rules/ValidNameRule.cs Uses ordinal comparison for reserved-name detection to avoid culture-sensitive overhead.
src/HotChocolate/Core/src/Types.Validation/Rules/ValidImplementationsRule.cs Avoids allocating error lists and argument dictionaries unless needed during interface-implementation checks.
src/HotChocolate/Core/src/Types.Validation/Events/ValidationEvents.cs Converts validation events to readonly record struct to eliminate per-event allocations.
src/HotChocolate/Core/src/Types.Validation/Events/Contracts/IValidationEventHandler.cs Removes in variance to align with struct-based events and updated dispatch.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions

github-actions Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Patch coverage

95.1% of changed lines covered (58/61)

File Covered Changed Patch %
…/Core/src/Types.Validation/Events/ValidationEvents.cs 13 16 81.3% 🟡
…/Core/src/Types.Validation/Rules/ValidImplementationsRule.cs 21 21 100.0% 🟢
…/Core/src/Types.Validation/Rules/ValidNameRule.cs 1 1 100.0% 🟢
…/HotChocolate/Core/src/Types.Validation/SchemaValidator.cs 23 23 100.0% 🟢
Uncovered changed lines (JSON)
{
  "sha": "0685ce1ab4dc473d4c3ec6d63ea26f4642734b0e",
  "files": [
    { "path": "src/HotChocolate/Core/src/Types.Validation/Events/ValidationEvents.cs", "ranges": [[60, 60], [105, 105], [110, 110]] }
  ]
}

Project coverage: 53.9% (232774/431566 lines)

@glen-84
glen-84 merged commit bdd5d99 into main Jul 28, 2026
285 of 287 checks passed
@glen-84
glen-84 deleted the gai/abstract-schema-validation-performance branch July 28, 2026 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants