Skip to content

Commit

Permalink
Write chunks of Enumerable bodies in a single system call
Browse files Browse the repository at this point in the history
Concatenate the header and body prior to the write call. I believe most Rubies'
String implementation will do this efficiently (paricularly with <<) and without
excess copying.

Fewer system calls should be more efficient.
  • Loading branch information
tarcieri committed Jan 10, 2016
1 parent b7a8103 commit a2d3269
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/http/request/writer.rb
Expand Up @@ -68,21 +68,20 @@ def join_headers
def send_request
headers = join_headers

# It's important to send the request in a single write call when
# possible in order to play nicely with Nagle's algorithm. Making
# two writes in a row triggers a pathological case where Nagle is
# expecting a third write that never happens.
case @body
when NilClass
write(headers)
when String
# It's important to send the request in a single write call when
# possible in order to play nicely with Nagle's algorithm. Making
# two writes in a row triggers a pathological case where Nagle is
# expecting a third write that never happens.
write(headers << @body)
when Enumerable
write(headers)

@body.each do |chunk|
write(chunk.bytesize.to_s(16) << CRLF)
write(chunk << CRLF)
write(chunk.bytesize.to_s(16) << CRLF << chunk << CRLF)
end

write(CHUNKED_END)
Expand Down

0 comments on commit a2d3269

Please sign in to comment.