Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
progress: cache size of structs when finding size of slice of structs
In the current Go binary.Size() implementation, the size of struct types are cached to prevent subsequent reflect based encoding for the same types over and over again but aren't cached when encoding slices of structs. There is an allocation everytime when finding the size of structs. See golang/go#2320. Below are the benchmarks for sysenc.Marshal and sysenc.Unmarshal before caching the sizes of structs in a slice: BenchmarkMarshal/*sysenc.explicitPad BenchmarkMarshal/*sysenc.explicitPad-8 29728785 40.49 ns/op 0 B/op 0 allocs/op BenchmarkMarshal/[]sysenc.explicitPad BenchmarkMarshal/[]sysenc.explicitPad-8 19365120 66.87 ns/op 8 B/op 1 allocs/op BenchmarkMarshal/[]sysenc.explicitPad#01 BenchmarkMarshal/[]sysenc.explicitPad#01-8 19420356 61.40 ns/op 8 B/op 1 allocs/op BenchmarkMarshal/[]sysenc.explicitPad#02 BenchmarkMarshal/[]sysenc.explicitPad#02-8 19114594 61.51 ns/op 8 B/op 1 allocs/op BenchmarkMarshal/*sysenc.struc BenchmarkMarshal/*sysenc.struc-8 13065495 90.56 ns/op 32 B/op 2 allocs/op BenchmarkMarshal/[]sysenc.struc BenchmarkMarshal/[]sysenc.struc-8 14870079 79.70 ns/op 16 B/op 2 allocs/op BenchmarkMarshal/[]sysenc.struc#01 BenchmarkMarshal/[]sysenc.struc#01-8 6759981 175.5 ns/op 64 B/op 6 allocs/op BenchmarkMarshal/[]sysenc.struc#02 BenchmarkMarshal/[]sysenc.struc#02-8 5963643 201.4 ns/op 80 B/op 6 allocs/op BenchmarkUnmarshal/*sysenc.explicitPad BenchmarkUnmarshal/*sysenc.explicitPad-8 28909479 42.84 ns/op 0 B/op 0 allocs/op BenchmarkUnmarshal/[]sysenc.explicitPad BenchmarkUnmarshal/[]sysenc.explicitPad-8 19096471 65.82 ns/op 8 B/op 1 allocs/op BenchmarkUnmarshal/[]sysenc.explicitPad#01 BenchmarkUnmarshal/[]sysenc.explicitPad#01-8 18749620 62.87 ns/op 8 B/op 1 allocs/op BenchmarkUnmarshal/[]sysenc.explicitPad#02 BenchmarkUnmarshal/[]sysenc.explicitPad#02-8 18832404 62.41 ns/op 8 B/op 1 allocs/op BenchmarkUnmarshal/*sysenc.struc BenchmarkUnmarshal/*sysenc.struc-8 14550914 82.28 ns/op 16 B/op 1 allocs/op BenchmarkUnmarshal/[]sysenc.struc BenchmarkUnmarshal/[]sysenc.struc-8 14860458 81.24 ns/op 16 B/op 2 allocs/op BenchmarkUnmarshal/[]sysenc.struc#01 BenchmarkUnmarshal/[]sysenc.struc#01-8 6388365 172.0 ns/op 48 B/op 5 allocs/op BenchmarkUnmarshal/[]sysenc.struc#02 BenchmarkUnmarshal/[]sysenc.struc#02-8 6026479 196.7 ns/op 56 B/op 5 allocs/op Below are the benchmarks after caching the size of struct type when finding the size of a slice of structs: BenchmarkMarshal/*sysenc.explicitPad BenchmarkMarshal/*sysenc.explicitPad-8 30003156 40.03 ns/op 0 B/op 0 allocs/op BenchmarkMarshal/[]sysenc.explicitPad BenchmarkMarshal/[]sysenc.explicitPad-8 28622454 42.11 ns/op 0 B/op 0 allocs/op BenchmarkMarshal/[]sysenc.explicitPad#01 BenchmarkMarshal/[]sysenc.explicitPad#01-8 28405308 42.28 ns/op 0 B/op 0 allocs/op BenchmarkMarshal/[]sysenc.explicitPad#02 BenchmarkMarshal/[]sysenc.explicitPad#02-8 28176344 42.27 ns/op 0 B/op 0 allocs/op BenchmarkMarshal/*sysenc.struc BenchmarkMarshal/*sysenc.struc-8 13159680 89.76 ns/op 32 B/op 2 allocs/op BenchmarkMarshal/[]sysenc.struc BenchmarkMarshal/[]sysenc.struc-8 27897460 44.13 ns/op 0 B/op 0 allocs/op BenchmarkMarshal/[]sysenc.struc#01 BenchmarkMarshal/[]sysenc.struc#01-8 8578203 140.3 ns/op 48 B/op 4 allocs/op BenchmarkMarshal/[]sysenc.struc#02 BenchmarkMarshal/[]sysenc.struc#02-8 7239033 169.4 ns/op 64 B/op 4 allocs/op BenchmarkUnmarshal/*sysenc.explicitPad BenchmarkUnmarshal/*sysenc.explicitPad-8 29648662 40.53 ns/op 0 B/op 0 allocs/op BenchmarkUnmarshal/[]sysenc.explicitPad BenchmarkUnmarshal/[]sysenc.explicitPad-8 28812706 41.94 ns/op 0 B/op 0 allocs/op BenchmarkUnmarshal/[]sysenc.explicitPad#01 BenchmarkUnmarshal/[]sysenc.explicitPad#01-8 28351814 42.45 ns/op 0 B/op 0 allocs/op BenchmarkUnmarshal/[]sysenc.explicitPad#02 BenchmarkUnmarshal/[]sysenc.explicitPad#02-8 28420052 42.10 ns/op 0 B/op 0 allocs/op BenchmarkUnmarshal/*sysenc.struc BenchmarkUnmarshal/*sysenc.struc-8 14428018 81.28 ns/op 16 B/op 1 allocs/op BenchmarkUnmarshal/[]sysenc.struc BenchmarkUnmarshal/[]sysenc.struc-8 28384674 42.16 ns/op 0 B/op 0 allocs/op BenchmarkUnmarshal/[]sysenc.struc#01 BenchmarkUnmarshal/[]sysenc.struc#01-8 9146433 130.7 ns/op 32 B/op 3 allocs/op BenchmarkUnmarshal/[]sysenc.struc#02 BenchmarkUnmarshal/[]sysenc.struc#02-8 7504413 159.2 ns/op 40 B/op 3 allocs/op Signed-off-by: kwakubiney <kebiney@hotmail.com>
- Loading branch information