1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/ext
normal a55abcc0ca zlib: reduce garbage on gzip writes (deflate)
Zlib::GzipWriter generated large amounts of garbage from
(struct zstream).input.  Reuse the .input field when it is
hidden, and recycle it when its lifetime is over.  This change
alone reduced memory usage of the writer from 90MB to 4.5MB.

For the detached buffer of compressed data used by
gzfile_write_raw, we can only clear the string (not recycle it)
since user code may hold references to it (but the data would be
clobbered, anyways).  This reduced memory usage slightly by
around 0.5MB (because it's smaller compressed data).

Combined, these changes reduce the anonymous RSS memory of a
dedicated writer process from over 90MB to under 4MB.

before:

    #      user     system      total        real

    writer   7.823332   0.053333   7.876665 (  7.881464)
    writer RssAnon:    92944 kB
    reader   6.969999   0.076666   7.046665 (  7.906377)
    reader RssAnon:   109820 kB

after:

    writer   7.359999   0.000000   7.359999 (  7.360639)
    writer RssAnon:     4040 kB
    reader   6.346667   0.070000   6.416667 (  7.387654)
    reader RssAnon:    98272 kB

Script used:
-------
require 'zlib'
require 'benchmark'
nr = 16384 * 2

def stats(pfx, bm)
  str = "#{bm}#{File.readlines("/proc/#$$/status").grep(/^RssAnon:/)[0]}"
  puts str.gsub!(/^/m, pfx)
end

rd, wr = IO.pipe
pid = fork do
  buf = ((0..255).map(&:chr).join * 128).freeze
  rd.close
  gzip = Zlib::GzipWriter.new(wr)
  bm = Benchmark.measure do
    nr.times { gzip.write(buf) }
    gzip.close
    wr.close
  end
  stats('writer ', bm)
end

wr.close
buf = ''
gunzip = Zlib::GzipReader.new(rd)
n = 0
bm = Benchmark.measure do
  begin
    gunzip.readpartial(16384, buf)
    n += buf.size
  rescue EOFError
    break
  end while true
end
stats('reader ', bm)
Process.waitall
-------
* ext/zlib/zlib.c (zstream_discard_input): reuse or recycle hidden input
  (zstream_reset_input): clear hidden input
  (zstream_run): detach input and recycle after use
  (gzfile_write_raw): clear buffer after write
  [ruby-core:84638] [Feature #14315]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61631 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-05 20:48:55 +00:00
..
-test- long long is a C99ism 2018-01-02 06:41:47 +00:00
bigdecimal flexible array member is a C99ism 2018-01-02 06:41:48 +00:00
cgi/escape cgi/util.rb: Don't escape tilde in #escape 2017-05-17 12:34:59 +00:00
continuation Update dependencies 2016-07-06 05:48:13 +00:00
coverage ext/coverage/coverage.c: remove COVERAGE_EXPERIMENTAL_MODE 2017-12-12 00:20:58 +00:00
date Bump version to date-1.0.0 as default gems. 2017-12-11 03:56:01 +00:00
dbm Bump dbm-1.0.0 for released version. 2017-12-06 08:32:01 +00:00
digest Remove digest.gemspec for default gems. 2017-12-19 02:43:09 +00:00
etc Bump version to etc-1.0.0 as default gems. 2017-12-13 05:52:47 +00:00
fcntl Bump version to fcntl-1.0.0 as default gems. 2017-12-11 06:03:53 +00:00
fiber Update dependencies 2016-07-06 05:48:13 +00:00
fiddle Bump fiddle-1.0.0 for released version. 2017-12-06 08:35:13 +00:00
gdbm Bump gdbm-2.0.0 for released versionn. 2017-12-06 08:34:34 +00:00
io gperf.sed: static declarations 2017-12-15 14:42:43 +00:00
json Merge json-2.1.0 from https://github.com/flori/json 2017-06-16 03:04:46 +00:00
nkf ext: check if null byte is contained 2017-09-30 08:35:23 +00:00
objspace node.h: remove NODE_PRELUDE 2018-01-05 08:59:23 +00:00
openssl openssl: import v2.1.0 2017-12-14 11:19:16 +00:00
pathname pathname.c: improve docs for Pathname 2017-11-07 20:14:46 +00:00
psych Merge psych-3.0.2 from ruby/psych. 2017-12-19 09:44:33 +00:00
pty Replace to_a[1..-1] on a MatchData with captures 2017-10-21 05:18:40 +00:00
racc/cparse Update dependencies 2016-07-06 05:48:13 +00:00
rbconfig/sizeof configure.ac 2017-09-29 13:21:17 +00:00
readline ext: check if null byte is contained 2017-09-30 08:35:23 +00:00
ripper Ripper::Lexer bit predicates 2017-12-13 10:26:09 +00:00
rubyvm get rid of test failures introduced at r56848 2016-11-20 06:19:05 +00:00
sdbm Bump version to sdbm-1.0.0 as default gems. 2017-12-11 06:48:09 +00:00
socket ifaddr.c: get_ifaddrs 2017-12-22 01:49:00 +00:00
stringio [DOC] StringIO#write accepts multiple arguments since r60377 [ci skip] 2017-12-22 03:35:54 +00:00
strscan Update version of strscan to 1.0.0. 2017-12-19 03:12:22 +00:00
syslog ext: check if null byte is contained 2017-09-30 08:35:23 +00:00
win32 Fixed misspelling words. 2017-10-22 11:27:06 +00:00
win32ole hide internal data objects 2017-12-04 07:37:21 +00:00
zlib zlib: reduce garbage on gzip writes (deflate) 2018-01-05 20:48:55 +00:00
.document ext/.document: follow-up r59734 2017-09-03 13:31:34 +00:00
extmk.rb ext/extmk.rb: colorize notes [Feature #13302] 2017-04-21 03:01:12 +00:00
Setup Removed deprecated extensions of mathn. 2017-04-20 08:21:24 +00:00
Setup.atheos * ext/tk: Tk is removed from stdlib. [Feature #8539] 2016-08-09 06:44:45 +00:00
Setup.nt * ext/tk: Tk is removed from stdlib. [Feature #8539] 2016-08-09 06:44:45 +00:00