Skip to content

Commit

Permalink
pkg/cache: use a shared buffer to limit allocations
Browse files Browse the repository at this point in the history
Previously, new buffers were allocated on each file we read in, which
was unnecessary and wasteful.

Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
  • Loading branch information
stevekuznetsov committed Jul 27, 2023
1 parent bca2bfb commit 2796254
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
5 changes: 3 additions & 2 deletions pkg/cache/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,14 @@ func (q *JSON) existingDigest() (string, error) {
}

func (q *JSON) computeDigest(fbcFsys fs.FS) (string, error) {
buf := make([]byte, 32*1024)
computedHasher := fnv.New64a()
if err := fsToTar(computedHasher, fbcFsys); err != nil {
if err := fsToTar(computedHasher, fbcFsys, buf); err != nil {
return "", err
}

if cacheFS, err := fs.Sub(os.DirFS(q.baseDir), jsonDir); err == nil {
if err := fsToTar(computedHasher, cacheFS); err != nil && !errors.Is(err, os.ErrNotExist) {
if err := fsToTar(computedHasher, cacheFS, buf); err != nil && !errors.Is(err, os.ErrNotExist) {
return "", fmt.Errorf("compute hash: %v", err)
}
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/cache/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
// This function unsets user and group information in the tar archive so that readers
// of archives produced by this function do not need to account for differences in
// permissions between source and destination filesystems.
func fsToTar(w io.Writer, fsys fs.FS) error {
func fsToTar(w io.Writer, fsys fs.FS, buf []byte) error {
if buf == nil || len(buf) == 0 {
buf = make([]byte, 32*1024)
}
tw := tar.NewWriter(w)
if err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
Expand Down Expand Up @@ -52,7 +55,7 @@ func fsToTar(w io.Writer, fsys fs.FS) error {
return fmt.Errorf("open file %q: %v", path, err)
}
defer f.Close()
if _, err := io.Copy(tw, f); err != nil {
if _, err := io.CopyBuffer(tw, f, buf); err != nil {
return fmt.Errorf("write tar data for %q: %v", path, err)
}
return nil
Expand Down

0 comments on commit 2796254

Please sign in to comment.