diff options
author | Jille Timmermans <jille@quis.cx> | 2023-12-02 18:59:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-02 18:59:46 +0100 |
commit | 0caa3b9bc135892f8c9a2c79d217e28675190a32 (patch) | |
tree | bd5f00a4fed9c85b0170a395d8c0eb9400c67247 | |
parent | copyZeroAlloc: Try WriteTo and ReadFrom before acquiring a buffer (#1673) (diff) | |
download | fasthttp-0caa3b9bc135892f8c9a2c79d217e28675190a32.tar.gz fasthttp-0caa3b9bc135892f8c9a2c79d217e28675190a32.tar.bz2 fasthttp-0caa3b9bc135892f8c9a2c79d217e28675190a32.zip |
writeBodyFixedSize: Only do an early flush if the reader is an *os.File (#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.
-rw-r--r-- | http.go | 17 |
1 files changed, 13 insertions, 4 deletions
@@ -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 + } } } |