Skip to content

Commit

Permalink
fix: actually use the size hint in util_windows.go
Browse files Browse the repository at this point in the history
This code was broken, it computed the size hint to do nothing with it.
  • Loading branch information
Jorropo committed Oct 15, 2023
1 parent 5166704 commit 2f66973
Showing 1 changed file with 15 additions and 11 deletions.
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 @@ package flatfs

import (
"bytes"
"io"
"math"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -83,19 +83,23 @@ func readFileOnce(filename string) ([]byte, error) {
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
}

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

0 comments on commit 2f66973

Please sign in to comment.