summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwatson1978 <watson1978@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-10-27 14:55:03 +0000
committerwatson1978 <watson1978@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-10-27 14:55:03 +0000
commitb03a44c4ac506f02a164fb5d5e9ce1fdacd546de (patch)
treedb85f2a234d93cec954776cee6414d9e33ecdd45
parentfd144546885390d549414e5bc103111c7563be71 (diff)
downloadruby-b03a44c4ac506f02a164fb5d5e9ce1fdacd546de.tar.gz
string.c: Improve String#prepend performance if only one argument is given
* string.c (rb_str_prepend_multi): Prepend the string without generating temporary String object if only one argument is given. This is very similar with https://github.com/ruby/ruby/pull/1634 String#prepend -> 47.5 % up [Fix GH-1670] [ruby-core:82195] [Bug #13773] * Before String#prepend 1.517M (± 1.8%) i/s - 7.614M in 5.019819s * After String#prepend 2.236M (± 3.4%) i/s - 11.234M in 5.029716s * Test code require 'benchmark/ips' Benchmark.ips do |x| x.report "String#prepend" do |loop| loop.times { "!".prepend("hello") } end end git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60480 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--string.c5
1 files changed, 4 insertions, 1 deletions
diff --git a/string.c b/string.c
index 050ef27d9b..e9204fc233 100644
--- a/string.c
+++ b/string.c
@@ -3077,7 +3077,10 @@ rb_str_prepend_multi(int argc, VALUE *argv, VALUE str)
{
str_modifiable(str);
- if (argc > 0) {
+ if (argc == 1) {
+ rb_str_update(str, 0L, 0L, argv[0]);
+ }
+ else if (argc > 1) {
int i;
VALUE arg_str = rb_str_tmp_new(0);
rb_enc_copy(arg_str, str);