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

fix: actually use the size hint in util_windows.go #109

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions util_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import (
"bytes"
"io"
"math"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -83,19 +83,23 @@
defer f.Close()
// It's a good but not certain bet that FileInfo will tell us exactly how much to
// read, so let's try it but be prepared for the answer to be wrong.
var n int64 = bytes.MinRead
var sizeHint int = bytes.MinRead

if fi, err := f.Stat(); err == nil {
// As initial capacity for readAll, use Size + a little extra in case Size
// is zero, and to avoid another allocation after Read has filled the
// buffer. The readAll call will read into its allocated internal buffer
// cheaply. If the size was wrong, we'll either waste some space off the end
// or reallocate as needed, but in the overwhelmingly common case we'll get
// it just right.
if size := fi.Size() + bytes.MinRead; size > n {
n = size
if sz := fi.Size(); sz <= math.MaxInt {
if sz := int(sz); sz > sizeHint {
sizeHint = sz
}
}
sizeHint++ // one byte for final read at EOF
}

var buf bytes.Buffer
buf.Grow(sizeHint)
_, err = buf.ReadFrom(f)
if err != nil {
return nil, err

Check warning on line 101 in util_windows.go

View check run for this annotation

Codecov / codecov/patch

util_windows.go#L101

Added line #L101 was not covered by tests
}

return io.ReadAll(f)
return buf.Bytes(), nil
}