msgpack 1.5.6 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/ChangeLog +50 -0
  3. data/README.md +48 -12
  4. data/ext/java/org/msgpack/jruby/Buffer.java +3 -3
  5. data/ext/java/org/msgpack/jruby/ExtensionRegistry.java +11 -20
  6. data/ext/java/org/msgpack/jruby/ExtensionValue.java +1 -1
  7. data/ext/java/org/msgpack/jruby/Factory.java +11 -50
  8. data/ext/java/org/msgpack/jruby/Packer.java +9 -24
  9. data/ext/java/org/msgpack/jruby/Unpacker.java +15 -32
  10. data/ext/msgpack/buffer.c +69 -56
  11. data/ext/msgpack/buffer.h +138 -44
  12. data/ext/msgpack/buffer_class.c +132 -31
  13. data/ext/msgpack/buffer_class.h +1 -0
  14. data/ext/msgpack/extconf.rb +20 -30
  15. data/ext/msgpack/factory_class.c +75 -86
  16. data/ext/msgpack/packer.c +13 -16
  17. data/ext/msgpack/packer.h +24 -21
  18. data/ext/msgpack/packer_class.c +72 -98
  19. data/ext/msgpack/packer_class.h +11 -0
  20. data/ext/msgpack/packer_ext_registry.c +31 -28
  21. data/ext/msgpack/packer_ext_registry.h +10 -14
  22. data/ext/msgpack/rbinit.c +1 -1
  23. data/ext/msgpack/rmem.c +3 -4
  24. data/ext/msgpack/sysdep.h +5 -2
  25. data/ext/msgpack/unpacker.c +201 -113
  26. data/ext/msgpack/unpacker.h +22 -15
  27. data/ext/msgpack/unpacker_class.c +87 -92
  28. data/ext/msgpack/unpacker_class.h +11 -0
  29. data/ext/msgpack/unpacker_ext_registry.c +4 -16
  30. data/ext/msgpack/unpacker_ext_registry.h +3 -7
  31. data/lib/msgpack/buffer.rb +9 -0
  32. data/lib/msgpack/factory.rb +90 -63
  33. data/lib/msgpack/packer.rb +10 -1
  34. data/lib/msgpack/unpacker.rb +14 -1
  35. data/lib/msgpack/version.rb +1 -1
  36. data/lib/msgpack.rb +1 -0
  37. data/msgpack.gemspec +8 -3
  38. metadata +21 -51
  39. data/.github/workflows/ci.yaml +0 -57
  40. data/.gitignore +0 -23
  41. data/.rubocop.yml +0 -36
  42. data/Gemfile +0 -9
  43. data/Rakefile +0 -70
  44. data/appveyor.yml +0 -18
  45. data/bench/bench.rb +0 -78
  46. data/doclib/msgpack/buffer.rb +0 -193
  47. data/doclib/msgpack/core_ext.rb +0 -101
  48. data/doclib/msgpack/error.rb +0 -19
  49. data/doclib/msgpack/extension_value.rb +0 -9
  50. data/doclib/msgpack/factory.rb +0 -145
  51. data/doclib/msgpack/packer.rb +0 -209
  52. data/doclib/msgpack/time.rb +0 -22
  53. data/doclib/msgpack/timestamp.rb +0 -44
  54. data/doclib/msgpack/unpacker.rb +0 -183
  55. data/doclib/msgpack.rb +0 -87
  56. data/msgpack.org.md +0 -46
  57. data/spec/bigint_spec.rb +0 -26
  58. data/spec/cases.json +0 -1
  59. data/spec/cases.msg +0 -0
  60. data/spec/cases_compact.msg +0 -0
  61. data/spec/cases_spec.rb +0 -39
  62. data/spec/cruby/buffer_io_spec.rb +0 -255
  63. data/spec/cruby/buffer_packer.rb +0 -29
  64. data/spec/cruby/buffer_spec.rb +0 -575
  65. data/spec/cruby/buffer_unpacker.rb +0 -19
  66. data/spec/cruby/unpacker_spec.rb +0 -70
  67. data/spec/ext_value_spec.rb +0 -99
  68. data/spec/exttypes.rb +0 -51
  69. data/spec/factory_spec.rb +0 -688
  70. data/spec/format_spec.rb +0 -301
  71. data/spec/jruby/benchmarks/shootout_bm.rb +0 -73
  72. data/spec/jruby/benchmarks/symbolize_keys_bm.rb +0 -25
  73. data/spec/jruby/unpacker_spec.rb +0 -186
  74. data/spec/msgpack_spec.rb +0 -214
  75. data/spec/pack_spec.rb +0 -61
  76. data/spec/packer_spec.rb +0 -575
  77. data/spec/random_compat.rb +0 -24
  78. data/spec/spec_helper.rb +0 -71
  79. data/spec/timestamp_spec.rb +0 -159
  80. data/spec/unpack_spec.rb +0 -57
  81. data/spec/unpacker_spec.rb +0 -859
