zstd-ruby 1.5.6.2 → 1.5.6.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5c50aade1eb5c3a36dfaba5dc325c1bc416ceaa3558539ae5b389bb4378f6c37
4
- data.tar.gz: 490b1268c79f5e481969fb1cf2fe2adb9bba5a51ab0e0197fd0338913a228611
3
+ metadata.gz: fe4c394ffc4bcfd9e48c1baaa1dd91f8bf19f9bd65397218017c39145db14106
4
+ data.tar.gz: 5b87fa1c74eff20d0abe4b90260ac6fdb0db8fc65afd4a571bf0f7b10aa419b2
5
5
  SHA512:
6
- metadata.gz: 14db577b5e10bc3a92df632d391dfdb55802da3768e5b829c9b849f440b0b5234be8d924a6b0c5d237cab7befdd540d1535de0ba56dafd30fbfe13c2e635e229
7
- data.tar.gz: e6959f0da13aaa2a609fec024a3f293e4366760e3efd7626fc545c29ad5a1b0290119bc088cf60cb6665c1f2a5d93f0faf72152be3cf470ce5cf43657695a588
6
+ metadata.gz: ea1413beb577cc5c735f2a6514e99071bba67e40de2216d84b11afa77b653d36b026db573f06002797e4f258142b441331eaa4f9fb71e5426d1408ad4123bfeb
7
+ data.tar.gz: 58476bc0ce00c3d612e1aa460728a2255fbb9e289b132dc76106f539b535323d71ce5c0cda3adcded4cd8407066b332f79efff8fe41085e64e8bdee6ac14f563
@@ -2,6 +2,10 @@
2
2
  #define ZSTD_RUBY_H 1
3
3
 
4
4
  #include <ruby.h>
5
+ #ifdef HAVE_RUBY_THREAD_H
6
+ #include <ruby/thread.h>
7
+ #endif
8
+ #include <stdbool.h>
5
9
  #include "./libzstd/zstd.h"
6
10
 
7
11
  static int convert_compression_level(VALUE compression_level_value)
@@ -12,11 +16,6 @@ static int convert_compression_level(VALUE compression_level_value)
12
16
  return NUM2INT(compression_level_value);
13
17
  }
14
18
 
15
- static size_t zstd_compress(ZSTD_CCtx* const ctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input, ZSTD_EndDirective endOp)
16
- {
17
- return ZSTD_compressStream2(ctx, output, input, endOp);
18
- }
19
-
20
19
  static void set_compress_params(ZSTD_CCtx* const ctx, VALUE level_from_args, VALUE kwargs)
21
20
  {
22
21
  ID kwargs_keys[2];
@@ -45,6 +44,36 @@ static void set_compress_params(ZSTD_CCtx* const ctx, VALUE level_from_args, VAL
45
44
  }
46
45
  }
47
46
 
47
+ struct stream_compress_params {
48
+ ZSTD_CCtx* ctx;
49
+ ZSTD_outBuffer* output;
50
+ ZSTD_inBuffer* input;
51
+ ZSTD_EndDirective endOp;
52
+ size_t ret;
53
+ };
54
+
55
+ static void* stream_compress_wrapper(void* args)
56
+ {
57
+ struct stream_compress_params* params = args;
58
+ params->ret = ZSTD_compressStream2(params->ctx, params->output, params->input, params->endOp);
59
+ return NULL;
60
+ }
61
+
62
+ static size_t zstd_stream_compress(ZSTD_CCtx* const ctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input, ZSTD_EndDirective endOp, bool gvl)
63
+ {
64
+ #ifdef HAVE_RUBY_THREAD_H
65
+ if (gvl) {
66
+ return ZSTD_compressStream2(ctx, output, input, endOp);
67
+ } else {
68
+ struct stream_compress_params params = { ctx, output, input, endOp };
69
+ rb_thread_call_without_gvl(stream_compress_wrapper, &params, NULL, NULL);
70
+ return params.ret;
71
+ }
72
+ #else
73
+ return ZSTD_compressStream2(ctx, output, input, endOp);
74
+ #endif
75
+ }
76
+
48
77
  static void set_decompress_params(ZSTD_DCtx* const dctx, VALUE kwargs)
