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

ruby-changes:11866

From: matz <ko1@a...>
Date: Fri, 22 May 2009 05:08:25 +0900 (JST)
Subject: [ruby-changes:11866] Ruby:r23523 (trunk): * string.c (rb_str_count): optimized for 1byte string count by

matz	2009-05-22 05:08:03 +0900 (Fri, 22 May 2009)

  New Revision: 23523

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=23523

  Log:
    * string.c (rb_str_count): optimized for 1byte string count by
      avoiding tr_setup_table().

  Modified files:
    trunk/ChangeLog
    trunk/string.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 23522)
+++ ChangeLog	(revision 23523)
@@ -53,6 +53,11 @@
 
 	* enumerator.c (inspect_enumerator): should use long.
 
+Wed May 20 09:18:44 2009  Yukihiro Matsumoto  <matz@r...>
+
+	* string.c (rb_str_count): optimized for 1byte string count by
+	  avoiding tr_setup_table().
+
 Wed May 20 06:25:29 2009  Yukihiro Matsumoto  <matz@r...>
 
 	* encoding.c (rb_enc_fast_mbclen): faster mbclen for strings known
Index: string.c
===================================================================
--- string.c	(revision 23522)
+++ string.c	(revision 23523)
@@ -5380,19 +5380,31 @@
 	rb_raise(rb_eArgError, "wrong number of arguments");
     }
     for (i=0; i<argc; i++) {
-	VALUE s = argv[i];
+	VALUE tstr = argv[i];
+	unsigned char c;
 
-	StringValue(s);
-	enc = rb_enc_check(str, s);
-	tr_setup_table(s, table, i==0, &del, &nodel, enc);
+	StringValue(tstr);
+	enc = rb_enc_check(str, tstr);
+	if (argc == 1 && RSTRING_LEN(tstr) == 1 && rb_enc_asciicompat(enc) &&
+	    (c = RSTRING_PTR(tstr)[0]) < 0x80 && !is_broken_string(str)) {
+	    int n = 0;
+
+	    s = RSTRING_PTR(str);
+	    if (!s || RSTRING_LEN(str) == 0) return INT2FIX(0);
+	    send = RSTRING_END(str);
+	    while (s < send) {
+		if (*(unsigned char*)s++ == c) n++;
+	    }
+	    return INT2NUM(n);
+	}
+	tr_setup_table(tstr, table, i==0, &del, &nodel, enc);
     }
 
     s = RSTRING_PTR(str);
     if (!s || RSTRING_LEN(str) == 0) return INT2FIX(0);
     send = RSTRING_END(str);
+    ascompat = rb_enc_asciicompat(enc);
     i = 0;
-    ascompat = rb_enc_asciicompat(enc);
-
     while (s < send) {
 	unsigned int c;
 	int clen;

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

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