Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid double alloc in NewCidV1 #132

Merged
merged 1 commit into from Sep 12, 2021
Merged

avoid double alloc in NewCidV1 #132

merged 1 commit into from Sep 12, 2021

Commits on Sep 7, 2021

  1. avoid double alloc in NewCidV1

    We allocate once via "make([]byte, len)",
    and again when that buffer is converted to a string.
    
    Thankfully, since Go 1.10 we have strings.Builder,
    designed specifically for this use case.
    
    In a downstream benchmark in go-car,
    which needs to reconstruct many CID values,
    we see small but nice gains:
    
        name           old time/op    new time/op    delta
        ReadBlocks-16    1.09ms ± 4%    1.06ms ± 5%   -3.33%  (p=0.007 n=11+11)
    
        name           old speed      new speed      delta
        ReadBlocks-16   478MB/s ± 4%   494MB/s ± 5%   +3.46%  (p=0.007 n=11+11)
    
        name           old alloc/op   new alloc/op   delta
        ReadBlocks-16    1.30MB ± 0%    1.25MB ± 0%   -3.86%  (p=0.000 n=12+12)
    
        name           old allocs/op  new allocs/op  delta
        ReadBlocks-16     9.50k ± 0%     8.45k ± 0%  -11.05%  (p=0.000 n=12+12)
    mvdan committed Sep 7, 2021
    Copy the full SHA
    373e255 View commit details
    Browse the repository at this point in the history