Skip to content

Commit

Permalink
java: grow buffers to the right capacity before use
Browse files Browse the repository at this point in the history
avoids reallocations when buffers grow repeatedly for large files, and oversizing (as buffer capacity doubles when capacity is reached)

Signed-off-by: Mark Frost <[email protected]>
  • Loading branch information
frostmar committed Jan 4, 2024
1 parent 07ae3f0 commit a020a5e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions java/jar/jar.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ func extractInner(ctx context.Context, p srcPath, z *zip.Reader) ([]Info, error)
}
defer rc.Close()
buf.Reset()
buf.Grow(int(fi.Size()))
h.Reset()
sz, err := buf.ReadFrom(io.TeeReader(rc, h))
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions java/packagescanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ func (s *Scanner) Scan(ctx context.Context, layer *claircore.Layer) ([]*claircor
if err != nil {
return nil, err
}
fStat, err := f.Stat()
if err == nil {
buf.Grow(int(fStat.Size()))
}
sz, err := buf.ReadFrom(io.TeeReader(f, sh))
f.Close()
if err != nil {
Expand Down

0 comments on commit a020a5e

Please sign in to comment.