-
Notifications
You must be signed in to change notification settings - Fork 108
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
Reset bytes.Buffer before returning it to sync.Pool #448
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -335,6 +335,11 @@ func (gr *reader) cacheWithReader(ctx context.Context, currentDepth int, eg *err | |
return | ||
} | ||
|
||
func (gr *reader) putBuffer(b *bytes.Buffer) { | ||
b.Reset() | ||
gr.bufPool.Put(b) | ||
} | ||
|
||
type file struct { | ||
name string | ||
digest string | ||
|
@@ -406,11 +411,10 @@ func (sf *file) ReadAt(p []byte, offset int64) (int, error) { | |
|
||
// Use temporally buffer for aligning this chunk | ||
b := sf.gr.bufPool.Get().(*bytes.Buffer) | ||
b.Reset() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we remain There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, but would it have real effects? Get() returns either a new Buffer, which is safe to use according to https://cs.opensource.google/go/go/+/refs/tags/go1.17:src/bytes/buffer.go;l=449
or a buffer which is put by putBuffer(). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I think you are right. Merging. |
||
b.Grow(int(ce.ChunkSize)) | ||
ip := b.Bytes()[:ce.ChunkSize] | ||
if _, err := sf.ra.ReadAt(ip, ce.ChunkOffset); err != nil && err != io.EOF { | ||
sf.gr.bufPool.Put(b) | ||
sf.gr.putBuffer(b) | ||
return 0, errors.Wrap(err, "failed to read data") | ||
} | ||
|
||
|
@@ -421,7 +425,7 @@ func (sf *file) ReadAt(p []byte, offset int64) (int, error) { | |
|
||
// Verify this chunk | ||
if err := sf.verify(ip, ce); err != nil { | ||
sf.gr.bufPool.Put(b) | ||
sf.gr.putBuffer(b) | ||
return 0, errors.Wrap(err, "invalid chunk") | ||
} | ||
|
||
|
@@ -435,7 +439,7 @@ func (sf *file) ReadAt(p []byte, offset int64) (int, error) { | |
w.Close() | ||
} | ||
n := copy(p[nr:], ip[lowerDiscard:ce.ChunkSize-upperDiscard]) | ||
sf.gr.bufPool.Put(b) | ||
sf.gr.putBuffer(b) | ||
if int64(n) != expectedSize { | ||
return 0, fmt.Errorf("unexpected final data size %d; want %d", n, expectedSize) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that calling Reset() is the safest but may not be the most efficient solution.
Go's standard library has some more sophisticated examples.
https://github.com/golang/go/blob/9e7bc80b31088dc62faf4776ffdb1a2e27afa94e/src/os/dir_unix.go#L30-L36
https://github.com/golang/go/blob/acc2957bc9873ab7e65942045830a3dc0592eda2/src/net/http/server.go#L794-L850