Repository navigation
perf: avoid interface boxing when decoding date/time values - #1085
Merged
pelletier merged 2 commits intoJul 3, 2026
Merged
Conversation
Unmarshaling a TOML date/time into a time.Time, LocalDate, LocalTime or LocalDateTime field used v.Set(reflect.ValueOf(x)), which boxes the value into an interface and forces a heap allocation (24 B for time.Time). Every other scalar assign path uses a typed setter (SetString/SetInt/SetFloat) and does not allocate; date/time was the exception because reflect has no typed setter for struct values. Add a small generic helper that writes directly through the field's address when it is addressable (struct fields, slice elements), falling back to the reflect path for non-addressable targets (map elements). This removes one allocation per date/time value decoded into a struct/slice and is ~3x faster at the assignment step. benchmark/BenchmarkUnmarshal/ReferenceFile/struct: 88 -> 85 allocs/op, 3.424 -> 3.353 KiB/op. Signed-off-by: Nishant Mehta <nishantmehta.n@gmail.com>
nishantmehta
marked this pull request as ready for review
June 28, 2026 21:08
Every value the decoder assigns into is settable, and reflect settability implies addressability, so the !CanAddr fallback could never execute without v.Set panicking anyway (map elements are decoded into reflect.New-allocated temporaries, not assigned directly). Removing the dead branch restores 100% coverage of setConcrete, which was failing the coverage gate. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0187anwQiB6g8iG6AHHyeG6e
Owner
|
Thanks, this is a nice win! The coverage gate was failing because the |
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
Unmarshaling a TOML date/time into a
time.Time,LocalDate,LocalTimeorLocalDateTimefield usedv.Set(reflect.ValueOf(x)), which boxes the value into an interface and forces a heap allocation (24 B fortime.Time). Every other scalar assignment path uses a typed setter (SetString/SetInt/SetFloat) and does not allocate; date/time was the exception becausereflecthas no typed setter for struct values.This adds a small generic helper that writes directly through the field's address when it is addressable (struct fields, slice elements), falling back to the reflect path for non-addressable targets (map elements). It removes one allocation per date/time value decoded into a struct/slice and is ~3x faster at the assignment step.
Benchmark
Microbenchmark of the assignment alone:
24 B/op, 1 alloc -> 0 B/op, 0 allocs(~3x faster).Testing
Full test suite passes. No behavioral change โ the
time.Time/local-date values produced are identical; only the assignment mechanism changes.