From a2d3269c83fa206d06f817c910d95b77af478b5f Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sat, 9 Jan 2016 21:18:24 -0800 Subject: [PATCH] Write chunks of Enumerable bodies in a single system call 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. --- lib/http/request/writer.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/http/request/writer.rb b/lib/http/request/writer.rb index 5351a13e..7fa11531 100644 --- a/lib/http/request/writer.rb +++ b/lib/http/request/writer.rb @@ -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)