Skip to content

Commit

Permalink
feat: reduce string allocation for decompression
Browse files Browse the repository at this point in the history
  • Loading branch information
SpringMT committed May 24, 2024
1 parent 16cb3a3 commit b8cf5fa
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions ext/zstdruby/zstdruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,22 @@ static VALUE rb_compress_using_dict(int argc, VALUE *argv, VALUE self)
static VALUE decompress_buffered(ZSTD_DCtx* dctx, const char* input_data, size_t input_size)
{
ZSTD_inBuffer input = { input_data, input_size, 0 };
VALUE result = rb_str_new(0, 0);

size_t const buffOutSize = ZSTD_DStreamOutSize();
VALUE output_string = rb_str_buf_new(buffOutSize);
ZSTD_outBuffer output = { RSTRING_PTR(output_string), buffOutSize, 0 };
while (input.pos < input.size) {
ZSTD_outBuffer output = { NULL, 0, 0 };
output.size += ZSTD_DStreamOutSize();
VALUE output_string = rb_str_new(NULL, output.size);
output.dst = RSTRING_PTR(output_string);

size_t ret = zstd_stream_decompress(dctx, &output, &input, false);
if (ZSTD_isError(ret)) {
ZSTD_freeDCtx(dctx);
rb_raise(rb_eRuntimeError, "%s: %s", "ZSTD_decompressStream failed", ZSTD_getErrorName(ret));
}
rb_str_cat(result, output.dst, output.pos);
rb_str_modify_expand(output_string, buffOutSize);
output.dst = RSTRING_PTR(output_string) + output.size;
output.size += buffOutSize;
}
rb_str_set_len(output_string, output.pos);
ZSTD_freeDCtx(dctx);
return result;
return output_string;
}

static VALUE rb_decompress(int argc, VALUE *argv, VALUE self)
Expand Down

0 comments on commit b8cf5fa

Please sign in to comment.