Skip to content

Commit 3b001bd

Browse files
committed
[GR-16389] Change rb_str_cat to avoid conversion to and from native strings.
PullRequest: truffleruby/884
2 parents e400a51 + cb8b0c5 commit 3b001bd

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Performance:
3030

3131
* `eval(code, binding)` for a fixed `code` containing blocks is now much faster. This improves the performance of rendering `ERB` templates containing loops.
3232

33+
Performance:
34+
35+
* `rb_str_cat` has been changed to improve performance. THe C string is now concatentated without first being converted to a Ruby string or having its encoding checked. As a side effect the behaviour of `rb_str_cat` should now more closely match that of MRI.
36+
3337
# 20.0.0 beta 1
3438

3539
Bug fixes:

src/main/c/cext/ruby.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -879,13 +879,22 @@ ID rb_intern_str(VALUE string) {
879879
}
880880

881881
VALUE rb_str_cat(VALUE string, const char *to_concat, long length) {
882-
polyglot_invoke(rb_tr_unwrap(string), "concat", rb_tr_unwrap(rb_enc_str_new(to_concat, length, STR_ENC_GET(string))));
882+
if (length == 0) {
883+
return string;
884+
}
885+
if (length < 0) {
886+
rb_raise(rb_eArgError, "negative string size (or size too big)");
887+
}
888+
int old_length = RSTRING_LEN(string);
889+
rb_str_resize(string, old_length + length);
890+
// Resizing the string will clear out the code range, so there is no
891+
// need to do it explicitly.
892+
memcpy(RSTRING_PTR(string) + old_length, to_concat, length);
883893
return string;
884894
}
885895

886896
VALUE rb_str_cat2(VALUE string, const char *to_concat) {
887-
polyglot_invoke(rb_tr_unwrap(string), "concat", rb_tr_unwrap(rb_str_new_cstr(to_concat)));
888-
return string;
897+
return rb_str_cat(string, to_concat, strlen(to_concat));
889898
}
890899

891900
VALUE rb_str_to_str(VALUE string) {

0 commit comments

Comments
 (0)