forked from microsoft/docker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve performance/reduce allocs of bytespipe
Creates a `fixedBuffer` type that is used to encapsulate functionality for reading/writing from the underlying byte slices. Uses lazily-loaded set of sync.Pools for storing buffers that are no longer needed so they can be re-used. ``` benchmark old ns/op new ns/op delta BenchmarkBytesPipeWrite-8 138469 48985 -64.62% BenchmarkBytesPipeRead-8 130922 56601 -56.77% benchmark old allocs new allocs delta BenchmarkBytesPipeWrite-8 18 8 -55.56% BenchmarkBytesPipeRead-8 0 0 +0.00% benchmark old bytes new bytes delta BenchmarkBytesPipeWrite-8 66903 1649 -97.54% BenchmarkBytesPipeRead-8 0 1 +Inf% ``` Signed-off-by: Brian Goff <cpuguy83@gmail.com>
- Loading branch information
Showing
5 changed files
with
209 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package ioutils | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
) | ||
|
||
var errBufferFull = errors.New("buffer is full") | ||
|
||
type fixedBuffer struct { | ||
buf []byte | ||
pos int | ||
lastRead int | ||
} | ||
|
||
func (b *fixedBuffer) Write(p []byte) (int, error) { | ||
n := copy(b.buf[b.pos:cap(b.buf)], p) | ||
b.pos += n | ||
|
||
if n < len(p) { | ||
if b.pos == cap(b.buf) { | ||
return n, errBufferFull | ||
} | ||
return n, io.ErrShortWrite | ||
} | ||
return n, nil | ||
} | ||
|
||
func (b *fixedBuffer) Read(p []byte) (int, error) { | ||
n := copy(p, b.buf[b.lastRead:b.pos]) | ||
b.lastRead += n | ||
return n, nil | ||
} | ||
|
||
func (b *fixedBuffer) Len() int { | ||
return b.pos - b.lastRead | ||
} | ||
|
||
func (b *fixedBuffer) Cap() int { | ||
return cap(b.buf) | ||
} | ||
|
||
func (b *fixedBuffer) Reset() { | ||
b.pos = 0 | ||
b.lastRead = 0 | ||
b.buf = b.buf[:0] | ||
} | ||
|
||
func (b *fixedBuffer) String() string { | ||
return string(b.buf[b.lastRead:b.pos]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package ioutils | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestFixedBufferWrite(t *testing.T) { | ||
buf := &fixedBuffer{buf: make([]byte, 0, 64)} | ||
n, err := buf.Write([]byte("hello")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if n != 5 { | ||
t.Fatalf("expected 5 bytes written, got %d", n) | ||
} | ||
|
||
if string(buf.buf[:5]) != "hello" { | ||
t.Fatalf("expected \"hello\", got %q", string(buf.buf[:5])) | ||
} | ||
|
||
n, err = buf.Write(bytes.Repeat([]byte{1}, 64)) | ||
if err != errBufferFull { | ||
t.Fatalf("expected errBufferFull, got %v - %v", err, buf.buf[:64]) | ||
} | ||
} | ||
|
||
func TestFixedBufferRead(t *testing.T) { | ||
buf := &fixedBuffer{buf: make([]byte, 0, 64)} | ||
if _, err := buf.Write([]byte("hello world")); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
b := make([]byte, 5) | ||
n, err := buf.Read(b) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if n != 5 { | ||
t.Fatalf("expected 5 bytes read, got %d - %s", n, buf.String()) | ||
} | ||
|
||
if string(b) != "hello" { | ||
t.Fatalf("expected \"hello\", got %q", string(b)) | ||
} | ||
|
||
n, err = buf.Read(b) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if n != 5 { | ||
t.Fatalf("expected 5 bytes read, got %d", n) | ||
} | ||
|
||
if string(b) != " worl" { | ||
t.Fatalf("expected \" worl\", got %s", string(b)) | ||
} | ||
|
||
b = b[:1] | ||
n, err = buf.Read(b) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if n != 1 { | ||
t.Fatalf("expected 1 byte read, got %d - %s", n, buf.String()) | ||
} | ||
|
||
if string(b) != "d" { | ||
t.Fatalf("expected \"d\", got %s", string(b)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.