From: Matt Joiner Date: Tue, 19 Aug 2025 07:44:37 +0000 (+1000) Subject: Specialize the limited write for hashing in file storage X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=eb0897c01dcbdd211ae5142e3306969bf18bae97;p=btrtrc.git Specialize the limited write for hashing in file storage --- diff --git a/storage/file-io-classic.go b/storage/file-io-classic.go index 5c5ea7a6..56c5e9e1 100644 --- a/storage/file-io-classic.go +++ b/storage/file-io-classic.go @@ -28,6 +28,14 @@ type classicFileReader struct { *os.File } +func (c classicFileReader) writeToN(w io.Writer, n int64) (written int64, err error) { + lw := limitWriter{ + rem: n, + w: w, + } + return c.File.WriteTo(&lw) +} + func (c classicFileReader) seekDataOrEof(offset int64) (ret int64, err error) { ret, err = seekData(c.File, offset) if err == io.EOF { diff --git a/storage/file-io-mmap.go b/storage/file-io-mmap.go index 3e1d1277..ec060712 100644 --- a/storage/file-io-mmap.go +++ b/storage/file-io-mmap.go @@ -210,6 +210,19 @@ func (me *mmapFileHandle) WriteTo(w io.Writer) (n int64, err error) { return } +func (me *mmapFileHandle) writeToN(w io.Writer, n int64) (written int64, err error) { + b := me.shared.f.m + if me.pos >= int64(len(b)) { + return + } + b = b[me.pos:] + b = b[:min(int64(len(b)), n)] + i, err := w.Write(b) + written = int64(i) + me.pos += written + return +} + func (me *mmapFileHandle) Close() error { return me.shared.Close() } diff --git a/storage/file-io.go b/storage/file-io.go index 0f86f0f6..437ccc95 100644 --- a/storage/file-io.go +++ b/storage/file-io.go @@ -12,7 +12,7 @@ type fileWriter interface { type fileReader interface { // Seeks to the next data in the file. If there is no more data, seeks to the end of the file. seekDataOrEof(offset int64) (ret int64, err error) - io.WriterTo + writeToN(w io.Writer, n int64) (written int64, err error) io.ReadCloser } diff --git a/storage/file-piece.go b/storage/file-piece.go index 5173bb79..f9b73c25 100644 --- a/storage/file-piece.go +++ b/storage/file-piece.go @@ -376,19 +376,7 @@ func (me *filePieceImpl) writeFileTo(w io.Writer, fileIndex int, extent segments panicif.NotEq(n1, n) extentRemaining -= n1 } - var n1 int64 - if true { - n1, err = f.WriteTo(&limitWriter{ - rem: extentRemaining, - w: w, - }) - // limitWriter will block f from writing too much. - if n1 == extentRemaining { - err = nil - } - } else { - n1, err = io.CopyN(w, f, extentRemaining) - } + n1, err := f.writeToN(w, extentRemaining) packageExpvarMap.Add("bytesReadNotSkipped", n1) written += n1 return