⚡ Sangsakala Proxy 🏠 Home
Skip to content

Reduce allocation and traversal work during schema generation - #4535

Open
andimarek wants to merge 5 commits into
masterfrom
codex/large-schema-generation-performance
Open

andimarek wants to merge 5 commits into
masterfrom
codex/large-schema-generation-performance

Conversation

@andimarek

@andimarek andimarek commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Both schema generators spend substantial time traversing schema elements and validating the resulting schema. This PR reduces allocation and repeated work in that shared code through four changes, developed over three profiling rounds:

  1. Create ancestor lists only when needed — DefaultTraverserContext. Previously, every traversal context copied its parent's complete ancestor list, even when the visitor never requested that list. Now it captures the parent breadcrumb and builds/caches the full immutable list on the first getBreadcrumbs() call. Traversals that do not need breadcrumbs avoid those list allocations and copies. Parent-node snapshots and custom context behavior are preserved.

  2. Create local variable maps only on the first write — TraverserState / DefaultTraverserContext. Previously, every non-root sequential traversal context allocated an empty LinkedHashMap. Now the map is allocated only when setVar() is called. Contexts that only read inherited state therefore need no local map; parent lookup and variable shadowing remain dynamic.

  3. Allocate the flattened child list at its final size — SchemaElementChildrenContainer.getChildrenAsList(). Previously, repeated addAll() calls grew the result array and created temporary arrays for child groups. Now the method sums the child counts, allocates the required capacity once, and appends children directly. This removes temporary arrays and growth copies while preserving order, duplicates, and an independent mutable result.

  4. Look up validation state only for nodes with directives — AppliedDirectivesAreValid. Previously, every visited node searched its ancestor contexts for the schema and error collector before checking whether it had directives. Now that check happens first. Nodes without directives avoid both ancestor searches; nodes with directives receive the same validation checks.

The JMH benchmarks now cover both generators, all five large schemas, and both generation-only and parsing-plus-generation, with schema validation enabled. Spock tests cover the behavior affected by these optimizations.

Final mean time reduction versus master b88bb44f5 (already includes #4528):

Fixture SchemaGenerator: generation Fast: generation SchemaGenerator: parse + generation Fast: parse + generation
1 7.75% 4.53% 5.53% 3.77%
2 6.26% 4.76% 4.27% 2.17%
3 12.01% 7.49% 7.46% 5.83%
4 15.87% 12.57% 10.66% 8.64%
5 21.54% 24.55% 16.91% 19.79%

Allocation per operation falls 2.48–30.32%. The 2.17% Fast/fixture-2 parsing gain has overlapping variability; it is not claimed as independently significant.

Measured serially on a dedicated Ryzen 5 3600 using Java 25, ZGC, compact headers, adaptive GC workers, and a 24 GiB heap. Final comparison: 3 forks/version/case, each with 10 × 10s warmup and 5 × 10s measurement, alternating version order. Profiling was separate. All 120 forks completed without allocation stalls; one warmup trend was inspected and documented. Original server settings were restored.

Validation: 6,029 Spock cases passed (7 existing skips), all ten generator/fixture outputs match baseline, build/TCK checks passed, and the local per-class coverage gate reports no regressions. Local counts include two pre-existing parser tests excluded from this PR. CI also passed Java 17/21/25, jcstress, Javadoc, and the per-class coverage gate.

Full results, raw JMH samples, profile evidence, caveats, and reproduction instructions.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Test Report

Test Results

Java Version Total Passed Failed Errors Skipped
Java 17 6193 (+15 🟢) 6137 (+15 🟢) 0 (±0) 0 (±0) 56 (±0)
Java 21 6193 (+15 🟢) 6137 (+15 🟢) 0 (±0) 0 (±0) 56 (±0)
Java 25 6193 (+15 🟢) 6137 (+15 🟢) 0 (±0) 0 (±0) 56 (±0)
jcstress 32 (±0) 32 (±0) 0 (±0) 0 (±0) 0 (±0)
Total 18611 (+45 🟢) 18443 (+45 🟢) 0 (±0) 0 (±0) 168 (±0)

Code Coverage (Java 25)

Metric Covered Missed Coverage vs Master
Lines 30348 3078 90.8% ±0.0%
Branches 8973 1505 85.6% ±0.0%
Methods 8097 1185 87.2% ±0.0%

Changed Class Coverage (4 classes)

Class Line Branch Method
g.s.GraphQLDirectiveContainer +25.0% 🟢 ±0.0% +25.0% 🟢
g.s.SchemaElementChildrenContainer +12.1% 🟢 +15.0% 🟢 +10.0% 🟢
g.s.v.AppliedDirectivesAreValid +4.3% 🟢 +10.0% 🟢 ±0.0%
g.u.DefaultTraverserContext +1.6% 🟢 +3.3% 🟢 +3.3% 🟢

Full HTML report: build artifact jacoco-html-report

Updated: 2026-11-03 22:25:10 UTC

This branch has not been deployed

No deployments
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