Skip to content

Optimize repeated FieldsWillMerge comparisons - #5704

Merged
rmosolgo merged 1 commit into
rmosolgo:masterfrom
ydah:optimize-fields-will-merge-comparisons
Aug 21, 2026
Merged

Optimize repeated FieldsWillMerge comparisons#5704
rmosolgo merged 1 commit into
rmosolgo:masterfrom
ydah:optimize-fields-will-merge-comparisons

Conversation

@ydah

@ydah ydah commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Improve the performance of FieldsWillMerge validation for queries containing many repeated composite fields and shared fragment selections.

Previously, the validator could repeatedly compare the same expanded fragment fields for every pair of parent fields. This caused nested Cartesian comparisons and severe superlinear runtime growth before query execution or authentication.

This change:

  • groups fields with identical sub-selections and compares only one representative from each group
  • memoizes comparisons between equivalent expanded field groups
  • keeps mutually exclusive and non-exclusive comparisons separate
  • adds a regression test that verifies a divergent conflict is still detected without reaching the default validation timeout

Benchmark

Measured using a query with repeated composite fields and a shared fragment containing repeated fields.

Fields Query size Before After Speedup
100 2,320 bytes 193.6 ms 3.14 ms 61.7x
200 4,552 bytes 1,438.4 ms 3.72 ms 386.7x
400 9,000 bytes 11,732.5 ms 8.68 ms 1,351.7x
800 17,864 bytes did not finish within 30 seconds 20.08 ms

The 400-field case is reduced by approximately 99.93%.

@rmosolgo
rmosolgo requested a review from swalkinshaw August 17, 2026 19:00

@swalkinshaw swalkinshaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks like a good optimization to me

@rmosolgo

Copy link
Copy Markdown
Owner

Thanks for taking a look, @swalkinshaw . The only thing that stood out to me was the use of to_query_string, which creates a big string for large nested selections. But I think it's pretty optimized in terms of object allocations, and the speedup here is worth it. Thanks for sharing this, @ydah!

@rmosolgo
rmosolgo merged commit b85914d into rmosolgo:master Aug 21, 2026
13 of 15 checks passed
@rmosolgo rmosolgo added this to the 2.6.10 milestone Aug 21, 2026
@ydah
ydah deleted the optimize-fields-will-merge-comparisons branch August 21, 2026 20:32
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.

3 participants