aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jille Timmermans <jille@quis.cx> 2023-12-02 18:59:46 +0100
committerGravatar GitHub <noreply@github.com> 2023-12-02 18:59:46 +0100
commit0caa3b9bc135892f8c9a2c79d217e28675190a32 (patch)
treebd5f00a4fed9c85b0170a395d8c0eb9400c67247
parentcopyZeroAlloc: Try WriteTo and ReadFrom before acquiring a buffer (#1673) (diff)
downloadfasthttp-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.go17
1 files changed, 13 insertions, 4 deletions
diff --git a/http.go b/http.go
index 4f8718c..3b5747c 100644
--- a/http.go
+++ b/http.go
@@ -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
+ }
}
}