Repository navigation
builder: avoid reflect.DeepEqual boxing, error-only json.Marshal - #655
Open
mauriciopoppe wants to merge 1 commit into
Open
mauriciopoppe wants to merge 1 commit into
mauriciopoppe wants to merge 1 commit into
Conversation
Contributor
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: mauriciopoppe The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
mauriciopoppe
force-pushed
the
pr-wraprefs-builder-allocs
branch
3 times, most recently
from
October 8, 2026 00:41
c5aded4 to
4cbac46
Compare
…r-only json.Marshal in findCommonParameters
mauriciopoppe
force-pushed
the
pr-wraprefs-builder-allocs
branch
from
October 8, 2026 01:50
4cbac46 to
3ac76a1
Compare
mauriciopoppe
marked this pull request as ready for review
October 8, 2026 16:39
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What type of PR is this?
/kind cleanup
What this PR does / why we need it:
This PR reduces memory allocations and CPU overhead during OpenAPI v2 and v3 spec construction without changing any generated schemas or error messages:
Avoid
reflect.DeepEqualboxing and property loop escapes inWrapRefs(pkg/builder3/util/util.go):WrapRefspreviously checked if a$refschema had sibling fields via!reflect.DeepEqual(*schema, spec.Schema{SchemaProps: spec.SchemaProps{Ref: schema.Ref}}), boxing two 456-bytespec.Schemavalues onto the heap on every$ref. In addition, walking viaschemamutation.Walkercaused everyfor k, v := range schema.Propertiesloop variablevto escape to the heap (moved to heap: v).reflect.DeepEqualwith a zero-allocationhasSiblingElements(s *spec.Schema) boolcheck (guarded by compile-timestruct{ ... }(...)type conversion assertions onspec.Schema,spec.VendorExtensible,spec.SchemaProps, andspec.SwaggerSchemaProps, plus a per-field reflection testTestHasSiblingElementsAllFields), and uses copy-on-write helpers (wrapSchemaInPlace,wrapSchemaMap,wrapSchemaSlice) so unmodified property values stay on the stack.Defer duplicate-parameter error marshaling and avoid boxing
mapKeyFromParam(pkg/builder3/openapi.go,pkg/builder3/util.go,pkg/builder/openapi.go,pkg/builder/util.go):m, _ := json.Marshal(param)insideif routeParamDuplicateMap[key]infindCommonParametersso parameters are only marshaled when formatting a duplicate-parameter error, and changesmapKeyFromParamfrom returninginterface{}to a concreteparamKeystruct.Special notes for your reviewer:
Package Microbenchmarks (
pkg/builder3/util,pkg/builder3,pkg/builder)go test -bench=. -benchmem -count=5 ./pkg/builder3/util ./pkg/builder3 ./pkg/buildere2e80c32a35f)BenchmarkWrapRefs(pkg/builder3/util)ns/op)42,551 ns/op9,956 ns/op-76.6%(4.27xfaster)BenchmarkWrapRefs(pkg/builder3/util)B/op)48,472 B/op17,688 B/op-63.5%(-30,784 B/op)BenchmarkWrapRefs(pkg/builder3/util)allocs/op)109 allocs/op33 allocs/op-69.7%(-76 allocs/op)BenchmarkBuildOpenAPISpec(pkg/builder3)ns/op)172,649 ns/op119,630 ns/op-30.7%(1.44xfaster)BenchmarkBuildOpenAPISpec(pkg/builder3)B/op)112,975 B/op96,061 B/op-15.0%(-16,914 B/op)BenchmarkBuildOpenAPISpec(pkg/builder3)allocs/op)991 allocs/op752 allocs/op-24.1%(-239 allocs/op)BenchmarkBuildOpenAPISpec(pkg/builder)ns/op)305,553 ns/op263,102 ns/op-13.9%(1.16xfaster)BenchmarkBuildOpenAPISpec(pkg/builder)allocs/op)1,184 allocs/op954 allocs/op-19.4%(-230 allocs/op)Fixed-iteration
pprofdiff (go test -bench=BenchmarkWrapRefs -benchtime=20000x ./pkg/builder3/util):Kubernetes Integration Benchmark (
BenchmarkRequestCost, n=6)Evaluated by vendoring this PR (
3ac76a11efb8) intokubernetes/kubernetes(baseline704449b06d65) and runningBenchmarkRequestCost(test/integration/apiserver/flowcontrol/cost,-count=6,GOMAXPROCS=4). DuringsetupAPIServer,(*GenericAPIServer).InstallAPIGroupscallss.getOpenAPIModels()to build the Server-Side Applymanagedfields.TypeConverter, which invokesopenapibuilder3.BuildOpenAPIDefinitionsForResources->buildDefinitionRecursively->builderutil.WrapRefs(schema):go test -run=^$ -bench=^BenchmarkRequestCost$ -benchmem -count=6 ./test/integration/apiserver/flowcontrol/costFull 11-metric benchstat output
Raw benchmark output (before.txt / after.txt, n=6)
before.txt
after.txt
Does this PR introduce a user-facing change?
AI usage disclosure:
YES. AI tooling assisted with
pprofallocation profile analysis, benchmarking, and formatting. All code changes were reviewed and verified by the author.