• Lorenz Bauer's avatar
    encoding/binary: cache struct sizes to speed up Read and Write · c9d89f6b
    Lorenz Bauer authored
    A majority of work is spent in dataSize when en/decoding the same
    struct over and over again. This wastes a lot of work, since
    the result doesn't change for a given reflect.Value.
    
    Cache the result of the function for structs, so that subsequent
    calls to dataSize can avoid doing work.
    
        name         old time/op    new time/op     delta
        ReadStruct     1.00µs ± 1%     0.37µs ± 1%   -62.99%  (p=0.029 n=4+4)
        WriteStruct    1.00µs ± 3%     0.37µs ± 1%   -62.69%  (p=0.008 n=5+5)
    
        name         old speed      new speed       delta
        ReadStruct   75.1MB/s ± 1%  202.9MB/s ± 1%  +170.16%  (p=0.029 n=4+4)
        WriteStruct  74.8MB/s ± 3%  200.4MB/s ± 1%  +167.96%  (p=0.008 n=5+5)
    
    Fixes #34471
    
    Change-Id: Ic5d987ca95f1197415ef93643a0af6fc1224fdf0
    Reviewed-on: https://go-review.googlesource.com/c/go/+/199539Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
    Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
    TryBot-Result: Gobot Gobot <gobot@golang.org>
    c9d89f6b
binary_test.go 13.3 KB