diff --git a/benchmark/string_concat.yml b/benchmark/string_concat.yml index 656bcd1cd79889..0ff1dc25b63351 100644 --- a/benchmark/string_concat.yml +++ b/benchmark/string_concat.yml @@ -1,6 +1,7 @@ prelude: | CHUNK = "a" * 64 BCHUNK = "a".b * 64 + GC.disable # GC causes a lot of variance benchmark: binary_concat_utf8: | buffer = String.new(capacity: 4096, encoding: Encoding::BINARY) @@ -11,7 +12,7 @@ benchmark: buffer << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK buffer << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK buffer << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK - buffer << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK + buffer << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK binary_concat_binary: | buffer = String.new(capacity: 4096, encoding: Encoding::BINARY) buffer << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK @@ -21,7 +22,7 @@ benchmark: buffer << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK buffer << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK buffer << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK - buffer << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK + buffer << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK << BCHUNK utf8_concat_utf8: | buffer = String.new(capacity: 4096, encoding: Encoding::UTF_8) buffer << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK @@ -31,4 +32,4 @@ benchmark: buffer << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK buffer << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK buffer << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK - buffer << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK + buffer << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK << CHUNK diff --git a/string.c b/string.c index 8f519da7d10447..c726adb2c38e79 100644 --- a/string.c +++ b/string.c @@ -150,7 +150,21 @@ VALUE rb_cSymbol; }\ } while (0) -#define TERM_LEN(str) rb_enc_mbminlen(rb_enc_get(str)) +static inline bool +str_enc_fastpath(VALUE str) +{ + // The overwhelming majority of strings are in one of these 3 encodings. + switch (ENCODING_GET_INLINED(str)) { + case ENCINDEX_ASCII_8BIT: + case ENCINDEX_UTF_8: + case ENCINDEX_US_ASCII: + return true; + default: + return false; + } +} + +#define TERM_LEN(str) (str_enc_fastpath(str) ? 1 : rb_enc_mbminlen(rb_enc_from_index(ENCODING_GET(str)))) #define TERM_FILL(ptr, termlen) do {\ char *const term_fill_ptr = (ptr);\ const int term_fill_len = (termlen);\ @@ -3311,20 +3325,6 @@ rb_str_buf_cat_ascii(VALUE str, const char *ptr) } } -static inline bool -str_enc_fastpath(VALUE str) -{ - // The overwhelming majority of strings are in one of these 3 encodings. - switch (ENCODING_GET_INLINED(str)) { - case ENCINDEX_ASCII_8BIT: - case ENCINDEX_UTF_8: - case ENCINDEX_US_ASCII: - return true; - default: - return false; - } -} - VALUE rb_str_buf_append(VALUE str, VALUE str2) {