encoding/binary: cache dataSize result across invocations of Read and Write for slice of structs #66253
Labels
FrozenDueToAge
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
This patch aims to extend this optimization c9d89f6 to slice of structs.
I have written some benchmarks in the
encoding/binary
package to measure the impact of the change when encoding a slice of structs.Profiling those benchmarks show that there is an absurd number of allocations when writing a slice of structs caused by reflection. Allocations at
reflect.(*structType).Field
account for ~75% of the total allocs made as shown below.Allocations without patch
This patch essentially also caches the result of
binary.dataSize
in the samesync.Map
being used in thestruct
case. This leads to huge allocation savings in writing a slice of structs.Allocations with patch
Running
benchstat
on both implementations shows these savings for writes and reads.The text was updated successfully, but these errors were encountered: