ed-precompiled_msgpack 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.
- checksums.yaml +7 -0
- data/ChangeLog +368 -0
- data/LICENSE +177 -0
- data/README.md +302 -0
- data/ext/java/org/msgpack/jruby/Buffer.java +233 -0
- data/ext/java/org/msgpack/jruby/Decoder.java +307 -0
- data/ext/java/org/msgpack/jruby/Encoder.java +456 -0
- data/ext/java/org/msgpack/jruby/ExtensionRegistry.java +167 -0
- data/ext/java/org/msgpack/jruby/ExtensionValue.java +128 -0
- data/ext/java/org/msgpack/jruby/Factory.java +130 -0
- data/ext/java/org/msgpack/jruby/MessagePackLibrary.java +45 -0
- data/ext/java/org/msgpack/jruby/Packer.java +266 -0
- data/ext/java/org/msgpack/jruby/Types.java +37 -0
- data/ext/java/org/msgpack/jruby/Unpacker.java +336 -0
- data/ext/msgpack/buffer.c +669 -0
- data/ext/msgpack/buffer.h +604 -0
- data/ext/msgpack/buffer_class.c +616 -0
- data/ext/msgpack/buffer_class.h +33 -0
- data/ext/msgpack/compat.h +26 -0
- data/ext/msgpack/extconf.rb +53 -0
- data/ext/msgpack/extension_value_class.c +34 -0
- data/ext/msgpack/extension_value_class.h +31 -0
- data/ext/msgpack/factory_class.c +276 -0
- data/ext/msgpack/factory_class.h +33 -0
- data/ext/msgpack/packer.c +199 -0
- data/ext/msgpack/packer.h +513 -0
- data/ext/msgpack/packer_class.c +442 -0
- data/ext/msgpack/packer_class.h +43 -0
- data/ext/msgpack/packer_ext_registry.c +74 -0
- data/ext/msgpack/packer_ext_registry.h +140 -0
- data/ext/msgpack/rbinit.c +35 -0
- data/ext/msgpack/rmem.c +93 -0
- data/ext/msgpack/rmem.h +109 -0
- data/ext/msgpack/sysdep.h +118 -0
- data/ext/msgpack/sysdep_endian.h +50 -0
- data/ext/msgpack/sysdep_types.h +46 -0
- data/ext/msgpack/unpacker.c +986 -0
- data/ext/msgpack/unpacker.h +152 -0
- data/ext/msgpack/unpacker_class.c +447 -0
- data/ext/msgpack/unpacker_class.h +43 -0
- data/ext/msgpack/unpacker_ext_registry.c +74 -0
- data/ext/msgpack/unpacker_ext_registry.h +62 -0
- data/lib/msgpack/bigint.rb +69 -0
- data/lib/msgpack/buffer.rb +9 -0
- data/lib/msgpack/core_ext.rb +139 -0
- data/lib/msgpack/factory.rb +211 -0
- data/lib/msgpack/packer.rb +37 -0
- data/lib/msgpack/symbol.rb +26 -0
- data/lib/msgpack/time.rb +29 -0
- data/lib/msgpack/timestamp.rb +76 -0
- data/lib/msgpack/unpacker.rb +41 -0
- data/lib/msgpack/version.rb +6 -0
- data/lib/msgpack.rb +53 -0
- data/msgpack.gemspec +41 -0
- metadata +216 -0
@@ -0,0 +1,604 @@
|
|
1
|
+
/*
|
2
|
+
* MessagePack for Ruby
|
3
|
+
*
|
4
|
+
* Copyright (C) 2008-2013 Sadayuki Furuhashi
|
5
|
+
*
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
* you may not use this file except in compliance with the License.
|
8
|
+
* You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
* See the License for the specific language governing permissions and
|
16
|
+
* limitations under the License.
|
17
|
+
*/
|
18
|
+
#ifndef MSGPACK_RUBY_BUFFER_H__
|
19
|
+
#define MSGPACK_RUBY_BUFFER_H__
|
20
|
+
|
21
|
+
#include "compat.h"
|
22
|
+
#include "sysdep.h"
|
23
|
+
|
24
|
+
#ifndef MSGPACK_BUFFER_STRING_WRITE_REFERENCE_DEFAULT
|
25
|
+
#define MSGPACK_BUFFER_STRING_WRITE_REFERENCE_DEFAULT (512*1024)
|
26
|
+
#endif
|
27
|
+
|
28
|
+
/* at least 23 (RSTRING_EMBED_LEN_MAX) bytes */
|
29
|
+
#ifndef MSGPACK_BUFFER_STRING_WRITE_REFERENCE_MINIMUM
|
30
|
+
#define MSGPACK_BUFFER_STRING_WRITE_REFERENCE_MINIMUM 256
|
31
|
+
#endif
|
32
|
+
|
33
|
+
#ifndef MSGPACK_BUFFER_STRING_READ_REFERENCE_DEFAULT
|
34
|
+
#define MSGPACK_BUFFER_STRING_READ_REFERENCE_DEFAULT 256
|
35
|
+
#endif
|
36
|
+
|
37
|
+
/* at least 23 (RSTRING_EMBED_LEN_MAX) bytes */
|
38
|
+
#ifndef MSGPACK_BUFFER_STRING_READ_REFERENCE_MINIMUM
|
39
|
+
#define MSGPACK_BUFFER_STRING_READ_REFERENCE_MINIMUM 256
|
40
|
+
#endif
|
41
|
+
|
42
|
+
#ifndef MSGPACK_BUFFER_IO_BUFFER_SIZE_DEFAULT
|
43
|
+
#define MSGPACK_BUFFER_IO_BUFFER_SIZE_DEFAULT (32*1024)
|
44
|
+
#endif
|
45
|
+
|
46
|
+
#ifndef MSGPACK_BUFFER_IO_BUFFER_SIZE_MINIMUM
|
47
|
+
#define MSGPACK_BUFFER_IO_BUFFER_SIZE_MINIMUM (1024)
|
48
|
+
#endif
|
49
|
+
|
50
|
+
#define NO_MAPPED_STRING ((VALUE)0)
|
51
|
+
|
52
|
+
#ifndef RB_ENC_INTERNED_STR_NULL_CHECK
|
53
|
+
#define RB_ENC_INTERNED_STR_NULL_CHECK 0
|
54
|
+
#endif
|
55
|
+
|
56
|
+
extern int msgpack_rb_encindex_utf8;
|
57
|
+
extern int msgpack_rb_encindex_usascii;
|
58
|
+
extern int msgpack_rb_encindex_ascii8bit;
|
59
|
+
|
60
|
+
extern ID s_uminus;
|
61
|
+
|
62
|
+
struct msgpack_buffer_chunk_t;
|
63
|
+
typedef struct msgpack_buffer_chunk_t msgpack_buffer_chunk_t;
|
64
|
+
|
65
|
+
struct msgpack_buffer_t;
|
66
|
+
typedef struct msgpack_buffer_t msgpack_buffer_t;
|
67
|
+
|
68
|
+
/*
|
69
|
+
* msgpack_buffer_chunk_t
|
70
|
+
* +----------------+
|
71
|
+
* | filled | free |
|
72
|
+
* +---------+------+
|
73
|
+
* ^ first ^ last
|
74
|
+
*/
|
75
|
+
struct msgpack_buffer_chunk_t {
|
76
|
+
char* first;
|
77
|
+
char* last;
|
78
|
+
void* mem;
|
79
|
+
msgpack_buffer_chunk_t* next;
|
80
|
+
VALUE mapped_string; /* RBString or NO_MAPPED_STRING */
|
81
|
+
bool rmem;
|
82
|
+
};
|
83
|
+
|
84
|
+
struct msgpack_buffer_t {
|
85
|
+
char* read_buffer;
|
86
|
+
char* tail_buffer_end;
|
87
|
+
|
88
|
+
msgpack_buffer_chunk_t tail;
|
89
|
+
msgpack_buffer_chunk_t* head;
|
90
|
+
msgpack_buffer_chunk_t* free_list;
|
91
|
+
|
92
|
+
char* rmem_last;
|
93
|
+
char* rmem_end;
|
94
|
+
void** rmem_owner;
|
95
|
+
|
96
|
+
VALUE io;
|
97
|
+
VALUE io_buffer;
|
98
|
+
ID io_write_all_method;
|
99
|
+
ID io_partial_read_method;
|
100
|
+
|
101
|
+
size_t write_reference_threshold;
|
102
|
+
size_t read_reference_threshold;
|
103
|
+
size_t io_buffer_size;
|
104
|
+
};
|
105
|
+
|
106
|
+
/*
|
107
|
+
* initialization functions
|
108
|
+
*/
|
109
|
+
void msgpack_buffer_static_init(void);
|
110
|
+
|
111
|
+
void msgpack_buffer_static_destroy(void);
|
112
|
+
|
113
|
+
void msgpack_buffer_init(msgpack_buffer_t* b);
|
114
|
+
|
115
|
+
void msgpack_buffer_destroy(msgpack_buffer_t* b);
|
116
|
+
|
117
|
+
void msgpack_buffer_mark(void* b);
|
118
|
+
|
119
|
+
void msgpack_buffer_clear(msgpack_buffer_t* b);
|
120
|
+
|
121
|
+
size_t msgpack_buffer_memsize(const msgpack_buffer_t* b);
|
122
|
+
|
123
|
+
static inline void msgpack_buffer_set_write_reference_threshold(msgpack_buffer_t* b, size_t length)
|
124
|
+
{
|
125
|
+
if(length < MSGPACK_BUFFER_STRING_WRITE_REFERENCE_MINIMUM) {
|
126
|
+
length = MSGPACK_BUFFER_STRING_WRITE_REFERENCE_MINIMUM;
|
127
|
+
}
|
128
|
+
b->write_reference_threshold = length;
|
129
|
+
}
|
130
|
+
|
131
|
+
static inline void msgpack_buffer_set_read_reference_threshold(msgpack_buffer_t* b, size_t length)
|
132
|
+
{
|
133
|
+
if(length < MSGPACK_BUFFER_STRING_READ_REFERENCE_MINIMUM) {
|
134
|
+
length = MSGPACK_BUFFER_STRING_READ_REFERENCE_MINIMUM;
|
135
|
+
}
|
136
|
+
b->read_reference_threshold = length;
|
137
|
+
}
|
138
|
+
|
139
|
+
static inline void msgpack_buffer_set_io_buffer_size(msgpack_buffer_t* b, size_t length)
|
140
|
+
{
|
141
|
+
if(length < MSGPACK_BUFFER_IO_BUFFER_SIZE_MINIMUM) {
|
142
|
+
length = MSGPACK_BUFFER_IO_BUFFER_SIZE_MINIMUM;
|
143
|
+
}
|
144
|
+
b->io_buffer_size = length;
|
145
|
+
}
|
146
|
+
|
147
|
+
static inline void msgpack_buffer_reset_io(msgpack_buffer_t* b)
|
148
|
+
{
|
149
|
+
b->io = Qnil;
|
150
|
+
}
|
151
|
+
|
152
|
+
static inline bool msgpack_buffer_has_io(msgpack_buffer_t* b)
|
153
|
+
{
|
154
|
+
return b->io != Qnil;
|
155
|
+
}
|
156
|
+
|
157
|
+
static inline void msgpack_buffer_reset(msgpack_buffer_t* b)
|
158
|
+
{
|
159
|
+
msgpack_buffer_clear(b);
|
160
|
+
msgpack_buffer_reset_io(b);
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
/*
|
165
|
+
* writer functions
|
166
|
+
*/
|
167
|
+
|
168
|
+
static inline size_t msgpack_buffer_writable_size(const msgpack_buffer_t* b)
|
169
|
+
{
|
170
|
+
return b->tail_buffer_end - b->tail.last;
|
171
|
+
}
|
172
|
+
|
173
|
+
static inline void msgpack_buffer_write_1(msgpack_buffer_t* b, int byte)
|
174
|
+
{
|
175
|
+
(*b->tail.last++) = (char) byte;
|
176
|
+
}
|
177
|
+
|
178
|
+
static inline void msgpack_buffer_write_2(msgpack_buffer_t* b, int byte1, unsigned char byte2)
|
179
|
+
{
|
180
|
+
*(b->tail.last++) = (char) byte1;
|
181
|
+
*(b->tail.last++) = (char) byte2;
|
182
|
+
}
|
183
|
+
|
184
|
+
static inline void msgpack_buffer_write_byte_and_data(msgpack_buffer_t* b, int byte, const void* data, size_t length)
|
185
|
+
{
|
186
|
+
(*b->tail.last++) = (char) byte;
|
187
|
+
|
188
|
+
memcpy(b->tail.last, data, length);
|
189
|
+
b->tail.last += length;
|
190
|
+
}
|
191
|
+
|
192
|
+
void _msgpack_buffer_expand(msgpack_buffer_t* b, const char* data, size_t length, bool use_flush);
|
193
|
+
|
194
|
+
size_t msgpack_buffer_flush_to_io(msgpack_buffer_t* b, VALUE io, ID write_method, bool consume);
|
195
|
+
|
196
|
+
static inline size_t msgpack_buffer_flush(msgpack_buffer_t* b)
|
197
|
+
{
|
198
|
+
if(b->io == Qnil) {
|
199
|
+
return 0;
|
200
|
+
}
|
201
|
+
return msgpack_buffer_flush_to_io(b, b->io, b->io_write_all_method, true);
|
202
|
+
}
|
203
|
+
|
204
|
+
static inline void msgpack_buffer_ensure_writable(msgpack_buffer_t* b, size_t require)
|
205
|
+
{
|
206
|
+
if(msgpack_buffer_writable_size(b) < require) {
|
207
|
+
_msgpack_buffer_expand(b, NULL, require, true);
|
208
|
+
}
|
209
|
+
}
|
210
|
+
|
211
|
+
static inline void _msgpack_buffer_append_impl(msgpack_buffer_t* b, const char* data, size_t length, bool flush_to_io)
|
212
|
+
{
|
213
|
+
if(length == 0) {
|
214
|
+
return;
|
215
|
+
}
|
216
|
+
|
217
|
+
if(length <= msgpack_buffer_writable_size(b)) {
|
218
|
+
memcpy(b->tail.last, data, length);
|
219
|
+
b->tail.last += length;
|
220
|
+
return;
|
221
|
+
}
|
222
|
+
|
223
|
+
_msgpack_buffer_expand(b, data, length, flush_to_io);
|
224
|
+
}
|
225
|
+
|
226
|
+
static inline void msgpack_buffer_append(msgpack_buffer_t* b, const char* data, size_t length)
|
227
|
+
{
|
228
|
+
_msgpack_buffer_append_impl(b, data, length, true);
|
229
|
+
}
|
230
|
+
|
231
|
+
static inline void msgpack_buffer_append_nonblock(msgpack_buffer_t* b, const char* data, size_t length)
|
232
|
+
{
|
233
|
+
_msgpack_buffer_append_impl(b, data, length, false);
|
234
|
+
}
|
235
|
+
|
236
|
+
void _msgpack_buffer_append_long_string(msgpack_buffer_t* b, VALUE string);
|
237
|
+
|
238
|
+
static inline size_t msgpack_buffer_append_string(msgpack_buffer_t* b, VALUE string)
|
239
|
+
{
|
240
|
+
size_t length;
|
241
|
+
char *ptr;
|
242
|
+
RSTRING_GETMEM(string, ptr, length);
|
243
|
+
|
244
|
+
if(length > b->write_reference_threshold) {
|
245
|
+
_msgpack_buffer_append_long_string(b, string);
|
246
|
+
} else {
|
247
|
+
msgpack_buffer_append(b, ptr, length);
|
248
|
+
}
|
249
|
+
|
250
|
+
return length;
|
251
|
+
}
|
252
|
+
|
253
|
+
static inline size_t msgpack_buffer_append_string_reference(msgpack_buffer_t* b, VALUE string)
|
254
|
+
{
|
255
|
+
size_t length = RSTRING_LEN(string);
|
256
|
+
if (length > 0) {
|
257
|
+
_msgpack_buffer_append_long_string(b, string);
|
258
|
+
}
|
259
|
+
return length;
|
260
|
+
}
|
261
|
+
|
262
|
+
|
263
|
+
/*
|
264
|
+
* IO functions
|
265
|
+
*/
|
266
|
+
size_t _msgpack_buffer_feed_from_io(msgpack_buffer_t* b);
|
267
|
+
|
268
|
+
size_t _msgpack_buffer_read_from_io_to_string(msgpack_buffer_t* b, VALUE string, size_t length);
|
269
|
+
|
270
|
+
size_t _msgpack_buffer_skip_from_io(msgpack_buffer_t* b, size_t length);
|
271
|
+
|
272
|
+
|
273
|
+
/*
|
274
|
+
* reader functions
|
275
|
+
*/
|
276
|
+
|
277
|
+
static inline size_t msgpack_buffer_top_readable_size(const msgpack_buffer_t* b)
|
278
|
+
{
|
279
|
+
return b->head->last - b->read_buffer;
|
280
|
+
}
|
281
|
+
|
282
|
+
size_t msgpack_buffer_all_readable_size(const msgpack_buffer_t* b);
|
283
|
+
|
284
|
+
bool _msgpack_buffer_shift_chunk(msgpack_buffer_t* b);
|
285
|
+
|
286
|
+
static inline void _msgpack_buffer_consumed(msgpack_buffer_t* b, size_t length)
|
287
|
+
{
|
288
|
+
b->read_buffer += length;
|
289
|
+
if(b->read_buffer >= b->head->last) {
|
290
|
+
_msgpack_buffer_shift_chunk(b);
|
291
|
+
}
|
292
|
+
}
|
293
|
+
|
294
|
+
static inline int msgpack_buffer_peek_top_1(msgpack_buffer_t* b)
|
295
|
+
{
|
296
|
+
return (int) (unsigned char) b->read_buffer[0];
|
297
|
+
}
|
298
|
+
|
299
|
+
static inline int msgpack_buffer_read_top_1(msgpack_buffer_t* b)
|
300
|
+
{
|
301
|
+
int r = (int) (unsigned char) b->read_buffer[0];
|
302
|
+
|
303
|
+
_msgpack_buffer_consumed(b, 1);
|
304
|
+
|
305
|
+
return r;
|
306
|
+
}
|
307
|
+
|
308
|
+
static inline int msgpack_buffer_read_1(msgpack_buffer_t* b)
|
309
|
+
{
|
310
|
+
if(msgpack_buffer_top_readable_size(b) <= 0) {
|
311
|
+
if(b->io == Qnil) {
|
312
|
+
return -1;
|
313
|
+
}
|
314
|
+
_msgpack_buffer_feed_from_io(b);
|
315
|
+
}
|
316
|
+
|
317
|
+
int r = (int) (unsigned char) b->read_buffer[0];
|
318
|
+
_msgpack_buffer_consumed(b, 1);
|
319
|
+
|
320
|
+
return r;
|
321
|
+
}
|
322
|
+
|
323
|
+
|
324
|
+
/*
|
325
|
+
* bulk read / skip functions
|
326
|
+
*/
|
327
|
+
|
328
|
+
size_t msgpack_buffer_read_nonblock(msgpack_buffer_t* b, char* buffer, size_t length);
|
329
|
+
|
330
|
+
static inline bool msgpack_buffer_ensure_readable(msgpack_buffer_t* b, size_t require)
|
331
|
+
{
|
332
|
+
if(msgpack_buffer_top_readable_size(b) < require) {
|
333
|
+
size_t sz = msgpack_buffer_all_readable_size(b);
|
334
|
+
if(sz < require) {
|
335
|
+
if(b->io == Qnil) {
|
336
|
+
return false;
|
337
|
+
}
|
338
|
+
do {
|
339
|
+
size_t rl = _msgpack_buffer_feed_from_io(b);
|
340
|
+
sz += rl;
|
341
|
+
} while(sz < require);
|
342
|
+
}
|
343
|
+
}
|
344
|
+
return true;
|
345
|
+
}
|
346
|
+
|
347
|
+
bool _msgpack_buffer_read_all2(msgpack_buffer_t* b, char* buffer, size_t length);
|
348
|
+
|
349
|
+
static inline bool msgpack_buffer_read_all(msgpack_buffer_t* b, char* buffer, size_t length)
|
350
|
+
{
|
351
|
+
size_t avail = msgpack_buffer_top_readable_size(b);
|
352
|
+
if(avail < length) {
|
353
|
+
return _msgpack_buffer_read_all2(b, buffer, length);
|
354
|
+
}
|
355
|
+
|
356
|
+
memcpy(buffer, b->read_buffer, length);
|
357
|
+
_msgpack_buffer_consumed(b, length);
|
358
|
+
return true;
|
359
|
+
}
|
360
|
+
|
361
|
+
static inline size_t msgpack_buffer_skip_nonblock(msgpack_buffer_t* b, size_t length)
|
362
|
+
{
|
363
|
+
size_t avail = msgpack_buffer_top_readable_size(b);
|
364
|
+
if(avail < length) {
|
365
|
+
return msgpack_buffer_read_nonblock(b, NULL, length);
|
366
|
+
}
|
367
|
+
_msgpack_buffer_consumed(b, length);
|
368
|
+
return length;
|
369
|
+
}
|
370
|
+
|
371
|
+
size_t msgpack_buffer_read_to_string_nonblock(msgpack_buffer_t* b, VALUE string, size_t length);
|
372
|
+
|
373
|
+
static inline size_t msgpack_buffer_read_to_string(msgpack_buffer_t* b, VALUE string, size_t length)
|
374
|
+
{
|
375
|
+
if(length == 0) {
|
376
|
+
return 0;
|
377
|
+
}
|
378
|
+
|
379
|
+
size_t avail = msgpack_buffer_top_readable_size(b);
|
380
|
+
if(avail > 0) {
|
381
|
+
return msgpack_buffer_read_to_string_nonblock(b, string, length);
|
382
|
+
} else if(b->io != Qnil) {
|
383
|
+
return _msgpack_buffer_read_from_io_to_string(b, string, length);
|
384
|
+
} else {
|
385
|
+
return 0;
|
386
|
+
}
|
387
|
+
}
|
388
|
+
|
389
|
+
static inline size_t msgpack_buffer_skip(msgpack_buffer_t* b, size_t length)
|
390
|
+
{
|
391
|
+
if(length == 0) {
|
392
|
+
return 0;
|
393
|
+
}
|
394
|
+
|
395
|
+
size_t avail = msgpack_buffer_top_readable_size(b);
|
396
|
+
if(avail > 0) {
|
397
|
+
return msgpack_buffer_skip_nonblock(b, length);
|
398
|
+
} else if(b->io != Qnil) {
|
399
|
+
return _msgpack_buffer_skip_from_io(b, length);
|
400
|
+
} else {
|
401
|
+
return 0;
|
402
|
+
}
|
403
|
+
}
|
404
|
+
|
405
|
+
|
406
|
+
VALUE msgpack_buffer_all_as_string(msgpack_buffer_t* b);
|
407
|
+
|
408
|
+
VALUE msgpack_buffer_all_as_string_array(msgpack_buffer_t* b);
|
409
|
+
|
410
|
+
static inline VALUE _msgpack_buffer_refer_head_mapped_string(msgpack_buffer_t* b, size_t length)
|
411
|
+
{
|
412
|
+
size_t offset = b->read_buffer - b->head->first;
|
413
|
+
return rb_str_substr(b->head->mapped_string, offset, length);
|
414
|
+
}
|
415
|
+
|
416
|
+
static inline VALUE msgpack_buffer_read_top_as_string(msgpack_buffer_t* b, size_t length, bool will_be_frozen, bool utf8)
|
417
|
+
{
|
418
|
+
/* optimize */
|
419
|
+
if(!will_be_frozen &&
|
420
|
+
b->head->mapped_string != NO_MAPPED_STRING &&
|
421
|
+
length >= b->read_reference_threshold) {
|
422
|
+
VALUE result = _msgpack_buffer_refer_head_mapped_string(b, length);
|
423
|
+
if (utf8) ENCODING_SET(result, msgpack_rb_encindex_utf8);
|
424
|
+
_msgpack_buffer_consumed(b, length);
|
425
|
+
return result;
|
426
|
+
}
|
427
|
+
|
428
|
+
VALUE result;
|
429
|
+
|
430
|
+
#ifdef HAVE_RB_ENC_INTERNED_STR
|
431
|
+
if (will_be_frozen) {
|
432
|
+
if (RB_ENC_INTERNED_STR_NULL_CHECK && length == 0) {
|
433
|
+
result = rb_enc_interned_str("", length, utf8 ? rb_utf8_encoding() : rb_ascii8bit_encoding());
|
434
|
+
} else {
|
435
|
+
result = rb_enc_interned_str(b->read_buffer, length, utf8 ? rb_utf8_encoding() : rb_ascii8bit_encoding());
|
436
|
+
}
|
437
|
+
} else {
|
438
|
+
if (utf8) {
|
439
|
+
result = rb_utf8_str_new(b->read_buffer, length);
|
440
|
+
} else {
|
441
|
+
result = rb_str_new(b->read_buffer, length);
|
442
|
+
}
|
443
|
+
}
|
444
|
+
_msgpack_buffer_consumed(b, length);
|
445
|
+
return result;
|
446
|
+
|
447
|
+
#else
|
448
|
+
|
449
|
+
if (utf8) {
|
450
|
+
result = rb_utf8_str_new(b->read_buffer, length);
|
451
|
+
} else {
|
452
|
+
result = rb_str_new(b->read_buffer, length);
|
453
|
+
}
|
454
|
+
|
455
|
+
if (will_be_frozen) {
|
456
|
+
#if STR_UMINUS_DEDUPE_FROZEN
|
457
|
+
// Starting from MRI 2.8 it is preferable to freeze the string
|
458
|
+
// before deduplication so that it can be interned directly
|
459
|
+
// otherwise it would be duplicated first which is wasteful.
|
460
|
+
rb_str_freeze(result);
|
461
|
+
#endif //STR_UMINUS_DEDUPE_FROZEN
|
462
|
+
// MRI 2.5 and older do not deduplicate strings that are already
|
463
|
+
// frozen.
|
464
|
+
result = rb_funcall(result, s_uminus, 0);
|
465
|
+
}
|
466
|
+
_msgpack_buffer_consumed(b, length);
|
467
|
+
return result;
|
468
|
+
|
469
|
+
#endif // HAVE_RB_ENC_INTERNED_STR
|
470
|
+
}
|
471
|
+
|
472
|
+
static inline VALUE msgpack_buffer_read_top_as_symbol(msgpack_buffer_t* b, size_t length, bool utf8)
|
473
|
+
{
|
474
|
+
return rb_str_intern(msgpack_buffer_read_top_as_string(b, length, true, utf8));
|
475
|
+
}
|
476
|
+
|
477
|
+
// Hash keys are likely to be repeated, and are frozen.
|
478
|
+
// As such we can re-use them if we keep a cache of the ones we've seen so far,
|
479
|
+
// and save much more expensive lookups into the global fstring table.
|
480
|
+
// This cache implementation is deliberately simple, as we're optimizing for compactness,
|
481
|
+
// to be able to fit easily embeded inside msgpack_unpacker_t.
|
482
|
+
// As such, binary search into a sorted array gives a good tradeoff between compactness and
|
483
|
+
// performance.
|
484
|
+
#define MSGPACK_KEY_CACHE_CAPACITY 63
|
485
|
+
|
486
|
+
typedef struct msgpack_key_cache_t msgpack_key_cache_t;
|
487
|
+
struct msgpack_key_cache_t {
|
488
|
+
int length;
|
489
|
+
VALUE entries[MSGPACK_KEY_CACHE_CAPACITY];
|
490
|
+
};
|
491
|
+
|
492
|
+
static inline VALUE build_interned_string(const char *str, const long length)
|
493
|
+
{
|
494
|
+
# ifdef HAVE_RB_ENC_INTERNED_STR
|
495
|
+
return rb_enc_interned_str(str, length, rb_utf8_encoding());
|
496
|
+
# else
|
497
|
+
VALUE rstring = rb_utf8_str_new(str, length);
|
498
|
+
return rb_funcall(rb_str_freeze(rstring), s_uminus, 0);
|
499
|
+
# endif
|
500
|
+
}
|
501
|
+
|
502
|
+
static inline VALUE build_symbol(const char *str, const long length)
|
503
|
+
{
|
504
|
+
return rb_str_intern(build_interned_string(str, length));
|
505
|
+
}
|
506
|
+
|
507
|
+
static void rvalue_cache_insert_at(msgpack_key_cache_t *cache, int index, VALUE rstring)
|
508
|
+
{
|
509
|
+
MEMMOVE(&cache->entries[index + 1], &cache->entries[index], VALUE, cache->length - index);
|
510
|
+
cache->length++;
|
511
|
+
cache->entries[index] = rstring;
|
512
|
+
}
|
513
|
+
|
514
|
+
static inline int rstring_cache_cmp(const char *str, const long length, VALUE rstring)
|
515
|
+
{
|
516
|
+
long rstring_length = RSTRING_LEN(rstring);
|
517
|
+
if (length == rstring_length) {
|
518
|
+
return memcmp(str, RSTRING_PTR(rstring), length);
|
519
|
+
} else {
|
520
|
+
return (int)(length - rstring_length);
|
521
|
+
}
|
522
|
+
}
|
523
|
+
|
524
|
+
static VALUE rstring_cache_fetch(msgpack_key_cache_t *cache, const char *str, const long length)
|
525
|
+
{
|
526
|
+
int low = 0;
|
527
|
+
int high = cache->length - 1;
|
528
|
+
int mid = 0;
|
529
|
+
int last_cmp = 0;
|
530
|
+
|
531
|
+
while (low <= high) {
|
532
|
+
mid = (high + low) >> 1;
|
533
|
+
VALUE entry = cache->entries[mid];
|
534
|
+
last_cmp = rstring_cache_cmp(str, length, entry);
|
535
|
+
|
536
|
+
if (last_cmp == 0) {
|
537
|
+
return entry;
|
538
|
+
} else if (last_cmp > 0) {
|
539
|
+
low = mid + 1;
|
540
|
+
} else {
|
541
|
+
high = mid - 1;
|
542
|
+
}
|
543
|
+
}
|
544
|
+
|
545
|
+
VALUE rstring = build_interned_string(str, length);
|
546
|
+
|
547
|
+
if (cache->length < MSGPACK_KEY_CACHE_CAPACITY) {
|
548
|
+
if (last_cmp > 0) {
|
549
|
+
mid += 1;
|
550
|
+
}
|
551
|
+
|
552
|
+
rvalue_cache_insert_at(cache, mid, rstring);
|
553
|
+
}
|
554
|
+
return rstring;
|
555
|
+
}
|
556
|
+
|
557
|
+
static VALUE rsymbol_cache_fetch(msgpack_key_cache_t *cache, const char *str, const long length)
|
558
|
+
{
|
559
|
+
int low = 0;
|
560
|
+
int high = cache->length - 1;
|
561
|
+
int mid = 0;
|
562
|
+
int last_cmp = 0;
|
563
|
+
|
564
|
+
while (low <= high) {
|
565
|
+
mid = (high + low) >> 1;
|
566
|
+
VALUE entry = cache->entries[mid];
|
567
|
+
last_cmp = rstring_cache_cmp(str, length, rb_sym2str(entry));
|
568
|
+
|
569
|
+
if (last_cmp == 0) {
|
570
|
+
return entry;
|
571
|
+
} else if (last_cmp > 0) {
|
572
|
+
low = mid + 1;
|
573
|
+
} else {
|
574
|
+
high = mid - 1;
|
575
|
+
}
|
576
|
+
}
|
577
|
+
|
578
|
+
VALUE rsymbol = build_symbol(str, length);
|
579
|
+
|
580
|
+
if (cache->length < MSGPACK_KEY_CACHE_CAPACITY) {
|
581
|
+
if (last_cmp > 0) {
|
582
|
+
mid += 1;
|
583
|
+
}
|
584
|
+
|
585
|
+
rvalue_cache_insert_at(cache, mid, rsymbol);
|
586
|
+
}
|
587
|
+
return rsymbol;
|
588
|
+
}
|
589
|
+
|
590
|
+
static inline VALUE msgpack_buffer_read_top_as_interned_symbol(msgpack_buffer_t* b, msgpack_key_cache_t *cache, size_t length)
|
591
|
+
{
|
592
|
+
VALUE result = rsymbol_cache_fetch(cache, b->read_buffer, length);
|
593
|
+
_msgpack_buffer_consumed(b, length);
|
594
|
+
return result;
|
595
|
+
}
|
596
|
+
|
597
|
+
static inline VALUE msgpack_buffer_read_top_as_interned_string(msgpack_buffer_t* b, msgpack_key_cache_t *cache, size_t length)
|
598
|
+
{
|
599
|
+
VALUE result = rstring_cache_fetch(cache, b->read_buffer, length);
|
600
|
+
_msgpack_buffer_consumed(b, length);
|
601
|
+
return result;
|
602
|
+
}
|
603
|
+
|
604
|
+
#endif
|