49
78
  {
50
79
  ID kwargs_keys[1];
@@ -63,4 +92,33 @@ static void set_decompress_params(ZSTD_DCtx* const dctx, VALUE kwargs)
63
92
  }
64
93
  }
65
94
 
95
+ struct decompress_params {
96
+ ZSTD_DCtx* dctx;
97
+ ZSTD_outBuffer* output;
98
+ ZSTD_inBuffer* input;
99
+ size_t ret;
100
+ };
101
+
102
+ static void* decompress_wrapper(void* args)
103
+ {
104
+ struct decompress_params* params = args;
105
+ params->ret = ZSTD_decompressStream(params->dctx, params->output, params->input);
106
+ return NULL;
107
+ }
108
+
109
+ static size_t zstd_decompress(ZSTD_DCtx* const dctx, ZSTD_outBuffer* output, ZSTD_inBuffer* input, bool gvl)
110
+ {
111
+ #ifdef HAVE_RUBY_THREAD_H
112
+ if (gvl) {
113
+ return ZSTD_decompressStream(dctx, output, input);
114
+ } else {
115
+ struct decompress_params params = { dctx, output, input };
116
+ rb_thread_call_without_gvl(decompress_wrapper, &params, NULL, NULL);
117
+ return params.ret;
118
+ }
119
+ #else
120
+ return ZSTD_decompressStream(dctx, output, input);
121
+ #endif
122
+ }
123
+
66
124
  #endif /* ZSTD_RUBY_H */
@@ -2,7 +2,7 @@ require "mkmf"
2
2
 
3
3
  have_func('rb_gc_mark_movable')
4
4
 
5
- $CFLAGS = '-I. -O3 -std=c99 -DZSTD_STATIC_LINKING_ONLY'
5
+ $CFLAGS = '-I. -O3 -std=c99 -DZSTD_STATIC_LINKING_ONLY -DZSTD_MULTITHREAD -pthread -DDEBUGLEVEL=0'
6
6
  $CPPFLAGS += " -fdeclspec" if CONFIG['CXX'] =~ /clang/
7
7
 
8
8
  Dir.chdir File.expand_path('..', __FILE__) do
