ruby-changes:51781
From: normal <ko1@a...>
Date: Wed, 18 Jul 2018 17:16:28 +0900 (JST)
Subject: [ruby-changes:51781] normal:r63993 (trunk): zlib (rb_gzreader_getc): localize and return cbuf directly
normal 2018-07-18 17:16:18 +0900 (Wed, 18 Jul 2018) New Revision: 63993 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63993 Log: zlib (rb_gzreader_getc): localize and return cbuf directly No point in having a long-lived cbuf in "struct gzfile" since GZFILE_CBUF_CAPA is smaller than RSTRING_EMBED_LEN_MAX (even on 32-bit). We can also have rb_econv_convert write directly to the return value instead of an intermediate buffer. This brings "struct gzfile" from 264 to 256 bytes on 64-bit systems to avoid taking an additional cache line. Modified files: trunk/ext/zlib/zlib.c Index: ext/zlib/zlib.c =================================================================== --- ext/zlib/zlib.c (revision 63992) +++ ext/zlib/zlib.c (revision 63993) @@ -2225,7 +2225,6 @@ struct gzfile { https://github.com/ruby/ruby/blob/trunk/ext/zlib/zlib.c#L2225 rb_encoding *enc2; rb_econv_t *ec; VALUE ecopts; - char *cbuf; VALUE path; }; #define GZFILE_CBUF_CAPA 10 @@ -2275,22 +2274,13 @@ gzfile_free(void *p) https://github.com/ruby/ruby/blob/trunk/ext/zlib/zlib.c#L2274 } zstream_finalize(z); } - if (gz->cbuf) { - xfree(gz->cbuf); - } xfree(gz); } static size_t gzfile_memsize(const void *p) { - const struct gzfile *gz = p; - size_t size = sizeof(struct gzfile); - - if (gz->cbuf) - size += GZFILE_CBUF_CAPA; - - return size; + return sizeof(struct gzfile); } static const rb_data_type_t gzfile_data_type = { @@ -2319,7 +2309,6 @@ gzfile_init(struct gzfile *gz, const str https://github.com/ruby/ruby/blob/trunk/ext/zlib/zlib.c#L2309 gz->ec = NULL; gz->ecflags = 0; gz->ecopts = Qnil; - gz->cbuf = 0; gz->path = Qnil; } @@ -2870,22 +2859,19 @@ gzfile_getc(struct gzfile *gz) https://github.com/ruby/ruby/blob/trunk/ext/zlib/zlib.c#L2859 if (gz->ec && rb_enc_dummy_p(gz->enc2)) { const unsigned char *ss, *sp, *se; unsigned char *ds, *dp, *de; + VALUE cbuf = rb_enc_str_new(0, GZFILE_CBUF_CAPA, gz->enc); - if (!gz->cbuf) { - gz->cbuf = ALLOC_N(char, GZFILE_CBUF_CAPA); - } ss = sp = (const unsigned char*)RSTRING_PTR(gz->z.buf); se = sp + ZSTREAM_BUF_FILLED(&gz->z); - ds = dp = (unsigned char *)gz->cbuf; + ds = dp = (unsigned char *)RSTRING_PTR(cbuf); de = (unsigned char *)ds + GZFILE_CBUF_CAPA; (void)rb_econv_convert(gz->ec, &sp, se, &dp, de, ECONV_PARTIAL_INPUT|ECONV_AFTER_OUTPUT); rb_econv_check_error(gz->ec); dst = zstream_shift_buffer(&gz->z, sp - ss); gzfile_calc_crc(gz, dst); - dst = rb_str_new(gz->cbuf, dp - ds); - rb_enc_associate(dst, gz->enc); - OBJ_TAINT(dst); - return dst; + rb_str_resize(cbuf, dp - ds); + OBJ_TAINT(cbuf); + return cbuf; } else { buf = gz->z.buf; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/