Skip to content

Commit e754f09

Browse files
committed
feat: unlock GVL for decompress
1 parent 3be703a commit e754f09

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

ext/zstdruby/common.h

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,6 @@ static int convert_compression_level(VALUE compression_level_value)
1515
return NUM2INT(compression_level_value);
1616
}
1717

18-
struct compress_params {
19-
ZSTD_CCtx* ctx;
20-
ZSTD_outBuffer* output;
21-
ZSTD_inBuffer* input;
22-
ZSTD_EndDirective endOp;
23-
size_t ret;
24-
};
25-
26-
static void* compress_wrapper(void* args)
27-
{
28-
struct compress_params* params = args;
29-
params->ret = ZSTD_compressStream2(params->ctx, params->output, params->input, params->endOp);
30-
return NULL;
31-
}
32-
33-
static size_t zstd_compress(ZSTD_CCtx* const ctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input, ZSTD_EndDirective endOp)
34-
{
35-
#ifdef HAVE_RUBY_THREAD_H
36-
struct compress_params params = { ctx, output, input, endOp };
37-
rb_thread_call_without_gvl(compress_wrapper, &params, NULL, NULL);
38-
return params.ret;
39-
#else
40-
return ZSTD_compressStream2(ctx, output, input, endOp);
41-
#endif
42-
}
43-
4418
static void set_compress_params(ZSTD_CCtx* const ctx, VALUE level_from_args, VALUE kwargs)
4519
{
4620
ID kwargs_keys[2];
@@ -69,28 +43,29 @@ static void set_compress_params(ZSTD_CCtx* const ctx, VALUE level_from_args, VAL
6943
}
7044
}
7145

72-
struct decompress_params {
73-
ZSTD_DCtx* dctx;
46+
struct compress_params {
47+
ZSTD_CCtx* ctx;
7448
ZSTD_outBuffer* output;
7549
ZSTD_inBuffer* input;
50+
ZSTD_EndDirective endOp;
7651
size_t ret;
7752
};
7853

79-
static void* decompress_wrapper(void* args)
54+
static void* compress_wrapper(void* args)
8055
{
81-
struct decompress_params* params = args;
82-
params->ret = ZSTD_decompressStream(params->dctx, params->output, params->input);
56+
struct compress_params* params = args;
57+
params->ret = ZSTD_compressStream2(params->ctx, params->output, params->input, params->endOp);
8358
return NULL;
8459
}
8560

86-
static size_t zstd_decompress(ZSTD_DCtx* const dctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
61+
static size_t zstd_compress(ZSTD_CCtx* const ctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input, ZSTD_EndDirective endOp)
8762
{
8863
#ifdef HAVE_RUBY_THREAD_H
89-
struct decompress_params params = { dctx, output, input };
90-
rb_thread_call_without_gvl(decompress_wrapper, &params, NULL, NULL);
64+
struct compress_params params = { ctx, output, input, endOp };
65+
rb_thread_call_without_gvl(compress_wrapper, &params, NULL, NULL);
9166
return params.ret;
9267
#else
93-
return ZSTD_decompressStream(dctx, output, input);
68+
return ZSTD_compressStream2(ctx, output, input, endOp);
9469
#endif
9570
}
9671

@@ -112,4 +87,29 @@ static void set_decompress_params(ZSTD_DCtx* const dctx, VALUE kwargs)
11287
}
11388
}
11489

90+
struct decompress_params {
91+
ZSTD_DCtx* dctx;
92+
ZSTD_outBuffer* output;
93+
ZSTD_inBuffer* input;
94+
size_t ret;
95+
};
96+
97+
static void* decompress_wrapper(void* args)
98+
{
99+
struct decompress_params* params = args;
100+
params->ret = ZSTD_decompressStream(params->dctx, params->output, params->input);
101+
return NULL;
102+
}
103+
104+
static size_t zstd_decompress(ZSTD_DCtx* const dctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
105+
{
106+
#ifdef HAVE_RUBY_THREAD_H
107+
struct decompress_params params = { dctx, output, input };
108+
rb_thread_call_without_gvl(decompress_wrapper, &params, NULL, NULL);
109+
return params.ret;
110+
#else
111+
return ZSTD_decompressStream(dctx, output, input);
112+
#endif
113+
}
114+
115115
#endif /* ZSTD_RUBY_H */

ext/zstdruby/zstdruby.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static VALUE decompress_buffered(ZSTD_DCtx* dctx, const char* input_data, size_t
9999
rb_str_resize(output_string, output.size);
100100
output.dst = RSTRING_PTR(output_string);
101101

102-
size_t ret = ZSTD_decompressStream(dctx, &output, &input);
102+
size_t ret = zstd_decompress(dctx, &output, &input);
103103
if (ZSTD_isError(ret)) {
104104
ZSTD_freeDCtx(dctx);
105105
rb_raise(rb_eRuntimeError, "%s: %s", "ZSTD_decompressStream failed", ZSTD_getErrorName(ret));

0 commit comments

Comments
 (0)