[前][次][番号順一覧][スレッド一覧]

ruby-changes:48366

From: watson1978 <ko1@a...>
Date: Fri, 27 Oct 2017 23:55:09 +0900 (JST)
Subject: [ruby-changes:48366] watson1978:r60480 (trunk): string.c: Improve String#prepend performance if only one argument is given

watson1978	2017-10-27 23:55:03 +0900 (Fri, 27 Oct 2017)

  New Revision: 60480

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60480

  Log:
    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 (?\194?\177 1.8%) i/s -      7.614M in   5.019819s
    
    * After
          String#prepend      2.236M (?\194?\177 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

  Modified files:
    trunk/string.c
Index: string.c
===================================================================
--- string.c	(revision 60479)
+++ string.c	(revision 60480)
@@ -3077,7 +3077,10 @@ rb_str_prepend_multi(int argc, VALUE *ar https://github.com/ruby/ruby/blob/trunk/string.c#L3077
 {
     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);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]