Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(941)

Unified Diff: src/regexp-delay.js

Issue 817001: Add 1-element caches to RegExp.exec and String.replace. We... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/string.js » ('j') | src/string.js » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/regexp-delay.js
===================================================================
--- src/regexp-delay.js (revision 4060)
+++ src/regexp-delay.js (working copy)
@@ -140,7 +140,36 @@
}
+var cachedRegexp;
+var cachedSubject;
+var cachedLastIndex;
+var cachedAnswer;
+
+
+function CloneRegexpAnswer(array) {
+ var len = array.length;
+ var answer = new $Array(len);
+ for (var i = 0; i < len; i++) {
+ answer[i] = array[i];
+ }
+ answer.index = array.index;
+ answer.input = array.input;
+ return answer;
+}
+
+
function RegExpExec(string) {
+ if (cachedLastIndex === this.lastIndex &&
Lasse Reichstein 2010/03/10 08:53:24 lastIndex is only relevant if the regexp is global
+ cachedRegexp === this &&
Lasse Reichstein 2010/03/10 08:53:24 Checking the regexp itself is unsafe. Its behavior
+ cachedSubject === string) {
Mads Ager (chromium) 2010/03/10 08:12:22 I would use %_ObjectEquals for the string and rege
+ var last = cachedAnswer;
+ if (last == null) {
Lasse Reichstein 2010/03/10 08:53:24 Use ===?
+ return last;
+ } else {
+ return CloneRegexpAnswer(last);
+ }
+ }
+
if (!IS_REGEXP(this)) {
throw MakeTypeError('incompatible_method_receiver',
['RegExp.prototype.exec', this]);
@@ -159,6 +188,7 @@
s = ToString(string);
}
var lastIndex = this.lastIndex;
+
var i = this.global ? TO_INTEGER(lastIndex) : 0;
if (i < 0 || i > s.length) {
@@ -172,7 +202,11 @@
if (matchIndices == null) {
if (this.global) this.lastIndex = 0;
- return matchIndices; // no match
+ cachedLastIndex = lastIndex;
+ cachedRegexp = this;
+ cachedSubject = s;
+ cachedAnswer = matchIndices; // Null.
+ return matchIndices; // No match.
}
var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1;
@@ -196,11 +230,18 @@
}
}
- if (this.global)
- this.lastIndex = lastMatchInfo[CAPTURE1];
result.index = lastMatchInfo[CAPTURE0];
result.input = s;
- return result;
+ if (this.global) {
+ this.lastIndex = lastMatchInfo[CAPTURE1];
+ return result;
+ } else {
+ cachedRegexp = this;
+ cachedSubject = s;
+ cachedLastIndex = lastIndex;
+ cachedAnswer = result;
+ return CloneRegexpAnswer(result);
+ }
}
« no previous file with comments | « no previous file | src/string.js » ('j') | src/string.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698