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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « src/regexp-delay.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. 1 // Copyright 2006-2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 // do anything locale specific. 155 // do anything locale specific.
156 function StringLocaleCompare(other) { 156 function StringLocaleCompare(other) {
157 if (%_ArgumentsLength() === 0) return 0; 157 if (%_ArgumentsLength() === 0) return 0;
158 158
159 var this_str = TO_STRING_INLINE(this); 159 var this_str = TO_STRING_INLINE(this);
160 var other_str = TO_STRING_INLINE(other); 160 var other_str = TO_STRING_INLINE(other);
161 return %StringLocaleCompare(this_str, other_str); 161 return %StringLocaleCompare(this_str, other_str);
162 } 162 }
163 163
164 164
165 var stringMatchCacheInput;
166 var stringMatchCacheId;
167 var stringMatchCacheResult;
168
165 // ECMA-262 section 15.5.4.10 169 // ECMA-262 section 15.5.4.10
166 function StringMatch(regexp) { 170 function StringMatch(regexp) {
167 if (!IS_REGEXP(regexp)) regexp = new ORIGINAL_REGEXP(regexp); 171 if (!IS_REGEXP(regexp)) regexp = new ORIGINAL_REGEXP(regexp);
168 var subject = TO_STRING_INLINE(this); 172 var subject = TO_STRING_INLINE(this);
169 173
170 if (!regexp.global) return regexp.exec(subject); 174 if (!regexp.global) return RegExpExec.call(regexp, subject);
171 %_Log('regexp', 'regexp-match,%0S,%1r', [subject, regexp]); 175 %_Log('regexp', 'regexp-match,%0S,%1r', [subject, regexp]);
176
177 var reid = %_RegExpId(regexp);
178 if (%_IsIdentical(reid, stringMatchCacheId) &&
179 %_IsIdentical(this, stringMatchCacheInput)) {
180 %_Log("regexp", "regexp-match-cache-hit", []);
181 var cachedResult = stringMatchCacheResult;
182 if (cachedResult) {
183 cachedResult = ArraySlice.call(cachedResult, 0, cachedResult.length);
184 }
185 return cachedResult;
186 }
187 %_Log("regexp", "regexp-match-cache-miss", []);
188
172 // lastMatchInfo is defined in regexp-delay.js. 189 // lastMatchInfo is defined in regexp-delay.js.
173 return %StringMatch(subject, regexp, lastMatchInfo); 190 var result = %StringMatch(subject, regexp, lastMatchInfo);
191 stringMatchCacheInput = this;
192 stringMatchCacheId = reid;
193 stringMatchCacheResult = result;
194 return result;
174 } 195 }
175 196
176 197
177 // SubString is an internal function that returns the sub string of 'string'. 198 // SubString is an internal function that returns the sub string of 'string'.
178 // If resulting string is of length 1, we use the one character cache 199 // If resulting string is of length 1, we use the one character cache
179 // otherwise we call the runtime system. 200 // otherwise we call the runtime system.
180 function SubString(string, start, end) { 201 function SubString(string, start, end) {
181 // Use the one character string cache. 202 // Use the one character string cache.
182 if (start + 1 == end) { 203 if (start + 1 == end) {
183 var char_code = %_FastCharCodeAt(string, start); 204 var char_code = %_FastCharCodeAt(string, start);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 ExpandReplacement(replace, subject, reusableMatchInfo, builder); 253 ExpandReplacement(replace, subject, reusableMatchInfo, builder);
233 } 254 }
234 255
235 // suffix 256 // suffix
236 builder.addSpecialSlice(end, subject.length); 257 builder.addSpecialSlice(end, subject.length);
237 258
238 return builder.generate(); 259 return builder.generate();
239 } 260 }
240 261
241 262
263 var stringReplaceRegExpCacheInput;
264 var stringReplaceRegExpCacheId;
265 var stringReplaceRegExpCacheIndex;
266 var stringReplaceRegExpCacheReplace;
267 var stringReplaceRegExpCacheResult;
242 // Helper function for regular expressions in String.prototype.replace. 268 // Helper function for regular expressions in String.prototype.replace.
243 function StringReplaceRegExp(subject, regexp, replace) { 269 function StringReplaceRegExp(subject, regexp, replace) {
270 if (%_IsIdentical(%_RegExpId(regexp), stringReplaceRegExpCacheId) &&
271 %_IsIdentical(subject, stringReplaceRegExpCacheInput) &&
272 %_IsIdentical(replace, stringReplaceRegExpCacheReplace) &&
273 (!regexp.global ||
274 %_IsIdentical(regexp.lastIndex, stringReplaceRegExpCacheIndex))) {
275 %_Log("regexp", "regexp-replace-cache-hit", []);
276 return stringReplaceRegExpCacheResult;
277 }
278 %_Log("regexp", "regexp-replace-cache-miss", []);
279 stringReplaceRegExpCacheId = %_RegExpId(regexp);
280 stringReplaceRegExpCacheInput = subject;
281 stringReplaceRegExpCacheIndex = regexp.lastIndex;
282 stringReplaceRegExpCacheReplace = replace;
244 replace = TO_STRING_INLINE(replace); 283 replace = TO_STRING_INLINE(replace);
245 return %StringReplaceRegExpWithString(subject, 284 var result = %StringReplaceRegExpWithString(subject,
246 regexp, 285 regexp,
247 replace, 286 replace,
248 lastMatchInfo); 287 lastMatchInfo);
288 stringReplaceRegExpCacheResult = result;
289 return result;
249 }; 290 };
250 291
251 292
252 // Expand the $-expressions in the string and return a new string with 293 // Expand the $-expressions in the string and return a new string with
253 // the result. 294 // the result.
254 function ExpandReplacement(string, subject, matchInfo, builder) { 295 function ExpandReplacement(string, subject, matchInfo, builder) {
255 var next = %StringIndexOf(string, '$', 0); 296 var next = %StringIndexOf(string, '$', 0);
256 if (next < 0) { 297 if (next < 0) {
257 builder.add(string); 298 builder.add(string);
258 return; 299 return;
(...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after
904 "small", StringSmall, 945 "small", StringSmall,
905 "strike", StringStrike, 946 "strike", StringStrike,
906 "sub", StringSub, 947 "sub", StringSub,
907 "sup", StringSup, 948 "sup", StringSup,
908 "toJSON", StringToJSON 949 "toJSON", StringToJSON
909 )); 950 ));
910 } 951 }
911 952
912 953
913 SetupString(); 954 SetupString();
OLDNEW
« 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