Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement io.ReaderFrom for gin.ResponseWriter #3197

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aaronjanse
Copy link

@aaronjanse aaronjanse commented Jun 13, 2022

Follow-up to fix the merge conflict in #1610. Credit to @heyimalex (marked as a co-author in Git) for writing the actual code involved.

This causes io.Copy, which calls io.copyBuffer, to automatically use the sendfile syscall by calling the underlying http.ResponseWriter's ReadFrom implementation [1].

[1] https://github.com/golang/go/blob/7eeec1f6e4/src/io/io.go#L410-L413

This can be a significant performance improvement. For example, downloading a 1 GB file is now 33% faster:

# After this patch
$ hyperfine --warmup 2 "curl 'http://localhost:8080/local/file' -o /dev/null"
Benchmark 1: curl 'http://localhost:8080/local/file' -o /dev/null
  Time (mean ± σ):     338.6 ms ±   7.8 ms    [User: 46.9 ms, System: 288.6 ms]
  Range (min … max):   323.0 ms … 348.4 ms    10 runs

# Before this patch
$ hyperfine --warmup 2 "curl 'http://localhost:8080/local/file' -o /dev/null"
Benchmark 1: curl 'http://localhost:8080/local/file' -o /dev/null
  Time (mean ± σ):     509.9 ms ±  17.3 ms    [User: 82.2 ms, System: 282.3 ms]
  Range (min … max):   489.0 ms … 540.7 ms    10 runs

This causes io.Copy, which calls io.copyBuffer, to automatically use the
sendfile syscall by calling the underlying http.ResponseWriter's
ReadFrom implementation [1]. This can be a significant performance improvement.

[1] https://github.com/golang/go/blob/7eeec1f6e4/src/io/io.go#L410-L413

Co-authored-by: Alex Guerra <alex@heyimalex.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant