Skip to content

Commit a8fe3e4

Browse files
authored
Merge pull request #379 from Shopify/micro-opt
Various micro optimizations
2 parents eb4859d + 6365387 commit a8fe3e4

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

ext/msgpack/buffer.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ static inline void _msgpack_buffer_add_new_chunk(msgpack_buffer_t* b)
300300
static inline void _msgpack_buffer_append_reference(msgpack_buffer_t* b, VALUE string)
301301
{
302302
VALUE mapped_string;
303-
if(ENCODING_GET(string) == msgpack_rb_encindex_ascii8bit && RTEST(rb_obj_frozen_p(string))) {
303+
if(ENCODING_GET_INLINED(string) == msgpack_rb_encindex_ascii8bit && RB_OBJ_FROZEN_RAW(string)) {
304304
mapped_string = string;
305305
} else {
306306
mapped_string = rb_str_dup(string);
@@ -309,8 +309,9 @@ static inline void _msgpack_buffer_append_reference(msgpack_buffer_t* b, VALUE s
309309

310310
_msgpack_buffer_add_new_chunk(b);
311311

312-
char* data = RSTRING_PTR(mapped_string);
313-
size_t length = RSTRING_LEN(mapped_string);
312+
char* data;
313+
size_t length;
314+
RSTRING_GETMEM(mapped_string, data, length);
314315

315316
b->tail.first = (char*) data;
316317
b->tail.last = (char*) data + length;
@@ -330,7 +331,7 @@ void _msgpack_buffer_append_long_string(msgpack_buffer_t* b, VALUE string)
330331
{
331332
if(b->io != Qnil) {
332333
msgpack_buffer_flush(b);
333-
if (ENCODING_GET(string) == msgpack_rb_encindex_ascii8bit) {
334+
if (ENCODING_GET_INLINED(string) == msgpack_rb_encindex_ascii8bit) {
334335
rb_funcall(b->io, b->io_write_all_method, 1, string);
335336
} else {
336337
msgpack_buffer_append(b, RSTRING_PTR(string), RSTRING_LEN(string));

ext/msgpack/buffer.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,14 @@ void _msgpack_buffer_append_long_string(msgpack_buffer_t* b, VALUE string);
237237

238238
static inline size_t msgpack_buffer_append_string(msgpack_buffer_t* b, VALUE string)
239239
{
240-
size_t length = RSTRING_LEN(string);
240+
size_t length;
241+
char *ptr;
242+
RSTRING_GETMEM(string, ptr, length);
241243

242244
if(length > b->write_reference_threshold) {
243245
_msgpack_buffer_append_long_string(b, string);
244-
245246
} else {
246-
msgpack_buffer_append(b, RSTRING_PTR(string), length);
247+
msgpack_buffer_append(b, ptr, length);
247248
}
248249

249250
return length;

ext/msgpack/extconf.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
"-fvisibility=hidden",
1010
"-I..",
1111
"-Wall",
12-
"-O3",
1312
"-std=gnu99"
1413
])
15-
append_cflags(RbConfig::CONFIG["debugflags"]) if RbConfig::CONFIG["debugflags"]
1614

17-
append_cflags("-DRUBY_DEBUG=1") if ENV["MSGPACK_DEBUG"]
15+
if ENV["MSGPACK_DEBUG"]
16+
append_cflags(RbConfig::CONFIG["debugflags"]) if RbConfig::CONFIG["debugflags"]
17+
append_cflags("-DRUBY_DEBUG=1")
18+
end
1819

1920
if RUBY_VERSION.start_with?('3.0.') && RUBY_VERSION <= '3.0.5'
2021
# https://bugs.ruby-lang.org/issues/18772

ext/msgpack/packer.h

+20-9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
#define MSGPACK_PACKER_IO_FLUSH_THRESHOLD_TO_WRITE_STRING_BODY (1024)
2626
#endif
2727

28+
#ifndef UNREACHABLE_RETURN
29+
// Ruby 2.5
30+
#define UNREACHABLE_RETURN() return
31+
#endif
32+
2833
struct msgpack_packer_t;
2934
typedef struct msgpack_packer_t msgpack_packer_t;
3035

@@ -404,27 +409,33 @@ static inline bool msgpack_packer_is_utf8_compat_string(VALUE v, int encindex)
404409
{
405410
return encindex == msgpack_rb_encindex_utf8
406411
|| encindex == msgpack_rb_encindex_usascii
407-
|| (rb_enc_asciicompat(rb_enc_from_index(encindex)) && ENC_CODERANGE_ASCIIONLY(v));
412+
|| ENC_CODERANGE_ASCIIONLY(v);
408413
}
409414

410415
static inline void msgpack_packer_write_string_value(msgpack_packer_t* pk, VALUE v)
411416
{
412-
/* actual return type of RSTRING_LEN is long */
413-
unsigned long len = RSTRING_LEN(v);
414-
if(len > 0xffffffffUL) {
415-
// TODO rb_eArgError?
416-
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();
422+
}
423+
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;
417428
}
418429

419-
int encindex = ENCODING_GET(v);
420-
if(msgpack_packer_is_binary(v, encindex) && !pk->compatibility_mode) {
430+
int encindex = ENCODING_GET_INLINED(v);
431+
if(msgpack_packer_is_binary(v, encindex)) {
421432
/* write ASCII-8BIT string using Binary type */
422433
msgpack_packer_write_bin_header(pk, (unsigned int)len);
423434
msgpack_buffer_append_string(PACKER_BUFFER_(pk), v);
424435
} else {
425436
/* write UTF-8, US-ASCII, or 7bit-safe ascii-compatible string using String type directly */
426437
/* in compatibility mode, packer packs String values as is */
427-
if(!pk->compatibility_mode && !msgpack_packer_is_utf8_compat_string(v, encindex)) {
438+
if(RB_UNLIKELY(!msgpack_packer_is_utf8_compat_string(v, encindex))) {
428439
/* transcode other strings to UTF-8 and write using String type */
429440
VALUE enc = rb_enc_from_encoding(rb_utf8_encoding()); /* rb_enc_from_encoding_index is not extern */
430441
v = rb_str_encode(v, enc, 0, Qnil);

0 commit comments

Comments
 (0)