String search performance improvements:
authorvitalyr@chromium.org <vitalyr@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Thu, 18 Mar 2010 09:01:08 +0000 (09:01 +0000)
committervitalyr@chromium.org <vitalyr@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Thu, 18 Mar 2010 09:01:08 +0000 (09:01 +0000)
 * Using memchr for first/only char lookup is faster than hand-coded
   loop. It processes one machine word per iteration so it helps even
   more on x64.

 * Tweaked badness computation in simple search. We pay only for the
   first char memchr scans, not for all of them.

Review URL: http://codereview.chromium.org/1100002

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@4173 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/runtime.cc

index 16164badd84de9ec3ed08b3beffebad9ebfc8c85..559bcd385765e2c5e37a1db8a82e11e7d7be03d5 100644 (file)
@@ -2353,6 +2353,14 @@ template <typename schar>
 static inline int SingleCharIndexOf(Vector<const schar> string,
                                     schar pattern_char,
                                     int start_index) {
+  if (sizeof(schar) == 1) {
+    schar* pos = reinterpret_cast<schar*>(
+        memchr(string.start() + start_index,
+               pattern_char,
+               string.length() - start_index));
+    if (pos == NULL) return -1;
+    return pos - string.start();
+  }
   for (int i = start_index, n = string.length(); i < n; i++) {
     if (pattern_char == string[i]) {
       return i;
@@ -2400,7 +2408,18 @@ static int SimpleIndexOf(Vector<const schar> subject,
       *complete = false;
       return i;
     }
-    if (subject[i] != pattern_first_char) continue;
+    if (sizeof(schar) == 1 && sizeof(pchar) == 1) {
+      schar* pos = reinterpret_cast<schar*>(memchr(subject.start() + i,
+                                                   pattern_first_char,
+                                                   n - i + 1));
+      if (pos == NULL) {
+        *complete = true;
+        return -1;
+      }
+      i = pos - subject.start();
+    } else {
+      if (subject[i] != pattern_first_char) continue;
+    }
     int j = 1;
     do {
       if (pattern[j] != subject[i+j]) {
@@ -2425,7 +2444,15 @@ static int SimpleIndexOf(Vector<const schar> subject,
                          int idx) {
   pchar pattern_first_char = pattern[0];
   for (int i = idx, n = subject.length() - pattern.length(); i <= n; i++) {
-    if (subject[i] != pattern_first_char) continue;
+    if (sizeof(schar) == 1 && sizeof(pchar) == 1) {
+      schar* pos = reinterpret_cast<schar*>(memchr(subject.start() + i,
+                                                   pattern_first_char,
+                                                   n - i + 1));
+      if (pos == NULL) return -1;
+      i = pos - subject.start();
+    } else {
+      if (subject[i] != pattern_first_char) continue;
+    }
     int j = 1;
     do {
       if (pattern[j] != subject[i+j]) {