René's URL Explorer Experiment


Title: Performance improvements to oneOf validation by xuorig · Pull Request #4542 · graphql-java/graphql-java · GitHub

Open Graph Title: Performance improvements to oneOf validation by xuorig · Pull Request #4542 · graphql-java/graphql-java

X Title: Performance improvements to oneOf validation by xuorig · Pull Request #4542 · graphql-java/graphql-java

Description: Hey folks, we've been seeing a pretty high cost with oneOf validation on our graphql gateway. I've launched an LLM at the problem, and while it does improve things, I'm not sure if you're OK with the added complexity here. Wanted to discuss it here first. The TL;DR is that we can avoid a lot of work in the common case by validating only one level and not recursing down on variables that were already validated, with the edge case of default values. Let me know what you think. AI Summary Problem While coercing variables, ValuesResolverConversion.externalValueToInternalValueImpl calls ValuesResolverOneOfValidation.validateOneOfInputTypes(inputObjectType, coercedMap, ...) after converting every input object. That validator is a recursive walk: it descends into lists and into every field defined on each input object type (inputObjectType.getFields() + objectMap.get(...)), including fields that were not provided. Conversion itself is already recursive (externalValueToInternalValueForObject calls externalValueToInternalValueImpl per field, externalValueToInternalValueForList calls it per element), and each of those nested calls runs the walk again over its own subtree. So: the innermost object is walked by itself, then again by its parent, then again by its grandparent, ... an input object at depth d is validated d + 1 times, and the total work is O(depth x size) instead of O(size); types with no @oneOf anywhere below them pay the full cost, because the walk has to look at every field to find out. We noticed this in a production CPU profile of a GraphQL federation gateway, where it accounted for ~5% of total CPU: validateOneOfInputTypes <- validateOneOfInputTypes self-recursion, dominated by HashMap.getNode self time from the per-field map lookups. Fix In the variable conversion path, each input object is now checked at its own level only, right after it has been converted: if the type is @oneOf, the existing single-object check (validateOneOfInputTypesInternal) runs on the just coerced map; provided fields don't need re-walking: their values were produced by externalValueToInternalValueImpl, which already checked them at their own level (and so on, recursively); fields that were filled from a literal or internal default value never go through externalValueToInternalValueImpl, so those (and only those) are still walked recursively with the existing validateOneOfInputTypes. This is rare and only walks the default value's subtree. The literal/argument path (ValuesResolver.getArgumentValuesImpl) is unchanged and keeps its single recursive walk per argument: literal coercion does not validate per level, so that walk is not redundant there. The change is ~20 lines in ValuesResolverConversion plus making validateOneOfInputTypesInternal package-private. No public API, signature or schema changes. Why this is equivalent Every input object map in a coerced variable value is produced by exactly one of: externalValueToInternalValueImpl on an input object type (variables, list elements, provided object fields, external defaults, ValuesResolver.externalValueToInternalValue, valueToInternalValue for external values, DefaultValuesAreValid schema validation). Each such map is now validated at that point, which is exactly when the old code first validated it (its own Impl call ran the walk on its own subtree before returning). The parent re-walks could only re-visit maps that had already passed. defaultValueToInternalValue for a literal or internal default. These were previously only covered by the parent's walk, and are still walked recursively. This gap is real and not covered by schema validation: internal defaults (GraphQLInputObjectField.Builder.defaultValue(Object)) are not validated by DefaultValuesAreValid at all, and a literal default such as child: OneOf = {a: null} passes ValidationUtil.isValidLiteralValue (it only checks the key count) but must fail with OneOfNullValueException at coercion time. Both are covered by new tests, which fail if the default handling is removed. Other points checked: Lists coerced from a single non-list value (8c143ef): externalValueToInternalValueForList converts each element through externalValueToInternalValueImpl, including the singleton case, so each element is validated at its own level. Tested for top-level and nested lists. Null handling: unchanged. The walk skipped nulls; explicit nulls are still put in the coerced map as before and a oneOf {a: null} is still rejected by the single-level check. InputInterceptor: the check runs on the coerced map, after interception, exactly as before. Field visibility: unchanged. The old walk iterated getFields(), but hidden fields are never in the coerced map, so they were no-ops. Error types, messages and precedence: unchanged. Because each object was already validated by its own Impl call before its parent continued with the next sibling, the first error raised is the same in both versions: a nested oneOf violation still wins over a later sibling's coercion error, and an earlier sibling's coercion error still wins over a later oneOf violation (tested). For an object with literal/internal defaults, the object's own oneOf check still runs before its defaults are walked, matching the old walk's order. All new tests also pass against master, i.e. they pin the existing behaviour rather than a new one. Prior attempt: #4516 #4516 ("Skip OneOf input validation walk when no @OneOf type is reachable") added a schema-owned OneOf reachability map computed at schema construction (GraphQLSchema constructors, FastBuilder, transformWithoutTypes), a new @Internal GraphQLSchema.inputTypeContainsOneOf, and threaded an optional GraphQLSchema through ValuesResolver, ValuesResolverConversion, DirectivesResolver and others. The author closed it because local testing did not show a benefit worth the complexity. That approach only short-circuits types from which no @oneOf is reachable. When a oneOf is reachable (which is the case for any schema where oneOf matters), every level still ran the full recursive walk, so the O(depth x size) re-walk remained. It also introduced new schema state that every schema construction path has to keep correct. This change instead removes the redundant re-walk itself: it is a local change to the conversion code, with no new API, no schema state and no signature changes; it helps whether or not a @oneOf type is reachable (see the oneOfLeaf=true rows below, which #4516 could not have improved); it is backed by the benchmark below. Benchmark New benchmark.OneOfVariableCoercionBenchmark calls ValuesResolver.coerceVariableValues on a variable made of nested input objects depth levels deep. Each level has 8 optional String fields of which 3 are provided, plus a next field; the innermost level holds a Leaf { a: Int, b: String } input object, either plain (oneOfLeaf=false, no oneOf reachable anywhere) or @oneOf (oneOfLeaf=true). singleObject coerces one such value, listOfObjects a [Level0!] list of 20 of them. Settings: JMH 1.37, AverageTime, 2 forks, 3 warmup iterations x 2 s, 5 measurement iterations x 2 s (./gradlew jmh -PjmhInclude=benchmark.OneOfVariableCoercionBenchmark), JDK 25.0.2 (Zulu), Apple M2 Max. "Before" is the same benchmark run against master's src/main. Benchmark depth oneOfLeaf before (ns/op) after (ns/op) speedup singleObject 1 false 460.1 ± 5.4 364.5 ± 5.7 1.26x singleObject 1 true 472.7 ± 4.0 364.2 ± 17.6 1.30x singleObject 8 false 3,875.3 ± 28.4 1,744.9 ± 9.6 2.22x singleObject 8 true 3,963.0 ± 237.3 1,742.3 ± 30.7 2.27x listOfObjects 1 false 7,550.2 ± 166.9 5,446.0 ± 61.0 1.39x listOfObjects 1 true 8,180.6 ± 246.2 5,529.8 ± 70.3 1.48x listOfObjects 8 false 75,090.8 ± 892.9 33,401.3 ± 586.6 2.25x listOfObjects 8 true 80,076.9 ± 828.2 33,726.7 ± 420.2 2.37x With the fix, the cost is the same whether or not a oneOf is reachable, and it scales with the size of the value instead of size x depth. Tests New Spock tests in ValuesResolverTest: nested oneOf violations in variables are reported (too many keys, empty object, null value) at depth >= 3 through non-oneOf parents, for oneOf nested in oneOf, inside lists of oneOf, inside lists of non-oneOf parents, in a top-level list variable, and for single values coerced into (nested) lists; a valid deeply nested value is accepted unchanged; error precedence between a nested oneOf violation and a sibling coercion error, in both orders; oneOf violations in nested internal and literal defaults are still reported during variable coercion; external defaults with a oneOf violation are still rejected while the schema is validated. ./gradlew test (JDK 25 toolchain, the project default): 6088 tests, 0 failures, 0 errors, 7 skipped. This includes the existing ValuesResolver*, oneOf, graphql.execution and ArchUnit (JMH fork limit) tests. Side observation (unchanged by this PR) DefaultValuesAreValid.isValidExternalValue only catches CoercingParseValueException and NonNullableValueCoercedAsNullException, so an external default with a oneOf violation makes schema building throw the raw OneOfTooManyKeysException / OneOfNullValueException rather than an InvalidSchemaException. This is the same on master; the new test pins the current behaviour.

