Allow Array#join to allocate smaller strings

rb_str_buf_new always allocates at least 127 bytes of capacity, even
when less is requested.

    > ObjectSpace.dump(%w[a b c].join)
    {"address":"0x7f935f06ebf0", "type":"STRING", "class":"0x7f935d8b7bb0", "bytesize":3, "capacity":127, "value":"abc", "encoding":"UTF-8", "memsize":168, "flags":{"wb_protected":true}}

Instead, by using rb_str_new and then setting the length to 0, we can
allocate the exact amount of memory needed, without extra capacity.

    > ObjectSpace.dump(%w[a b c].join)
    {"address":"0x7f903fcab530", "type":"STRING", "class":"0x7f903f8b7988", "embedded":true, "bytesize":3, "value":"abc", "encoding":"UTF-8", "memsize":40, "flags":{"wb_protected":true}}
This commit is contained in:
John Hawthorn 2019-06-26 09:52:30 -07:00 коммит произвёл Aaron Patterson
Родитель b1678338e5
Коммит 9d298b9dab
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 953170BCB4FFAFC6
1 изменённых файлов: 3 добавлений и 1 удалений

Просмотреть файл

@ -2368,7 +2368,9 @@ rb_ary_join(VALUE ary, VALUE sep)
len += RSTRING_LEN(tmp); len += RSTRING_LEN(tmp);
} }
result = rb_str_buf_new(len); result = rb_str_new(0, len);
rb_str_set_len(result, 0);
if (taint) OBJ_TAINT(result); if (taint) OBJ_TAINT(result);
ary_join_0(ary, sep, RARRAY_LEN(ary), result); ary_join_0(ary, sep, RARRAY_LEN(ary), result);