Skip to content

Commit 5146a1b

Browse files
authored
Merge pull request #127 from Watson1978/performance
Improve Zstd.decompress performance by avoiding unnecessary memory copy
2 parents f5aa756 + bf38e83 commit 5146a1b

File tree

1 file changed

+1
-6
lines changed

1 file changed

+1
-6
lines changed

ext/zstdruby/zstdruby.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ static VALUE rb_decompress(int argc, VALUE *argv, VALUE self)
7878
StringValue(input_value);
7979

8080
size_t in_size = RSTRING_LEN(input_value);
81-
const unsigned char *in_r = (const unsigned char *)RSTRING_PTR(input_value);
82-
unsigned char *in = ALLOC_N(unsigned char, in_size);
83-
memcpy(in, in_r, in_size);
81+
const unsigned char *in = (const unsigned char *)RSTRING_PTR(input_value);
8482

8583
size_t off = 0;
8684
const uint32_t ZSTD_MAGIC = 0xFD2FB528U;
@@ -107,22 +105,19 @@ static VALUE rb_decompress(int argc, VALUE *argv, VALUE self)
107105
if (magic == ZSTD_MAGIC) {
108106
ZSTD_DCtx *dctx = ZSTD_createDCtx();
109107
if (!dctx) {
110-
xfree(in);
111108
rb_raise(rb_eRuntimeError, "ZSTD_createDCtx failed");
112109
}
113110

114111
VALUE out = decode_one_frame(dctx, in + off, in_size - off, kwargs);
115112

116113
ZSTD_freeDCtx(dctx);
117-
xfree(in);
118114
RB_GC_GUARD(input_value);
119115
return out;
120116
}
121117

122118
off += 1;
123119
}
124120

125-
xfree(in);
126121
RB_GC_GUARD(input_value);
127122
rb_raise(rb_eRuntimeError, "not a zstd frame (magic not found)");
128123
}

0 commit comments

Comments
 (0)