http_spew RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: http_spew-public@bogomips.org
Subject: [PATCH 2/2] explicitly clear large buf when it is obviously safe
Date: Sat,  5 Nov 2016 02:07:37 +0000	[thread overview]
Message-ID: <20161105020737.1616-2-e@80x24.org> (raw)
In-Reply-To: <20161105020737.1616-1-e@80x24.org>

We can reduce memory pressure on the GC by explicitly
calling String#clear after we're done with a buffer.
This will hopefully reduce malloc fragmentation as
well as improving locality.
---
 lib/http_spew/content_md5.rb | 1 +
 lib/http_spew/input_spray.rb | 1 +
 lib/http_spew/request.rb     | 4 +++-
 3 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/lib/http_spew/content_md5.rb b/lib/http_spew/content_md5.rb
index ed68523..153ad86 100644
--- a/lib/http_spew/content_md5.rb
+++ b/lib/http_spew/content_md5.rb
@@ -42,6 +42,7 @@ class HTTP_Spew::ContentMD5
             digest.update(buf)
             wr.write(buf << "\r\n".freeze)
           end while input.read(0x4000, buf)
+          buf.clear
         end
         if expect_len && expect_len.to_i != @bytes_digested
           raise HTTP_Spew::LengthError,
diff --git a/lib/http_spew/input_spray.rb b/lib/http_spew/input_spray.rb
index a12d569..dfd23c4 100644
--- a/lib/http_spew/input_spray.rb
+++ b/lib/http_spew/input_spray.rb
@@ -35,6 +35,7 @@ class HTTP_Spew::InputSpray
           @pipes.delete_if { |rd, wr| write_fail?(rd, wr, buf) }.empty? and
             raise HTTP_Spew::NoWritersError, "all writers have died", []
         end
+        buf.clear
       rescue => e
         @pipes.each { |rd, _| rd.error = e }
       ensure
diff --git a/lib/http_spew/request.rb b/lib/http_spew/request.rb
index 3c3ef7e..1d02a98 100644
--- a/lib/http_spew/request.rb
+++ b/lib/http_spew/request.rb
@@ -38,7 +38,7 @@ class HTTP_Spew::Request
   def resume
     if @buf
       case rv = @to_io.kgio_trywrite(@buf)
-      when String
+      when String # unlikely
         @buf = rv # loop retry, socket buffer could've expanded
       when Symbol
         return rv
@@ -60,6 +60,7 @@ class HTTP_Spew::Request
     if @input
       @to_io.write(buf) while @input.read(0x4000, buf)
     end
+    buf.clear
     timeout -= (Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0)
     while :wait_readable == (rv = read_response) && timeout >= 0.0
       t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
@@ -104,6 +105,7 @@ class HTTP_Spew::Request
     while @to_io.kgio_read(0x4000, buf)
       yield buf
     end
+    buf.clear
   end
 
   # Used internally by various HTTP_Spew elements to report errors
-- 
EW


      reply	other threads:[~2016-11-05  2:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-05  2:07 [PATCH 1/2] request: drop to_path support Eric Wong
2016-11-05  2:07 ` Eric Wong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/http_spew/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161105020737.1616-2-e@80x24.org \
    --to=e@80x24.org \
    --cc=http_spew-public@bogomips.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/http_spew.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox