• Albert Nigmatzianov's avatar
    io: Improve performance of CopyN · 098eb016
    Albert Nigmatzianov authored
    Benchmarks:
    name          old time/op    new time/op    delta
    CopyNSmall-4    5.09µs ± 1%    2.25µs ±86%  -55.91%  (p=0.000 n=11+14)
    CopyNLarge-4     114µs ±73%     121µs ±72%     ~     (p=0.701 n=14+14)
    
    name          old alloc/op   new alloc/op   delta
    CopyNSmall-4    34.6kB ± 0%     1.9kB ±19%  -94.60%  (p=0.000 n=12+14)
    CopyNLarge-4     129kB ± 8%     127kB ±18%   -2.00%  (p=0.007 n=14+14)
    
    name          old allocs/op  new allocs/op  delta
    CopyNSmall-4      2.00 ± 0%      1.00 ± 0%  -50.00%  (p=0.000 n=14+14)
    CopyNLarge-4      2.00 ± 0%      1.00 ± 0%  -50.00%  (p=0.000 n=14+14)
    
    Benchmark code:
    type Buffer struct {
    	bytes.Buffer
    	io.ReaderFrom
    }
    
    func BenchmarkCopyNSmall(b *testing.B) {
    	bs := bytes.Repeat([]byte{0}, 1024)
    	rd := bytes.NewReader(bs)
    	buf := new(Buffer)
    	b.ResetTimer()
    
    	for i := 0; i < b.N; i++ {
    		io.CopyN(buf, rd, 512)
    		rd.Reset(bs)
    	}
    }
    
    func BenchmarkCopyNLarge(b *testing.B) {
    	bs := bytes.Repeat([]byte{0}, 64*1024)
    	rd := bytes.NewReader(bs)
    	buf := new(Buffer)
    	b.ResetTimer()
    
    	for i := 0; i < b.N; i++ {
    		io.CopyN(buf, rd, (32*1024)+1)
    		rd.Reset(bs)
    	}
    }
    
    Change-Id: Id8d29e55758452c870cf372db640f07baec05849
    Reviewed-on: https://go-review.googlesource.com/60630
    Run-TryBot: Ian Lance Taylor <iant@golang.org>
    TryBot-Result: Gobot Gobot <gobot@golang.org>
    Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
    098eb016
io.go 17.5 KB