Skip to content

Avoid repeated fragment complexity analysis - #5724

Open
ydah wants to merge 1 commit into
rmosolgo:masterfrom
ydah:avoid-repeated-fragment-complexity-analysis
Open

Avoid repeated fragment complexity analysis#5724
ydah wants to merge 1 commit into
rmosolgo:masterfrom
ydah:avoid-repeated-fragment-complexity-analysis

Conversation

@ydah

@ydah ydah commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Improve query complexity analysis for recursively reused fragments.

QueryComplexity currently visits each fragment spread as an independent expanded tree. A fragment graph such as:

fragment F2 on Node {
  a { ...F1 }
  b { ...F1 }
}

revisits the same nested selections for every path, causing analysis time to grow exponentially with the fragment level.

This PR calculates concrete object selections as a DAG and memoizes shared fragment complexity. Built-in QueryComplexity and MaxQueryComplexity analyzers use the precomputed result, while abstract selections and custom analyzer subclasses retain the existing visitor path.

Multiplex analyzers decide visitation per query, preventing an unsupported query in the same multiplex from causing already-precomputed queries to be expanded again.

Benchmark

Measured on Ruby 4.0.0 using binary fragment expansion. The previous implementation was measured through an analyzer subclass that retains the original visitor path.

Levels Query size Before After Complexity Speedup
10 573 bytes 11.832 ms 0.081 ms 3,071 146x
14 793 bytes 191.585 ms 0.092 ms 49,151 2,082x
16 903 bytes 823.619 ms 0.112 ms 196,607 7,354x
18 1,013 bytes 3,766.861 ms 0.117 ms 786,431 32,195x
20 1,123 bytes timeout after 3,082 ms 0.125 ms 3,145,727 >24,000x

The level-20 query now completes complexity analysis instead of reaching the validation timeout.

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.

1 participant