Revision: 4364
Author: [email protected]
Date: Thu Apr  8 07:42:27 2010
Log: Adding boolean saveAnswer property of RegExpCache to avoid unnecessary cloning of the regexp answer object/array.
Review URL: http://codereview.chromium.org/1556019
http://code.google.com/p/v8/source/detail?r=4364

Modified:
 /branches/bleeding_edge/src/regexp.js
 /branches/bleeding_edge/src/string.js

=======================================
--- /branches/bleeding_edge/src/regexp.js       Tue Mar 30 00:15:23 2010
+++ /branches/bleeding_edge/src/regexp.js       Thu Apr  8 07:42:27 2010
@@ -126,6 +126,9 @@
   this.replaceString = 0;
   this.lastIndex = 0;
   this.answer = 0;
+  // answerSaved marks whether the contents of answer is valid for a cache
+  // hit in RegExpExec, StringMatch and StringSplit.
+  this.answerSaved = false;
 }


@@ -133,6 +136,7 @@


 function CloneRegexpAnswer(array) {
+  if (array == null) return null;
   var len = array.length;
   var answer = new $Array(len);
   for (var i = 0; i < len; i++) {
@@ -151,17 +155,19 @@
   }

   var cache = regExpCache;
+  var saveAnswer = false;

   if (%_ObjectEquals(cache.type, 'exec') &&
       %_ObjectEquals(cache.lastIndex, this.lastIndex) &&
       %_ObjectEquals(cache.regExp, this) &&
       %_ObjectEquals(cache.subject, string)) {
-    var last = cache.answer;
-    if (last == null) {
-      return last;
+    if (cache.answerSaved) {
+      return CloneRegexpAnswer(cache.answer);
     } else {
-      return CloneRegexpAnswer(last);
-    }
+      saveAnswer = true;
+    }
+  } else {
+    cache.answerSaved = false;
   }

   if (%_ArgumentsLength() == 0) {
@@ -196,6 +202,7 @@
     cache.regExp = this;
     cache.subject = s;
     cache.answer = matchIndices;  // Null.
+    cache.answerSaved = true;     // Safe since no cloning is needed.
     cache.type = 'exec';
     return matchIndices;        // No match.
   }
@@ -225,15 +232,18 @@
   result.input = s;
   if (this.global) {
     this.lastIndex = lastMatchInfo[CAPTURE1];
-    return result;
   } else {
     cache.regExp = this;
     cache.subject = s;
     cache.lastIndex = lastIndex;
-    cache.answer = result;
+    if (saveAnswer) {
+      cache.answer = CloneRegexpAnswer(result);
+      cache.answerSaved = true;
+    }
     cache.type = 'exec';
-    return CloneRegexpAnswer(result);
-  }
+  }
+  return result;
+
 }


=======================================
--- /branches/bleeding_edge/src/string.js       Tue Mar 30 00:15:23 2010
+++ /branches/bleeding_edge/src/string.js       Thu Apr  8 07:42:27 2010
@@ -170,15 +170,15 @@
   if (!regexp.global) return regexp.exec(subject);

   var cache = regExpCache;
+  var saveAnswer = false;

   if (%_ObjectEquals(cache.type, 'match') &&
       %_ObjectEquals(cache.regExp, regexp) &&
       %_ObjectEquals(cache.subject, subject)) {
-    var last = cache.answer;
-    if (last == null) {
-      return last;
+    if (cache.answerSaved) {
+      return CloneRegexpAnswer(cache.answer);
     } else {
-      return CloneRegexpAnswer(last);
+      saveAnswer = true;
     }
   }

@@ -188,12 +188,9 @@
   cache.type = 'match';
   cache.regExp = regexp;
   cache.subject = subject;
-  cache.answer = result;
-  if (result == null) {
-    return result;
-  } else {
-    return CloneRegexpAnswer(result);
-  }
+  if (saveAnswer) cache.answer = CloneRegexpAnswer(result);
+  cache.answerSaved = saveAnswer;
+  return result;
 }


@@ -596,11 +593,16 @@
   }

   var cache = regExpCache;
+  var saveAnswer = false;

   if (%_ObjectEquals(cache.type, 'split') &&
       %_ObjectEquals(cache.regExp, separator) &&
       %_ObjectEquals(cache.subject, subject)) {
-    return CloneRegexpAnswer(cache.answer);
+    if (cache.answerSaved) {
+      return CloneRegexpAnswer(cache.answer);
+    } else {
+      saveAnswer = true;
+    }
   }

   cache.type = 'split';
@@ -610,6 +612,7 @@
   %_Log('regexp', 'regexp-split,%0S,%1r', [subject, separator]);

   if (length === 0) {
+    cache.answerSaved = true;
     if (splitMatch(separator, subject, 0, 0) != null) {
       cache.answer = [];
       return [];
@@ -622,20 +625,19 @@
   var startIndex = 0;
   var result = [];

+  outer_loop:
   while (true) {

     if (startIndex === length) {
       result[result.length] = subject.slice(currentIndex, length);
-      cache.answer = result;
-      return CloneRegexpAnswer(result);
+      break;
     }

var matchInfo = splitMatch(separator, subject, currentIndex, startIndex);

     if (IS_NULL(matchInfo)) {
       result[result.length] = subject.slice(currentIndex, length);
-      cache.answer = result;
-      return CloneRegexpAnswer(result);
+      break;
     }

     var endIndex = matchInfo[CAPTURE1];
@@ -647,10 +649,7 @@
     }

result[result.length] = SubString(subject, currentIndex, matchInfo[CAPTURE0]);
-    if (result.length === limit) {
-      cache.answer = result;
-      return CloneRegexpAnswer(result);
-    }
+    if (result.length === limit) break;

     var num_captures = NUMBER_OF_CAPTURES(matchInfo);
     for (var i = 2; i < num_captures; i += 2) {
@@ -661,14 +660,15 @@
       } else {
         result[result.length] = void 0;
       }
-      if (result.length === limit) {
-        cache.answer = result;
-        return CloneRegexpAnswer(result);
-      }
+      if (result.length === limit) break outer_loop;
     }

     startIndex = currentIndex = endIndex;
   }
+  if (saveAnswer) cache.answer = CloneRegexpAnswer(result);
+  cache.answerSaved = saveAnswer;
+  return result;
+
 }


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

To unsubscribe, reply using "remove me" as the subject.

Reply via email to