From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS24940 5.9.0.0/16 X-Spam-Status: No, score=-2.6 required=3.0 tests=BAYES_00,RCVD_IN_XBL,SPF_FAIL, SPF_HELO_FAIL shortcircuit=no autolearn=no autolearn_force=no version=3.4.0 Received: from 80x24.org (tor-relay.zwiebeltoralf.de [5.9.158.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 882CB203C1 for ; Sat, 5 Nov 2016 02:07:42 +0000 (UTC) From: Eric Wong 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 Message-Id: <20161105020737.1616-2-e@80x24.org> In-Reply-To: <20161105020737.1616-1-e@80x24.org> References: <20161105020737.1616-1-e@80x24.org> List-Id: 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