Watson1978
4/14/2011 - 1:13 AM

benchmark_result.txt

* before
$ macruby bm_string_match.rb
Rehearsal ------------------------------------
   0.670000   0.000000   0.670000 (  0.484087)
--------------------------- total: 0.670000sec

       user     system      total        real
   0.670000   0.010000   0.680000 (  0.487022)

* after
$ DYLD_LIBRARY_PATH=. ./macruby ~/tmp/bm_string_match.rb 
Rehearsal ------------------------------------
   0.510000   0.000000   0.510000 (  0.331892)
--------------------------- total: 0.510000sec

       user     system      total        real
   0.490000   0.010000   0.500000 (  0.328414)
require 'benchmark'

Benchmark.bmbm do |x|
  reg = /foo/
  def reg.match(x, y); "hello " + y; end

  x.report {
    100000.times do
      "foo".match(reg, "bar")
    end
  }
end
diff --git a/string.c b/string.c
index 31793bc..662d58c 100644
--- a/string.c
+++ b/string.c
@@ -32,6 +32,8 @@ VALUE rb_cRubyString;
 
 VALUE rb_fs;
 
+SEL selMATCH;
+
 // rb_str_t primitives.
 
 static void
@@ -3084,7 +3086,7 @@ rstr_match2(VALUE self, SEL sel, int argc, VALUE *argv)
     }
     VALUE re = get_pat(argv[0], false);
     argv[0] = self;
-    VALUE result = rb_funcall2(re, rb_intern("match"), argc, argv);
+    VALUE result = rb_vm_call(re, selMATCH, argc, argv);
     if (!NIL_P(result) && rb_block_given_p()) {
 	return rb_yield(result);
     }
@@ -6085,6 +6087,8 @@ Init_String(void)
     VALUE NSData = (VALUE)objc_getClass("NSData");
     assert(NSData != 0);
     rb_objc_define_method(NSData, "to_str", nsdata_to_str, 0);
+
+    selMATCH = sel_registerName("match:");
 }
 
 bool