Repository navigation
reflectlite: use less stack when reading struct fields - #5884
Merged
davecheney merged 1 commit intoOct 17, 2026
Merged
Conversation
davecheney
force-pushed
the
dfc/reflectlite-field-stack
branch
from
October 15, 2026 21:57
7f6c740 to
f142461
Compare
Contributor
Author
|
@jakebailey @dgryski gentle ping |
dgryski
approved these changes
Oct 16, 2026
dgryski
left a comment
Member
There was a problem hiding this comment.
LGTM. Had a discussion with by bot about this and forgot to come back here with a response.
davecheney
force-pushed
the
dfc/reflectlite-field-stack
branch
from
October 16, 2026 21:58
f142461 to
aa8a1bd
Compare
davecheney
marked this pull request as draft
October 16, 2026 23:57
davecheney
force-pushed
the
dfc/reflectlite-field-stack
branch
from
October 17, 2026 01:02
0b4a558 to
a8706a0
Compare
Reflection on struct fields used too much stack. RawType.Field and Value.Field each built a complete rawStructField in an extra stack frame, and that frame was the deepest part of the call. On small goroutine stacks this is a problem: simavr has a 384-byte default stack, and its value went up and down in the past because tests overflowed it (32a7b8c, a94e03e, and the 512-byte setting for math.go in main_test.go). Code that calls Value.Field in a recursive walk, for example struct equality in the runtime (runtime.reflectValueEqual), reaches the limit quickly. Stack use for a four-field struct, dev compared to this PR. Built at -opt=z with LLVM 22.1.8. The values come from the disassembly of complete programs: pushed registers, stack adjustment and return address of each frame in the deepest chain. runtime.alloc is not included, because it is the same before and after. Below runtime.reflectValueEqual (Value.Field and its callees): Target | dev | This PR --- | ---: | ---: cortex-m-qemu | 112 B | 48 B riscv-qemu | 128 B | 80 B simavr | 65 B | 45 B Below the caller of reflect.Type.Field, with Field called from several locations so that it is not inlined into one caller: Target | dev | This PR --- | ---: | ---: cortex-m-qemu | 48 B | 40 B riscv-qemu | 48 B | 48 B simavr | 68 B | 31 B The gain is largest on AVR. On cortex-m and riscv, LLVM already inlined most of the old Field path, so the gain there is small.
davecheney
force-pushed
the
dfc/reflectlite-field-stack
branch
from
October 17, 2026 01:05
a8706a0 to
6a230d1
Compare
davecheney
marked this pull request as ready for review
October 17, 2026 01:06
Contributor
Author
|
@dgryski @jakebailey PTAL |
jakebailey
approved these changes
Oct 17, 2026
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.
Reflection on struct fields used too much stack. RawType.Field and Value.Field each built a complete rawStructField in an extra stack frame, and that frame was the deepest part of the call. On small goroutine stacks this is a problem: simavr has a 384-byte default stack, and its value went up and down in the past because tests overflowed it (32a7b8c, a94e03e, and the 512-byte setting for math.go in main_test.go). Code that calls Value.Field in a recursive walk, for example struct equality in the runtime (runtime.reflectValueEqual), reaches the limit quickly.
Stack use for a four-field struct, dev compared to this PR. Built at -opt=z with LLVM 22.1.8. The values come from the disassembly of complete programs: pushed registers, stack adjustment and return address of each frame in the deepest chain. runtime.alloc is not included, because it is the same before and after.
Below runtime.reflectValueEqual (Value.Field and its callees):
Below the caller of reflect.Type.Field, with Field called from several locations so that it is not inlined into one caller:
The gain is largest on AVR. On cortex-m and riscv, LLVM already inlined most of the old Field path, so the gain there is small.