Open Graph Description: Hey folks, we've been seeing a pretty high cost with oneOf validation on our graphql gateway. I've launched an LLM at the problem, and while it does improve things, I'm not sure if you&...

X Description: Hey folks, we&#39;ve been seeing a pretty high cost with oneOf validation on our graphql gateway. I&#39;ve launched an LLM at the problem, and while it does improve things, I&#39;m not ...

Opengraph URL: https://github.com/graphql-java/graphql-java/pull/4542

X: @github

direct link

Domain: github.com

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:b066dda8-5b74-ef63-00b0-072473d876c4
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idE7A2:842EC:1F9D843:2A19C10:6B017C8E
html-safe-nonce8a0bf5a703ebcbfb6e34b8938cc64bcef63539fff3ab054e05a9a2635ab785df
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFN0EyOjg0MkVDOjFGOUQ4NDM6MkExOUMxMDo2QjAxN0M4RSIsInZpc2l0b3JfaWQiOiI3MzkwNjQwNTE2MjcyMDYxNTgyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac911831ee0f5a303462902bf90a10a2ba8fb3a1cf19da41e1c7522423f68aebad
hovercard-subject-tagpull_request:5190872062
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/graphql-java/graphql-java/pull/4542/files
twitter:imagehttps://avatars.githubusercontent.com/u/1919498?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/1919498?s=400&v=4
og:image:altHey folks, we've been seeing a pretty high cost with oneOf validation on our graphql gateway. I've launched an LLM at the problem, and while it does improve things, I'm not sure if you&...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
Nonecc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/graphql-java/graphql-java git https://github.com/graphql-java/graphql-java.git
octolytics-dimension-user_id14289921
octolytics-dimension-user_logingraphql-java
octolytics-dimension-repository_id38602457
octolytics-dimension-repository_nwographql-java/graphql-java
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id38602457
octolytics-dimension-repository_network_root_nwographql-java/graphql-java
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release8c77c9e8439031ad54c75a250694842892f693e1
ui-targetcanary-2
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/graphql-java/graphql-java/pull/4542/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgraphql-java%2Fgraphql-java%2Fpull%2F4542%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgraphql-java%2Fgraphql-java%2Fpull%2F4542%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=graphql-java%2Fgraphql-java
Reloadhttps://github.com/graphql-java/graphql-java/pull/4542/files
Reloadhttps://github.com/graphql-java/graphql-java/pull/4542/files
Reloadhttps://github.com/graphql-java/graphql-java/pull/4542/files
graphql-javahttps://github.com/graphql-java
graphql-javahttps://github.com/graphql-java/graphql-java
Codehttps://github.com/graphql-java/graphql-java
Issues27 (27)https://github.com/graphql-java/graphql-java/issues
Pull requests35 (35)https://github.com/graphql-java/graphql-java/pulls
Discussionshttps://github.com/graphql-java/graphql-java/discussions
Actionshttps://github.com/graphql-java/graphql-java/actions
Projectshttps://github.com/graphql-java/graphql-java/projects
Wikihttps://github.com/graphql-java/graphql-java/wiki
Security and qualityhttps://github.com/graphql-java/graphql-java/security
Insightshttps://github.com/graphql-java/graphql-java/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fgraphql-java%2Fgraphql-java%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fgraphql-java%2Fgraphql-java%2Fissues%2Fnew%2Fchoose
xuorighttps://github.com/xuorig
graphql-java:masterhttps://github.com/graphql-java/graphql-java/tree/master
xuorig:perf/oneof-validation-single-levelhttps://github.com/xuorig/graphql-java/tree/perf/oneof-validation-single-level
Conversation 1 https://github.com/graphql-java/graphql-java/pull/4542
Commits 1 https://github.com/graphql-java/graphql-java/pull/4542/commits
Checks 9 https://github.com/graphql-java/graphql-java/pull/4542/checks
Files changed https://github.com/graphql-java/graphql-java/pull/4542/files
Please reload this pagehttps://github.com/graphql-java/graphql-java/pull/4542/files
Performance improvements to oneOf validation https://github.com/graphql-java/graphql-java/pull/4542/files#top
Show all changes 1 commit https://github.com/graphql-java/graphql-java/pull/4542/files
e436ca7 Validate oneOf input objects once per level during variable coercion xuorig Nov 6, 2026 https://github.com/graphql-java/graphql-java/pull/4542/commits/e436ca77d4c7b96051db9694c34c6544d88da035
Clear filters https://github.com/graphql-java/graphql-java/pull/4542/files
Please reload this pagehttps://github.com/graphql-java/graphql-java/pull/4542/files
Please reload this pagehttps://github.com/graphql-java/graphql-java/pull/4542/files
OneOfVariableCoercionBenchmark.java https://github.com/graphql-java/graphql-java/pull/4542/files#diff-24542157a61f4aaf1e81f14a88e3e7899db6f3c533195a71aa784e94305ae95d
ValuesResolverConversion.java https://github.com/graphql-java/graphql-java/pull/4542/files#diff-80b8748b24d21f6277a1e5019cd808621d276b1abb50bd87672d447f5c840ef0
ValuesResolverOneOfValidation.java https://github.com/graphql-java/graphql-java/pull/4542/files#diff-aa850fe4d226b7cf918bd7a0700052cfb62840a9a635f92163fc9f992cbea0c5
ValuesResolverTest.groovy https://github.com/graphql-java/graphql-java/pull/4542/files#diff-6df3af80107d5608b74eb17ef8f3b544ff9cf0e6c4ea06b02bf0accabf119821
src/jmh/java/benchmark/OneOfVariableCoercionBenchmark.javahttps://github.com/graphql-java/graphql-java/pull/4542/files#diff-24542157a61f4aaf1e81f14a88e3e7899db6f3c533195a71aa784e94305ae95d
View file https://github.com/graphql-java/graphql-java/blob/e436ca77d4c7b96051db9694c34c6544d88da035/src/jmh/java/benchmark/OneOfVariableCoercionBenchmark.java
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/graphql-java/graphql-java/pull/4542/{{ revealButtonHref }}
src/main/java/graphql/execution/ValuesResolverConversion.javahttps://github.com/graphql-java/graphql-java/pull/4542/files#diff-80b8748b24d21f6277a1e5019cd808621d276b1abb50bd87672d447f5c840ef0
View file https://github.com/graphql-java/graphql-java/blob/e436ca77d4c7b96051db9694c34c6544d88da035/src/main/java/graphql/execution/ValuesResolverConversion.java
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/graphql-java/graphql-java/pull/4542/{{ revealButtonHref }}
https://github.com/graphql-java/graphql-java/pull/4542/files#diff-80b8748b24d21f6277a1e5019cd808621d276b1abb50bd87672d447f5c840ef0
https://github.com/graphql-java/graphql-java/pull/4542/files#diff-80b8748b24d21f6277a1e5019cd808621d276b1abb50bd87672d447f5c840ef0
https://github.com/graphql-java/graphql-java/pull/4542/files#diff-80b8748b24d21f6277a1e5019cd808621d276b1abb50bd87672d447f5c840ef0
https://github.com/graphql-java/graphql-java/pull/4542/files#diff-80b8748b24d21f6277a1e5019cd808621d276b1abb50bd87672d447f5c840ef0
https://github.com/graphql-java/graphql-java/pull/4542/files#diff-80b8748b24d21f6277a1e5019cd808621d276b1abb50bd87672d447f5c840ef0
https://github.com/graphql-java/graphql-java/pull/4542/files#diff-80b8748b24d21f6277a1e5019cd808621d276b1abb50bd87672d447f5c840ef0
src/main/java/graphql/execution/ValuesResolverOneOfValidation.javahttps://github.com/graphql-java/graphql-java/pull/4542/files#diff-aa850fe4d226b7cf918bd7a0700052cfb62840a9a635f92163fc9f992cbea0c5
View file https://github.com/graphql-java/graphql-java/blob/e436ca77d4c7b96051db9694c34c6544d88da035/src/main/java/graphql/execution/ValuesResolverOneOfValidation.java
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/graphql-java/graphql-java/pull/4542/{{ revealButtonHref }}
https://github.com/graphql-java/graphql-java/pull/4542/files#diff-aa850fe4d226b7cf918bd7a0700052cfb62840a9a635f92163fc9f992cbea0c5
https://github.com/graphql-java/graphql-java/pull/4542/files#diff-aa850fe4d226b7cf918bd7a0700052cfb62840a9a635f92163fc9f992cbea0c5
src/test/groovy/graphql/execution/ValuesResolverTest.groovyhttps://github.com/graphql-java/graphql-java/pull/4542/files#diff-6df3af80107d5608b74eb17ef8f3b544ff9cf0e6c4ea06b02bf0accabf119821
View file https://github.com/graphql-java/graphql-java/blob/e436ca77d4c7b96051db9694c34c6544d88da035/src/test/groovy/graphql/execution/ValuesResolverTest.groovy
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/graphql-java/graphql-java/pull/4542/{{ revealButtonHref }}
https://github.com/graphql-java/graphql-java/pull/4542/files#diff-6df3af80107d5608b74eb17ef8f3b544ff9cf0e6c4ea06b02bf0accabf119821
https://github.com/graphql-java/graphql-java/pull/4542/files#diff-6df3af80107d5608b74eb17ef8f3b544ff9cf0e6c4ea06b02bf0accabf119821
Please reload this pagehttps://github.com/graphql-java/graphql-java/pull/4542/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.