@@ -37,15 +37,10 @@ struct msgpack_factory_t {
37
37
  int symbol_ext_type;
38
38
  };
39
39
 
40
- #define FACTORY(from, name) \
41
- msgpack_factory_t *name = NULL; \
42
- Data_Get_Struct(from, msgpack_factory_t, name); \
43
- if(name == NULL) { \
44
- rb_raise(rb_eArgError, "NULL found for " # name " when shouldn't be."); \
45
- }
46
-
47
- static void Factory_free(msgpack_factory_t* fc)
40
+ static void Factory_free(void *ptr)
48
41
  {
42
+ msgpack_factory_t *fc = ptr;
43
+
49
44
  if(fc == NULL) {
50
45
  return;
51
46
  }
@@ -54,25 +49,56 @@ static void Factory_free(msgpack_factory_t* fc)
54
49
  xfree(fc);
55
50
  }
56
51
 
57
- void Factory_mark(msgpack_factory_t* fc)
52
+ void Factory_mark(void *ptr)
58
53
  {
54
+ msgpack_factory_t *fc = ptr;
59
55
  msgpack_packer_ext_registry_mark(&fc->pkrg);
60
56
  msgpack_unpacker_ext_registry_mark(fc->ukrg);
61
57
  }
62
58
 
63
- static VALUE Factory_alloc(VALUE klass)
59
+ static size_t Factory_memsize(const void *ptr)
64
60
  {
65
- msgpack_factory_t* fc = ZALLOC_N(msgpack_factory_t, 1);
61
+ const msgpack_factory_t *fc = ptr;
62
+ size_t total_size = sizeof(msgpack_factory_t);
66
63
 
67
- VALUE self = Data_Wrap_Struct(klass, Factory_mark, Factory_free, fc);
68
- return self;
64
+ if (fc->ukrg) {
65
+ total_size += sizeof(msgpack_unpacker_ext_registry_t) / (fc->ukrg->borrow_count + 1);
66
+ }
67
+
68
+ return total_size;
69
+ }
70
+
71
+ static const rb_data_type_t factory_data_type = {
72
+ .wrap_struct_name = "msgpack:factory",
73
+ .function = {
74
+ .dmark = Factory_mark,
75
+ .dfree = Factory_free,
76
+ .dsize = Factory_memsize,
77
+ },
78
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
79
+ };
80
+
81
+ static inline msgpack_factory_t *Factory_get(VALUE object)
82
+ {
83
+ msgpack_factory_t *factory;
84
+ TypedData_Get_Struct(object, msgpack_factory_t, &factory_data_type, factory);
85
+ if (!factory) {
86
+ rb_raise(rb_eArgError, "Uninitialized Factory object");
87
+ }
88
+ return factory;
89
+ }
90
+
91
+ static VALUE Factory_alloc(VALUE klass)
92
+ {
93
+ msgpack_factory_t *fc;
94
+ return TypedData_Make_Struct(klass, msgpack_factory_t, &factory_data_type, fc);
69
95
  }
70
96
 
71
97
  static VALUE Factory_initialize(int argc, VALUE* argv, VALUE self)
72
98
  {
73
- FACTORY(self, fc);
99
+ msgpack_factory_t *fc = Factory_get(self);
74
100
 
75
- msgpack_packer_ext_registry_init(&fc->pkrg);
101
+ msgpack_packer_ext_registry_init(self, &fc->pkrg);
76
102
  // fc->ukrg is lazily initialized
77
103
 
78
104
  fc->has_symbol_ext_type = false;
@@ -92,20 +118,20 @@ static VALUE Factory_dup(VALUE self)
92
118
  {
93
119
  VALUE clone = Factory_alloc(rb_obj_class(self));
94
120
 
95
- FACTORY(self, fc);
96
- FACTORY(clone, cloned_fc);
121
+ msgpack_factory_t *fc = Factory_get(self);
122
+ msgpack_factory_t *cloned_fc = Factory_get(clone);
97
123
 
98
124
  cloned_fc->has_symbol_ext_type = fc->has_symbol_ext_type;
99
125
  cloned_fc->pkrg = fc->pkrg;
100
126
  msgpack_unpacker_ext_registry_borrow(fc->ukrg, &cloned_fc->ukrg);
101
- msgpack_packer_ext_registry_dup(&fc->pkrg, &cloned_fc->pkrg);
127
+ msgpack_packer_ext_registry_dup(clone, &fc->pkrg, &cloned_fc->pkrg);
102
128
 
103
129
  return clone;
104
130
  }
105
131
 
106
132
  static VALUE Factory_freeze(VALUE self) {
107
133
  if(!rb_obj_frozen_p(self)) {
108
- FACTORY(self, fc);
134
+ msgpack_factory_t *fc = Factory_get(self);
109
135
 
110
136
  if (RTEST(fc->pkrg.hash)) {
111
137
  rb_hash_freeze(fc->pkrg.hash);
@@ -113,7 +139,7 @@ static VALUE Factory_freeze(VALUE self) {
113
139
  // If the factory is frozen, we can safely share the packer cache between
114
140
  // all packers. So we eagerly create it now so it's available when #packer
115
141
  // is called.
116
- fc->pkrg.cache = rb_hash_new();
142
+ RB_OBJ_WRITE(self, &fc->pkrg.cache, rb_hash_new());
117
143
  }
118
144
  }
119
145
 
@@ -125,16 +151,14 @@ static VALUE Factory_freeze(VALUE self) {
125
151
 
126
152
  VALUE MessagePack_Factory_packer(int argc, VALUE* argv, VALUE self)
127
153
  {
128
- FACTORY(self, fc);
154
+ msgpack_factory_t *fc = Factory_get(self);
129
155
 
130
156
  VALUE packer = MessagePack_Packer_alloc(cMessagePack_Packer);
131
157
  MessagePack_Packer_initialize(argc, argv, packer);
132
158
 
133
- msgpack_packer_t* pk;
134
- Data_Get_Struct(packer, msgpack_packer_t, pk);
135
-
159
+ msgpack_packer_t* pk = MessagePack_Packer_get(packer);
136
160
  msgpack_packer_ext_registry_destroy(&pk->ext_registry);
137
- msgpack_packer_ext_registry_dup(&fc->pkrg, &pk->ext_registry);
161
+ msgpack_packer_ext_registry_borrow(packer, &fc->pkrg, &pk->ext_registry);
138
162
  pk->has_bigint_ext_type = fc->has_bigint_ext_type;
139
163
  pk->has_symbol_ext_type = fc->has_symbol_ext_type;
140
164
 
@@ -143,13 +167,12 @@ VALUE MessagePack_Factory_packer(int argc, VALUE* argv, VALUE self)
143
167
 
144
168
  VALUE MessagePack_Factory_unpacker(int argc, VALUE* argv, VALUE self)
145
169
  {
146
- FACTORY(self, fc);
170
+ msgpack_factory_t *fc = Factory_get(self);
147
171
 
148
172
  VALUE unpacker = MessagePack_Unpacker_alloc(cMessagePack_Unpacker);
149
173
  MessagePack_Unpacker_initialize(argc, argv, unpacker);
150
174
 
151
- msgpack_unpacker_t* uk;
152
- Data_Get_Struct(unpacker, msgpack_unpacker_t, uk);
175
+ msgpack_unpacker_t* uk = MessagePack_Unpacker_get(unpacker);
153
176
  msgpack_unpacker_ext_registry_borrow(fc->ukrg, &uk->ext_registry);
154
177
  uk->optimized_symbol_ext_type = fc->optimized_symbol_ext_type;
155
178
  uk->symbol_ext_type = fc->symbol_ext_type;
@@ -159,12 +182,12 @@ VALUE MessagePack_Factory_unpacker(int argc, VALUE* argv, VALUE self)
159
182
 
160
183
  static VALUE Factory_registered_types_internal(VALUE self)
161
184
  {
162
- FACTORY(self, fc);
185
+ msgpack_factory_t *fc = Factory_get(self);
163
186
 
164
187
  VALUE uk_mapping = rb_hash_new();
165
188
  if (fc->ukrg) {
166
189
  for(int i=0; i < 256; i++) {
167
- if(fc->ukrg->array[i] != Qnil) {
190
+ if(!NIL_P(fc->ukrg->array[i])) {
168
191
  rb_hash_aset(uk_mapping, INT2FIX(i - 128), fc->ukrg->array[i]);
169
192
  }
170
193
  }
@@ -177,73 +200,39 @@ static VALUE Factory_registered_types_internal(VALUE self)
177
200
  );
178
201
  }
179
202
 
180
- static VALUE Factory_register_type(int argc, VALUE* argv, VALUE self)
203
+ static VALUE Factory_register_type_internal(VALUE self, VALUE rb_ext_type, VALUE ext_module, VALUE options)
181
204
  {
182
- FACTORY(self, fc);
205
+ msgpack_factory_t *fc = Factory_get(self);
183
206
 
184
- int ext_type;
185
- int flags = 0;
186
- VALUE ext_module;
187
- VALUE options = Qnil;
188
- VALUE packer_arg, unpacker_arg;
189
- VALUE packer_proc, unpacker_proc;
207
+ Check_Type(rb_ext_type, T_FIXNUM);
190
208
 
191
- if (OBJ_FROZEN(self)) {
192
- rb_raise(rb_eRuntimeError, "can't modify frozen Factory");
209
+ if(rb_type(ext_module) != T_MODULE && rb_type(ext_module) != T_CLASS) {
210
+ rb_raise(rb_eArgError, "expected Module/Class but found %s.", rb_obj_classname(ext_module));
193
211
  }
194
212
 
195
- switch (argc) {
196
- case 2:
197
- /* register_type(0x7f, Time) */
198
- packer_arg = ID2SYM(rb_intern("to_msgpack_ext"));
199
- unpacker_arg = ID2SYM(rb_intern("from_msgpack_ext"));
200
- break;
201
- case 3:
202
- /* register_type(0x7f, Time, packer: proc-like, unapcker: proc-like) */
203
- options = argv[2];
204
- if(rb_type(options) != T_HASH) {
205
- rb_raise(rb_eArgError, "expected Hash but found %s.", rb_obj_classname(options));
206
- }
207
- packer_arg = rb_hash_aref(options, ID2SYM(rb_intern("packer")));
208
- unpacker_arg = rb_hash_aref(options, ID2SYM(rb_intern("unpacker")));
209
- break;
210
- default:
211
- rb_raise(rb_eArgError, "wrong number of arguments (%d for 2..3)", argc);
212
- }
213
+ int flags = 0;
213
214
 
214
- if (options != Qnil) {
215
+ VALUE packer_proc = Qnil;
216
+ VALUE unpacker_proc = Qnil;
217
+ if(!NIL_P(options)) {
215
218
  Check_Type(options, T_HASH);
219
+ packer_proc = rb_hash_aref(options, ID2SYM(rb_intern("packer")));
220
+ unpacker_proc = rb_hash_aref(options, ID2SYM(rb_intern("unpacker")));
216
221
  }
217
222
 
218
- ext_type = NUM2INT(argv[0]);
219
- if(ext_type < -128 || ext_type > 127) {
220
- rb_raise(rb_eRangeError, "integer %d too big to convert to `signed char'", ext_type);
221
- }
222
-
223
- ext_module = argv[1];
224
- if(rb_type(ext_module) != T_MODULE && rb_type(ext_module) != T_CLASS) {
225
- rb_raise(rb_eArgError, "expected Module/Class but found %s.", rb_obj_classname(ext_module));
226
- }
227
-
228
- packer_proc = Qnil;
229
- unpacker_proc = Qnil;
230
-
231
- if(packer_arg != Qnil) {
232
- packer_proc = rb_funcall(packer_arg, rb_intern("to_proc"), 0);
223
+ if (OBJ_FROZEN(self)) {
224
+ rb_raise(rb_eFrozenError, "can't modify frozen MessagePack::Factory");
233
225
  }
234
226
 
235
- if(unpacker_arg != Qnil) {
236
- if(rb_type(unpacker_arg) == T_SYMBOL || rb_type(unpacker_arg) == T_STRING) {
237
- unpacker_proc = rb_obj_method(ext_module, unpacker_arg);
238
- } else if (rb_respond_to(unpacker_arg, rb_intern("call"))) {
239
- unpacker_proc = unpacker_arg;
240
- } else {
241
- unpacker_proc = rb_funcall(unpacker_arg, rb_intern("method"), 1, ID2SYM(rb_intern("call")));
242
- }
227
+ int ext_type = NUM2INT(rb_ext_type);
228
+ if(ext_type < -128 || ext_type > 127) {
229
+ rb_raise(rb_eRangeError, "integer %d too big to convert to `signed char'", ext_type);
243
230
  }
244
231
 
245
232
  if(ext_module == rb_cSymbol) {
246
- fc->has_symbol_ext_type = true;
233
+ if(NIL_P(options) || RTEST(rb_hash_aref(options, ID2SYM(rb_intern("packer"))))) {
234
+ fc->has_symbol_ext_type = true;
235
+ }
247
236
  if(RTEST(options) && RTEST(rb_hash_aref(options, ID2SYM(rb_intern("optimized_symbols_parsing"))))) {
248
237
  fc->optimized_symbol_ext_type = true;
249
238
  }
@@ -263,8 +252,8 @@ static VALUE Factory_register_type(int argc, VALUE* argv, VALUE self)
263
252
  }
264
253
  }
265
254
 
266
- msgpack_packer_ext_registry_put(&fc->pkrg, ext_module, ext_type, flags, packer_proc, packer_arg);
267
- msgpack_unpacker_ext_registry_put(&fc->ukrg, ext_module, ext_type, flags, unpacker_proc, unpacker_arg);
255
+ msgpack_packer_ext_registry_put(self, &fc->pkrg, ext_module, ext_type, flags, packer_proc);
256
+ msgpack_unpacker_ext_registry_put(self, &fc->ukrg, ext_module, ext_type, flags, unpacker_proc);
268
257
 
269
258
  return Qnil;
270
259
  }
@@ -283,5 +272,5 @@ void MessagePack_Factory_module_init(VALUE mMessagePack)
283
272
  rb_define_method(cMessagePack_Factory, "unpacker", MessagePack_Factory_unpacker, -1);
284
273
 
285
274
  rb_define_private_method(cMessagePack_Factory, "registered_types_internal", Factory_registered_types_internal, 0);
286
- rb_define_method(cMessagePack_Factory, "register_type", Factory_register_type, -1);
275
+ rb_define_private_method(cMessagePack_Factory, "register_type_internal", Factory_register_type_internal, 3);
287
276
  }
data/ext/msgpack/packer.c CHANGED
@@ -17,21 +17,14 @@
17
17
  */
18
18
 
19
19
  #include "packer.h"
20
+ #include "buffer_class.h"
20
21
 
21
- static ID s_call;
22
-
23
- void msgpack_packer_static_init()
24
- {
25
- s_call = rb_intern("call");
26
- }
27
-
28
- void msgpack_packer_static_destroy()
29
- { }
22
+ #if !defined(HAVE_RB_PROC_CALL_WITH_BLOCK)
23
+ #define rb_proc_call_with_block(recv, argc, argv, block) rb_funcallv(recv, rb_intern("call"), argc, argv)
24
+ #endif
30
25
 
31
26
  void msgpack_packer_init(msgpack_packer_t* pk)
32
27
  {
33
- memset(pk, 0, sizeof(msgpack_packer_t));
34
-
35
28
  msgpack_buffer_init(PACKER_BUFFER_(pk));
36
29
  }
37
30
 
@@ -45,6 +38,7 @@ void msgpack_packer_mark(msgpack_packer_t* pk)
45
38
  /* See MessagePack_Buffer_wrap */
46
39
  /* msgpack_buffer_mark(PACKER_BUFFER_(pk)); */
47
40
  rb_gc_mark(pk->buffer_ref);
41
+ rb_gc_mark(pk->to_msgpack_arg);
48
42
  }
49
43
 
50
44
  void msgpack_packer_reset(msgpack_packer_t* pk)
@@ -101,14 +95,13 @@ struct msgpack_call_proc_args_t;
101
95
  typedef struct msgpack_call_proc_args_t msgpack_call_proc_args_t;
102
96
  struct msgpack_call_proc_args_t {
103
97
  VALUE proc;
104
- VALUE arg;
105
- VALUE packer;
98
+ VALUE args[2];
106
99
  };
107
100
 
108
101
  VALUE msgpack_packer_try_calling_proc(VALUE value)
109
102
  {
110
103
  msgpack_call_proc_args_t *args = (msgpack_call_proc_args_t *)value;
111
- return rb_funcall(args->proc, s_call, 2, args->arg, args->packer);
104
+ return rb_proc_call_with_block(args->proc, 2, args->args, Qnil);
112
105
  }
113
106
 
114
107
  bool msgpack_packer_try_write_with_ext_type_lookup(msgpack_packer_t* pk, VALUE v)
@@ -122,11 +115,13 @@ bool msgpack_packer_try_write_with_ext_type_lookup(msgpack_packer_t* pk, VALUE v
122
115
  }
123
116
 
124
117
  if(ext_flags & MSGPACK_EXT_RECURSIVE) {
118
+ VALUE held_buffer = MessagePack_Buffer_hold(&pk->buffer);
119
+
125
120
  msgpack_buffer_t parent_buffer = pk->buffer;
126
121
  msgpack_buffer_init(PACKER_BUFFER_(pk));
127
122
 
128
123
  int exception_occured = 0;
129
- msgpack_call_proc_args_t args = { proc, v, pk->to_msgpack_arg };
124
+ msgpack_call_proc_args_t args = { proc, { v, pk->to_msgpack_arg } };
130
125
  rb_protect(msgpack_packer_try_calling_proc, (VALUE)&args, &exception_occured);
131
126
 
132
127
  if (exception_occured) {
@@ -140,8 +135,10 @@ bool msgpack_packer_try_write_with_ext_type_lookup(msgpack_packer_t* pk, VALUE v
140
135
  pk->buffer = parent_buffer;
141
136
  msgpack_packer_write_ext(pk, ext_type, payload);
142
137
  }
138
+
139
+ RB_GC_GUARD(held_buffer);
143
140
  } else {
144
- VALUE payload = rb_funcall(proc, s_call, 1, v);
141
+ VALUE payload = rb_proc_call_with_block(proc, 1, &v, Qnil);
145
142
  StringValue(payload);
146
143
  msgpack_packer_write_ext(pk, ext_type, payload);
147
144
  }
data/ext/msgpack/packer.h CHANGED
@@ -25,21 +25,26 @@
25
25
  #define MSGPACK_PACKER_IO_FLUSH_THRESHOLD_TO_WRITE_STRING_BODY (1024)
26
26
  #endif
27
27
 
28
+ #ifndef UNREACHABLE_RETURN
29
+ // Ruby 2.5
30
+ #define UNREACHABLE_RETURN() return
31
+ #endif
32
+
28
33
  struct msgpack_packer_t;
29
34
  typedef struct msgpack_packer_t msgpack_packer_t;
30
35
 
31
36
  struct msgpack_packer_t {
32
37
  msgpack_buffer_t buffer;
33
38
 
34
- bool compatibility_mode;
35
- bool has_bigint_ext_type;
36
- bool has_symbol_ext_type;
37
-
38
39
  ID to_msgpack_method;
39
40
  VALUE to_msgpack_arg;
40
41
 
41
42
  VALUE buffer_ref;
42
43
 
44
+ bool compatibility_mode;
45
+ bool has_bigint_ext_type;
46
+ bool has_symbol_ext_type;
47
+
43
48
  /* options */
44
49
  bool comaptibility_mode;
45
50
  msgpack_packer_ext_registry_t ext_registry;
@@ -47,10 +52,6 @@ struct msgpack_packer_t {
47
52
 
48
53
  #define PACKER_BUFFER_(pk) (&(pk)->buffer)
49
54
 
50
- void msgpack_packer_static_init();
51
-
52
- void msgpack_packer_static_destroy();
53
-
54
55
  void msgpack_packer_init(msgpack_packer_t* pk);
55
56
 
56
57
  void msgpack_packer_destroy(msgpack_packer_t* pk);
@@ -408,27 +409,33 @@ static inline bool msgpack_packer_is_utf8_compat_string(VALUE v, int encindex)
408
409
  {
409
410
  return encindex == msgpack_rb_encindex_utf8
410
411
  || encindex == msgpack_rb_encindex_usascii
411
- || (rb_enc_asciicompat(rb_enc_from_index(encindex)) && ENC_CODERANGE_ASCIIONLY(v));
412
+ || ENC_CODERANGE_ASCIIONLY(v);
412
413
  }
413
414
 
414
415
  static inline void msgpack_packer_write_string_value(msgpack_packer_t* pk, VALUE v)
415
416
  {
416
- /* actual return type of RSTRING_LEN is long */
417
- unsigned long len = RSTRING_LEN(v);
418
- if(len > 0xffffffffUL) {
419
- // TODO rb_eArgError?
420
- rb_raise(rb_eArgError, "size of string is too long to pack: %lu bytes should be <= %lu", len, 0xffffffffUL);
417
+ long len = RSTRING_LEN(v);
418
+
419
+ if(RB_UNLIKELY(len > 0xffffffffL)) {
420
+ rb_raise(rb_eArgError, "size of string is too long to pack: %lu bytes should be <= %ld", len, 0xffffffffL);
421
+ UNREACHABLE_RETURN();
421
422
  }
422
423
 
423
- int encindex = ENCODING_GET(v);
424
- if(msgpack_packer_is_binary(v, encindex) && !pk->compatibility_mode) {
424
+ if (RB_UNLIKELY(pk->compatibility_mode)) {
425
+ msgpack_packer_write_raw_header(pk, (unsigned int)len);
426
+ msgpack_buffer_append_string(PACKER_BUFFER_(pk), v);
427
+ return;
428
+ }
429
+
430
+ int encindex = ENCODING_GET_INLINED(v);
431
+ if(msgpack_packer_is_binary(v, encindex)) {
425
432
  /* write ASCII-8BIT string using Binary type */
426
433
  msgpack_packer_write_bin_header(pk, (unsigned int)len);
427
434
  msgpack_buffer_append_string(PACKER_BUFFER_(pk), v);
428
435
  } else {
429
436
  /* write UTF-8, US-ASCII, or 7bit-safe ascii-compatible string using String type directly */
430
437
  /* in compatibility mode, packer packs String values as is */
431
- if(!pk->compatibility_mode && !msgpack_packer_is_utf8_compat_string(v, encindex)) {
438
+ if(RB_UNLIKELY(!msgpack_packer_is_utf8_compat_string(v, encindex))) {
432
439
  /* transcode other strings to UTF-8 and write using String type */
433
440
  VALUE enc = rb_enc_from_encoding(rb_utf8_encoding()); /* rb_enc_from_encoding_index is not extern */
434
441
  v = rb_str_encode(v, enc, 0, Qnil);
@@ -457,11 +464,7 @@ static inline void msgpack_packer_write_symbol_value(msgpack_packer_t* pk, VALUE
457
464
 
458
465
  static inline void msgpack_packer_write_fixnum_value(msgpack_packer_t* pk, VALUE v)
459
466
  {
460
- #ifdef JRUBY
461
- msgpack_packer_write_long(pk, FIXNUM_P(v) ? FIX2LONG(v) : rb_num2ll(v));
462
- #else
463
467
  msgpack_packer_write_long(pk, FIX2LONG(v));
464
- #endif
465
468
  }
466
469
 
467
470
  static inline void msgpack_packer_write_bignum_value(msgpack_packer_t* pk, VALUE v)