Skip to content

Commit

Permalink
writeBodyFixedSize: Only do an early flush if the reader is an *os.Fi…
Browse files Browse the repository at this point in the history
…le (#1674)

or an *io.LimitedReader of an *os.File (because that's also supported by
https://cs.opensource.google/go/go/+/refs/tags/go1.21.4:src/bufio/bufio.go;l=784)

I think that having to flush less often outweighs the overhead of the
extra check. The appended data is known to be large, but it might still
save us a syscall by allowing it to buffer more.
  • Loading branch information
Jille authored Dec 2, 2023
1 parent 2ac2a39 commit 0caa3b9
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2104,10 +2104,19 @@ func limitedReaderSize(r io.Reader) int64 {

func writeBodyFixedSize(w *bufio.Writer, r io.Reader, size int64) error {
if size > maxSmallFileSize {
// w buffer must be empty for triggering
// sendfile path in bufio.Writer.ReadFrom.
if err := w.Flush(); err != nil {
return err
earlyFlush := false
switch r := r.(type) {
case *os.File:
earlyFlush = true
case *io.LimitedReader:
_, earlyFlush = r.R.(*os.File)
}
if earlyFlush {
// w buffer must be empty for triggering
// sendfile path in bufio.Writer.ReadFrom.
if err := w.Flush(); err != nil {
return err
}
}
}

Expand Down

0 comments on commit 0caa3b9

Please sign in to comment.