Skip to content

Optimize validation rule hot paths#5588

Merged
rmosolgo merged 1 commit intomasterfrom
optimize-validation-rules
Mar 25, 2026
Merged

Optimize validation rule hot paths#5588
rmosolgo merged 1 commit intomasterfrom
optimize-validation-rules

Conversation

@swalkinshaw
Copy link
Copy Markdown
Collaborator

@swalkinshaw swalkinshaw commented Mar 24, 2026

Extracted from #5578

These a few miscellaneous micro-optimizations to validation rules.

  • ArgumentNamesAreUnique: avoid Hash.new block allocation on the common unique-args path; only build array on actual collision
  • FieldsHaveAppropriateSelections: fast-path leaf+no-selections and non-leaf+selections cases, skipping the full validation method
  • RequiredArgumentsArePresent: cache required arg names per definition to avoid re-iterating arguments for repeated field definitions
    - FragmentSpreadsArePossible: use Array#intersect? instead of none? { include? } for type overlap check

Note: I'm keeping all the FieldsWillMerge optimizations for their own PR.

@swalkinshaw
Copy link
Copy Markdown
Collaborator Author

Oh didn't realize Ruby 2.7 was still supported 😓 so intersect? isn't available. I'll drop that one

- ArgumentNamesAreUnique: avoid Hash.new block allocation on the common
  unique-args path; only build array on actual collision
- FieldsHaveAppropriateSelections: fast-path leaf+no-selections and
  non-leaf+selections cases, skipping the full validation method
- RequiredArgumentsArePresent: cache required arg names per definition
  to avoid re-iterating arguments for repeated field definitions
  context.query.types to reduce method dispatch
@swalkinshaw swalkinshaw force-pushed the optimize-validation-rules branch from 6fe9e2f to a7af5e4 Compare March 24, 2026 22:47
@rmosolgo rmosolgo added this to the 2.5.23 milestone Mar 25, 2026
@rmosolgo
Copy link
Copy Markdown
Owner

Thanks for these improvements!

I'll have to keep intersect in mind for when we drop Ruby 2.7 support. I think there are other places it could be used, too, for example:

p_types.any? { |t| c_p_types.include?(t) }

query.types.possible_types(b).any? { |t| a_types.include?(t) }

@rmosolgo rmosolgo merged commit e38defe into master Mar 25, 2026
13 checks passed
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