@@ -106,7 +106,7 @@ no_compress(struct streaming_compress_t* sc, ZSTD_EndDirective endOp)
106
106
  do {
107
107
  ZSTD_outBuffer output = { (void*)output_data, sc->buf_size, 0 };
108
108
 
109
- size_t const ret = zstd_compress(sc->ctx, &output, &input, endOp);
109
+ size_t const ret = zstd_stream_compress(sc->ctx, &output, &input, endOp, false);
110
110
  if (ZSTD_isError(ret)) {
111
111
  rb_raise(rb_eRuntimeError, "flush error error code: %s", ZSTD_getErrorName(ret));
112
112
  }
@@ -130,7 +130,7 @@ rb_streaming_compress_compress(VALUE obj, VALUE src)
130
130
  VALUE result = rb_str_new(0, 0);
131
131
  while (input.pos < input.size) {
132
132
  ZSTD_outBuffer output = { (void*)output_data, sc->buf_size, 0 };
133
- size_t const ret = zstd_compress(sc->ctx, &output, &input, ZSTD_e_continue);
133
+ size_t const ret = zstd_stream_compress(sc->ctx, &output, &input, ZSTD_e_continue, false);
134
134
  if (ZSTD_isError(ret)) {
135
135
  rb_raise(rb_eRuntimeError, "compress error error code: %s", ZSTD_getErrorName(ret));
136
136
  }
@@ -157,7 +157,7 @@ rb_streaming_compress_write(int argc, VALUE *argv, VALUE obj)
157
157
 
158
158
  while (input.pos < input.size) {
159
159
  ZSTD_outBuffer output = { (void*)output_data, sc->buf_size, 0 };
160
- size_t const ret = zstd_compress(sc->ctx, &output, &input, ZSTD_e_continue);
160
+ size_t const ret = zstd_stream_compress(sc->ctx, &output, &input, ZSTD_e_continue, false);
161
161
  if (ZSTD_isError(ret)) {
162
162
  rb_raise(rb_eRuntimeError, "compress error error code: %s", ZSTD_getErrorName(ret));
163
163
  }
@@ -104,7 +104,7 @@ rb_streaming_decompress_decompress(VALUE obj, VALUE src)
104
104
  VALUE result = rb_str_new(0, 0);
105
105
  while (input.pos < input.size) {
106
106
  ZSTD_outBuffer output = { (void*)output_data, sd->buf_size, 0 };
107
- size_t const ret = ZSTD_decompressStream(sd->dctx, &output, &input);
107
+ size_t const ret = zstd_decompress(sd->dctx, &output, &input, false);
108
108
  if (ZSTD_isError(ret)) {
109
109
  rb_raise(rb_eRuntimeError, "decompress error error code: %s", ZSTD_getErrorName(ret));
110
110
  }
@@ -25,21 +25,19 @@ static VALUE rb_compress(int argc, VALUE *argv, VALUE self)
25
25
  StringValue(input_value);
26
26
  char* input_data = RSTRING_PTR(input_value);
27
27
  size_t input_size = RSTRING_LEN(input_value);
28
- ZSTD_inBuffer input = { input_data, input_size, 0 };
29
- size_t max_compressed_size = ZSTD_compressBound(input_size);
30
- VALUE buf = rb_str_new(NULL, max_compressed_size);
31
- char* output_data = RSTRING_PTR(buf);
32
- ZSTD_outBuffer output = { (void*)output_data, max_compressed_size, 0 };
33
28
 
34
- size_t const ret = zstd_compress(ctx, &output, &input, ZSTD_e_end);
29
+ size_t const max_compressed_size = ZSTD_compressBound(input_size);
30
+ VALUE output = rb_str_new(NULL, max_compressed_size);
31
+ const char* output_data = RSTRING_PTR(output);
32
+
33
+ size_t const ret = ZSTD_compress2(ctx,(void*)output_data, max_compressed_size, (void*)input_data, input_size);
35
34
  if (ZSTD_isError(ret)) {
36
- ZSTD_freeCCtx(ctx);
37
- rb_raise(rb_eRuntimeError, "%s: %s", "compress failed", ZSTD_getErrorName(ret));
35
+ rb_raise(rb_eRuntimeError, "compress error error code: %s", ZSTD_getErrorName(ret));
38
36
  }
39
- VALUE result = rb_str_new(0, 0);
40
- rb_str_cat(result, output.dst, output.pos);
37
+ rb_str_resize(output, ret);
38
+
41
39
  ZSTD_freeCCtx(ctx);
42
- return result;
40
+ return output;
43
41
  }
44
42
 
45
43
  static VALUE rb_compress_using_dict(int argc, VALUE *argv, VALUE self)
@@ -87,19 +85,8 @@ static VALUE rb_compress_using_dict(int argc, VALUE *argv, VALUE self)
87
85
  }
88
86
 
89
87
 
90
- static VALUE decompress_buffered(const char* input_data, size_t input_size)
88
+ static VALUE decompress_buffered(ZSTD_DCtx* dctx, const char* input_data, size_t input_size)
91
89
  {
92
- ZSTD_DStream* const dstream = ZSTD_createDStream();
93
- if (dstream == NULL) {
94
- rb_raise(rb_eRuntimeError, "%s", "ZSTD_createDStream failed");
95
- }
96
-
97
- size_t initResult = ZSTD_initDStream(dstream);
98
- if (ZSTD_isError(initResult)) {
99
- ZSTD_freeDStream(dstream);
100
- rb_raise(rb_eRuntimeError, "%s: %s", "ZSTD_initDStream failed", ZSTD_getErrorName(initResult));
101
- }
102
-
103
90
  VALUE output_string = rb_str_new(NULL, 0);
104
91
  ZSTD_outBuffer output = { NULL, 0, 0 };
105
92
 
@@ -109,15 +96,14 @@ static VALUE decompress_buffered(const char* input_data, size_t input_size)
109
96
  rb_str_resize(output_string, output.size);
110
97
  output.dst = RSTRING_PTR(output_string);
111
98
 
112
- size_t readHint = ZSTD_decompressStream(dstream, &output, &input);
113
- if (ZSTD_isError(readHint)) {
114
- ZSTD_freeDStream(dstream);
115
- rb_raise(rb_eRuntimeError, "%s: %s", "ZSTD_decompressStream failed", ZSTD_getErrorName(readHint));
99
+ size_t ret = zstd_decompress(dctx, &output, &input, true);
100
+ if (ZSTD_isError(ret)) {
101
+ ZSTD_freeDCtx(dctx);
102
+ rb_raise(rb_eRuntimeError, "%s: %s", "ZSTD_decompressStream failed", ZSTD_getErrorName(ret));
116
103
  }
117
104
  }
118
-
119
- ZSTD_freeDStream(dstream);
120
105
  rb_str_resize(output_string, output.pos);
106
+ ZSTD_freeDCtx(dctx);
121
107
  return output_string;
122
108
  }
123
109
 
@@ -129,6 +115,11 @@ static VALUE rb_decompress(int argc, VALUE *argv, VALUE self)
129
115
  StringValue(input_value);
130
116
  char* input_data = RSTRING_PTR(input_value);
131
117
  size_t input_size = RSTRING_LEN(input_value);
118
+ ZSTD_DCtx* const dctx = ZSTD_createDCtx();
119
+ if (dctx == NULL) {
120
+ rb_raise(rb_eRuntimeError, "%s", "ZSTD_createDCtx failed");
121
+ }
122
+ set_decompress_params(dctx, kwargs);
132
123
 
133
124
  unsigned long long const uncompressed_size = ZSTD_getFrameContentSize(input_data, input_size);
134
125
  if (uncompressed_size == ZSTD_CONTENTSIZE_ERROR) {
@@ -137,15 +128,9 @@ static VALUE rb_decompress(int argc, VALUE *argv, VALUE self)
137
128
  // ZSTD_decompressStream may be called multiple times when ZSTD_CONTENTSIZE_UNKNOWN, causing slowness.
138
129
  // Therefore, we will not standardize on ZSTD_decompressStream
139
130
  if (uncompressed_size == ZSTD_CONTENTSIZE_UNKNOWN) {
140
- return decompress_buffered(input_data, input_size);
131
+ return decompress_buffered(dctx, input_data, input_size);
141
132
  }
142
133
 
143
- ZSTD_DCtx* const dctx = ZSTD_createDCtx();
144
- if (dctx == NULL) {
145
- rb_raise(rb_eRuntimeError, "%s", "ZSTD_createDCtx failed");
146
- }
147
- set_decompress_params(dctx, kwargs);
148
-
149
134
  VALUE output = rb_str_new(NULL, uncompressed_size);
150
135
  char* output_data = RSTRING_PTR(output);
151
136
 
@@ -153,7 +138,7 @@ static VALUE rb_decompress(int argc, VALUE *argv, VALUE self)
153
138
  if (ZSTD_isError(decompress_size)) {
154
139
  rb_raise(rb_eRuntimeError, "%s: %s", "decompress error", ZSTD_getErrorName(decompress_size));
155
140
  }
156
-
141
+ ZSTD_freeDCtx(dctx);
157
142
  return output;
158
143
  }
159
144
 
@@ -167,15 +152,6 @@ static VALUE rb_decompress_using_dict(int argc, VALUE *argv, VALUE self)
167
152
  StringValue(input_value);
168
153
  char* input_data = RSTRING_PTR(input_value);
169
154
  size_t input_size = RSTRING_LEN(input_value);
170
- unsigned long long const uncompressed_size = ZSTD_getFrameContentSize(input_data, input_size);
171
- if (uncompressed_size == ZSTD_CONTENTSIZE_ERROR) {
172
- rb_raise(rb_eRuntimeError, "%s: %s", "not compressed by zstd", ZSTD_getErrorName(uncompressed_size));
173
- }
174
- if (uncompressed_size == ZSTD_CONTENTSIZE_UNKNOWN) {
175
- return decompress_buffered(input_data, input_size);
176
- }
177
- VALUE output = rb_str_new(NULL, uncompressed_size);
178
- char* output_data = RSTRING_PTR(output);
179
155
 
180
156
  char* dict_buffer = RSTRING_PTR(dict);
181
157
  size_t dict_size = RSTRING_LEN(dict);
@@ -183,12 +159,11 @@ static VALUE rb_decompress_using_dict(int argc, VALUE *argv, VALUE self)
183
159
  if (ddict == NULL) {
184
160
  rb_raise(rb_eRuntimeError, "%s", "ZSTD_createDDict failed");
185
161
  }
186
-
187
162
  unsigned const expected_dict_id = ZSTD_getDictID_fromDDict(ddict);
188
163
  unsigned const actual_dict_id = ZSTD_getDictID_fromFrame(input_data, input_size);
189
164
  if (expected_dict_id != actual_dict_id) {
190
165
  ZSTD_freeDDict(ddict);
191
- rb_raise(rb_eRuntimeError, "%s: %s", "DictID mismatch", ZSTD_getErrorName(uncompressed_size));
166
+ rb_raise(rb_eRuntimeError, "DictID mismatch");
192
167
  }
193
168
 
194
169
  ZSTD_DCtx* const ctx = ZSTD_createDCtx();
@@ -196,6 +171,19 @@ static VALUE rb_decompress_using_dict(int argc, VALUE *argv, VALUE self)
196
171
  ZSTD_freeDDict(ddict);
197
172
  rb_raise(rb_eRuntimeError, "%s", "ZSTD_createDCtx failed");
198
173
  }
174
+
175
+ unsigned long long const uncompressed_size = ZSTD_getFrameContentSize(input_data, input_size);
176
+ if (uncompressed_size == ZSTD_CONTENTSIZE_ERROR) {
177
+ ZSTD_freeDDict(ddict);
178
+ ZSTD_freeDCtx(ctx);
179
+ rb_raise(rb_eRuntimeError, "%s: %s", "not compressed by zstd", ZSTD_getErrorName(uncompressed_size));
180
+ }
181
+ if (uncompressed_size == ZSTD_CONTENTSIZE_UNKNOWN) {
182
+ return decompress_buffered(ctx, input_data, input_size);
183
+ }
184
+
185
+ VALUE output = rb_str_new(NULL, uncompressed_size);
186
+ char* output_data = RSTRING_PTR(output);
199
187
  size_t const decompress_size = ZSTD_decompress_usingDDict(ctx, output_data, uncompressed_size, input_data, input_size, ddict);
200
188
  if (ZSTD_isError(decompress_size)) {
201
189
  ZSTD_freeDDict(ddict);
@@ -1,3 +1,3 @@
1
1
  module Zstd
2
- VERSION = "1.5.6.2"
2
+ VERSION = "1.5.6.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zstd-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.6.2
4
+ version: 1.5.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - SpringMT
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-11 00:00:00.000000000 Z
11
+ date: 2024-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler