Skip to content

Commit

Permalink
feat: unlock GVL for decompress
Browse files Browse the repository at this point in the history
  • Loading branch information
SpringMT committed Apr 13, 2024
1 parent 3be703a commit e754f09
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
70 changes: 35 additions & 35 deletions ext/zstdruby/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,6 @@ static int convert_compression_level(VALUE compression_level_value)
return NUM2INT(compression_level_value);
}

struct compress_params {
ZSTD_CCtx* ctx;
ZSTD_outBuffer* output;
ZSTD_inBuffer* input;
ZSTD_EndDirective endOp;
size_t ret;
};

static void* compress_wrapper(void* args)
{
struct compress_params* params = args;
params->ret = ZSTD_compressStream2(params->ctx, params->output, params->input, params->endOp);
return NULL;
}

static size_t zstd_compress(ZSTD_CCtx* const ctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input, ZSTD_EndDirective endOp)
{
#ifdef HAVE_RUBY_THREAD_H
struct compress_params params = { ctx, output, input, endOp };
rb_thread_call_without_gvl(compress_wrapper, &params, NULL, NULL);
return params.ret;
#else
return ZSTD_compressStream2(ctx, output, input, endOp);
#endif
}

static void set_compress_params(ZSTD_CCtx* const ctx, VALUE level_from_args, VALUE kwargs)
{
ID kwargs_keys[2];
Expand Down Expand Up @@ -69,28 +43,29 @@ static void set_compress_params(ZSTD_CCtx* const ctx, VALUE level_from_args, VAL
}
}

struct decompress_params {
ZSTD_DCtx* dctx;
struct compress_params {
ZSTD_CCtx* ctx;
ZSTD_outBuffer* output;
ZSTD_inBuffer* input;
ZSTD_EndDirective endOp;
size_t ret;
};

static void* decompress_wrapper(void* args)
static void* compress_wrapper(void* args)
{
struct decompress_params* params = args;
params->ret = ZSTD_decompressStream(params->dctx, params->output, params->input);
struct compress_params* params = args;
params->ret = ZSTD_compressStream2(params->ctx, params->output, params->input, params->endOp);
return NULL;
}

static size_t zstd_decompress(ZSTD_DCtx* const dctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
static size_t zstd_compress(ZSTD_CCtx* const ctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input, ZSTD_EndDirective endOp)
{
#ifdef HAVE_RUBY_THREAD_H
struct decompress_params params = { dctx, output, input };
rb_thread_call_without_gvl(decompress_wrapper, &params, NULL, NULL);
struct compress_params params = { ctx, output, input, endOp };
rb_thread_call_without_gvl(compress_wrapper, &params, NULL, NULL);
return params.ret;
#else
return ZSTD_decompressStream(dctx, output, input);
return ZSTD_compressStream2(ctx, output, input, endOp);
#endif
}

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

struct decompress_params {
ZSTD_DCtx* dctx;
ZSTD_outBuffer* output;
ZSTD_inBuffer* input;
size_t ret;
};

static void* decompress_wrapper(void* args)
{
struct decompress_params* params = args;
params->ret = ZSTD_decompressStream(params->dctx, params->output, params->input);
return NULL;
}

static size_t zstd_decompress(ZSTD_DCtx* const dctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
{
#ifdef HAVE_RUBY_THREAD_H
struct decompress_params params = { dctx, output, input };
rb_thread_call_without_gvl(decompress_wrapper, &params, NULL, NULL);
return params.ret;
#else
return ZSTD_decompressStream(dctx, output, input);
#endif
}

#endif /* ZSTD_RUBY_H */
2 changes: 1 addition & 1 deletion ext/zstdruby/zstdruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static VALUE decompress_buffered(ZSTD_DCtx* dctx, const char* input_data, size_t
rb_str_resize(output_string, output.size);
output.dst = RSTRING_PTR(output_string);

size_t ret = ZSTD_decompressStream(dctx, &output, &input);
size_t ret = zstd_decompress(dctx, &output, &input);
if (ZSTD_isError(ret)) {
ZSTD_freeDCtx(dctx);
rb_raise(rb_eRuntimeError, "%s: %s", "ZSTD_decompressStream failed", ZSTD_getErrorName(ret));
Expand Down

0 comments on commit e754f09

Please sign in to comment.