Reduce the use of unnecessary buffers around cache #294
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Though we currently use
bytes.Buffers in many places around the cache, some of these uses seem to be able to reduce.This commit reduces the use of buffers around the cache, which leads to reduce memory usage of
containerd-stargz-grpc.We are currently using buffer during
cache.Add()but we can reduce it by providing direct access to*os.Fileof each cache file to the cache client.stargz-snapshotter/cache/cache.go
Lines 226 to 229 in 17b648d
This commit modifies the interface to provide the writer of that file directly to the client when adding contents to the cache.
Another buffer we can reduce is the one in
blob.ReadAt(). We are using this buffer for trimming the data got from the registry. Thus we are buffering the downloaded data first into this buffer then trimming off the unnecessary range.stargz-snapshotter/fs/remote/blob.go
Lines 204 to 209 in 17b648d
But instead, we can remove this by just discarding the data out of the necessary range.
Both of the above buffers are on the code path of prefetch and background-fetch, where the entire layer blob is cached and loaded to/from the cache. So the above fixes reduced the memory usage, especially during
rpull.Memory usage during
ctr-remote i rpull ghcr.io/stargz-containers/python:3.7-esgz