Skip to content

perf: avoid interface boxing when decoding date/time values - #1085

Merged
pelletier merged 2 commits into
pelletier:v2from
nishantmehta:perf/datetime-decode-noboxing
Jul 3, 2026
Merged

pelletier merged 2 commits into
pelletier:v2from
nishantmehta:perf/datetime-decode-noboxing

Conversation

@nishantmehta

Copy link
Copy Markdown
Contributor

What

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 assignment 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.

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

benchmark/BenchmarkUnmarshal/ReferenceFile/struct:  88 -> 85 allocs/op,  3.424 -> 3.353 KiB/op

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.

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
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
@pelletier

Copy link
Copy Markdown
Owner

Thanks, this is a nice win! The coverage gate was failing because the !CanAddr() fallback in setConcrete is unreachable: everything the decoder assigns into is settable, and in reflect settable implies addressable (map elements are decoded into reflect.New-allocated temporaries, see descend). A non-addressable value would have panicked in v.Set just the same, so the fallback added no safety โ€” I pushed a commit removing it and updating the comment. Merging once CI is green.

@pelletier
pelletier merged commit f93de50 into pelletier:v2 Jul 3, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants