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

Unified Diff: src/string.js

Issue 652224: Proof-of-concept RegExp-cache for exec, test, replace. (Closed)
Patch Set: Cache regexp operations. 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 | « src/regexp-delay.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/string.js
diff --git a/src/string.js b/src/string.js
index 49f403de7da8140bb8aeebd9d87c00c329850a0d..4319560324d8cb249444d76d83f1d66c6c13f574 100644
--- a/src/string.js
+++ b/src/string.js
@@ -162,15 +162,36 @@ function StringLocaleCompare(other) {
}
+var stringMatchCacheInput;
+var stringMatchCacheId;
+var stringMatchCacheResult;
+
// ECMA-262 section 15.5.4.10
function StringMatch(regexp) {
if (!IS_REGEXP(regexp)) regexp = new ORIGINAL_REGEXP(regexp);
var subject = TO_STRING_INLINE(this);
- if (!regexp.global) return regexp.exec(subject);
+ if (!regexp.global) return RegExpExec.call(regexp, subject);
%_Log('regexp', 'regexp-match,%0S,%1r', [subject, regexp]);
+
+ var reid = %_RegExpId(regexp);
+ if (%_IsIdentical(reid, stringMatchCacheId) &&
+ %_IsIdentical(this, stringMatchCacheInput)) {
+ %_Log("regexp", "regexp-match-cache-hit", []);
+ var cachedResult = stringMatchCacheResult;
+ if (cachedResult) {
+ cachedResult = ArraySlice.call(cachedResult, 0, cachedResult.length);
+ }
+ return cachedResult;
+ }
+ %_Log("regexp", "regexp-match-cache-miss", []);
+
// lastMatchInfo is defined in regexp-delay.js.
- return %StringMatch(subject, regexp, lastMatchInfo);
+ var result = %StringMatch(subject, regexp, lastMatchInfo);
+ stringMatchCacheInput = this;
+ stringMatchCacheId = reid;
+ stringMatchCacheResult = result;
+ return result;
}
@@ -239,13 +260,33 @@ function StringReplace(search, replace) {
}
+var stringReplaceRegExpCacheInput;
+var stringReplaceRegExpCacheId;
+var stringReplaceRegExpCacheIndex;
+var stringReplaceRegExpCacheReplace;
+var stringReplaceRegExpCacheResult;
// Helper function for regular expressions in String.prototype.replace.
function StringReplaceRegExp(subject, regexp, replace) {
+ if (%_IsIdentical(%_RegExpId(regexp), stringReplaceRegExpCacheId) &&
+ %_IsIdentical(subject, stringReplaceRegExpCacheInput) &&
+ %_IsIdentical(replace, stringReplaceRegExpCacheReplace) &&
+ (!regexp.global ||
+ %_IsIdentical(regexp.lastIndex, stringReplaceRegExpCacheIndex))) {
+ %_Log("regexp", "regexp-replace-cache-hit", []);
+ return stringReplaceRegExpCacheResult;
+ }
+ %_Log("regexp", "regexp-replace-cache-miss", []);
+ stringReplaceRegExpCacheId = %_RegExpId(regexp);
+ stringReplaceRegExpCacheInput = subject;
+ stringReplaceRegExpCacheIndex = regexp.lastIndex;
+ stringReplaceRegExpCacheReplace = replace;
replace = TO_STRING_INLINE(replace);
- return %StringReplaceRegExpWithString(subject,
- regexp,
- replace,
- lastMatchInfo);
+ var result = %StringReplaceRegExpWithString(subject,
+ regexp,
+ replace,
+ lastMatchInfo);
+ stringReplaceRegExpCacheResult = result;
+ return result;
};
« no previous file with comments | « src/regexp-delay.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698