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

Reset bytes.Buffer before returning it to sync.Pool #448

Merged
merged 1 commit into from Sep 8, 2021

Conversation

kzys
Copy link
Member

@kzys kzys commented Sep 3, 2021

Most of bytes.Buffer in stargz-snapshotter is unbounded in terms of
size and sync.Pool's Get may choose to ignore the pool.

Resizing a buffer before returning to the pool may alleviate the
snapshotter's memory utilization issue.

golang/go#23199 has a long discussion about
the issue.

Signed-off-by: Kazuyoshi Kato katokazu@amazon.com

Most of bytes.Buffer in stargz-snapshotter is unbounded in terms of
size and sync.Pool's Get may choose to ignore the pool.

Resizing a buffer before returning to the pool may alleviate the
snapshotter's memory utilization issue.

golang/go#23199 has a long discussion about
the issue.

Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
return nil
},
}

return memW, nil
}

func (dc *directoryCache) putBuffer(b *bytes.Buffer) {
b.Reset()
Copy link
Member Author

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

@@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we remain Reset before use for safety?

Copy link
Member Author

Choose a reason for hiding this comment

The 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

In most cases, new(Buffer) (or just declaring a Buffer variable) is sufficient to initialize a Buffer.

or a buffer which is put by putBuffer().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I think you are right. Merging.

@ktock ktock merged commit 1d81483 into containerd:main Sep 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants