Smaller stack for set defaults #240
Closed
+75
−41
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? (check all applicable)
Description
Decoder.setDefaults()happens to use a lot of stack space for its local variables.Large stack usage by
setDefaults()is a performance problem because the go runtime assumes that goroutine stacks stay small. A big frame for local variables makes it very probable that the go runtime will need to grow the stack upon a call tosetDefaults(). In a service where I have observed this behaviour,Decoder.Decode()spends 3x more time inruntime.newstack()than in the rest ofDecoder.Decode().This patch set decreases the frame for
setDefaults()'s local variables from 2408 bytes to 312 (the figures are for go 1.25 on amd64).NOTE: the stack frame size of 2408 bytes guarantees that the goroutine stack will grow if
setDefaults()is called soon after a goroutine starts. Normally, there are some calls on the stack beforesetDefaults(), like the http server, the muxer, possibly otelhttp, etc. Because of thosesetDefaults()is only likely to grow the stack, not guaranteed to do so.Aside from reducing the stack usage, this patch series improves the speed of
go test -count=10000by some 4%.See individual commit messages for the rationale of each change.
Added/updated tests?
Run verifications and test
make verifyis passing:make verifyfails inmaindue togoseccomplaining about possible truncating conversions inconvertPointer(); this patch series adds no new failures.make testis passing