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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | src/string.js » ('j') | src/string.js » ('J')
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 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 DoConstructRegExp(this, pattern, flags, false); 133 DoConstructRegExp(this, pattern, flags, false);
134 } 134 }
135 } 135 }
136 136
137 137
138 function DoRegExpExec(regexp, string, index) { 138 function DoRegExpExec(regexp, string, index) {
139 return %_RegExpExec(regexp, string, index, lastMatchInfo); 139 return %_RegExpExec(regexp, string, index, lastMatchInfo);
140 } 140 }
141 141
142 142
143 var cachedRegexp;
144 var cachedSubject;
145 var cachedLastIndex;
146 var cachedAnswer;
147
148
149 function CloneRegexpAnswer(array) {
150 var len = array.length;
151 var answer = new $Array(len);
152 for (var i = 0; i < len; i++) {
153 answer[i] = array[i];
154 }
155 answer.index = array.index;
156 answer.input = array.input;
157 return answer;
158 }
159
160
143 function RegExpExec(string) { 161 function RegExpExec(string) {
162 if (cachedLastIndex === this.lastIndex &&
Lasse Reichstein 2010/03/10 08:53:24 lastIndex is only relevant if the regexp is global
163 cachedRegexp === this &&
Lasse Reichstein 2010/03/10 08:53:24 Checking the regexp itself is unsafe. Its behavior
164 cachedSubject === string) {
Mads Ager (chromium) 2010/03/10 08:12:22 I would use %_ObjectEquals for the string and rege
165 var last = cachedAnswer;
166 if (last == null) {
Lasse Reichstein 2010/03/10 08:53:24 Use ===?
167 return last;
168 } else {
169 return CloneRegexpAnswer(last);
170 }
171 }
172
144 if (!IS_REGEXP(this)) { 173 if (!IS_REGEXP(this)) {
145 throw MakeTypeError('incompatible_method_receiver', 174 throw MakeTypeError('incompatible_method_receiver',
146 ['RegExp.prototype.exec', this]); 175 ['RegExp.prototype.exec', this]);
147 } 176 }
148 if (%_ArgumentsLength() == 0) { 177 if (%_ArgumentsLength() == 0) {
149 var regExpInput = LAST_INPUT(lastMatchInfo); 178 var regExpInput = LAST_INPUT(lastMatchInfo);
150 if (IS_UNDEFINED(regExpInput)) { 179 if (IS_UNDEFINED(regExpInput)) {
151 throw MakeError('no_input_to_regexp', [this]); 180 throw MakeError('no_input_to_regexp', [this]);
152 } 181 }
153 string = regExpInput; 182 string = regExpInput;
154 } 183 }
155 var s; 184 var s;
156 if (IS_STRING(string)) { 185 if (IS_STRING(string)) {
157 s = string; 186 s = string;
158 } else { 187 } else {
159 s = ToString(string); 188 s = ToString(string);
160 } 189 }
161 var lastIndex = this.lastIndex; 190 var lastIndex = this.lastIndex;
191
162 var i = this.global ? TO_INTEGER(lastIndex) : 0; 192 var i = this.global ? TO_INTEGER(lastIndex) : 0;
163 193
164 if (i < 0 || i > s.length) { 194 if (i < 0 || i > s.length) {
165 this.lastIndex = 0; 195 this.lastIndex = 0;
166 return null; 196 return null;
167 } 197 }
168 198
169 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]); 199 %_Log('regexp', 'regexp-exec,%0r,%1S,%2i', [this, s, lastIndex]);
170 // matchIndices is either null or the lastMatchInfo array. 200 // matchIndices is either null or the lastMatchInfo array.
171 var matchIndices = %_RegExpExec(this, s, i, lastMatchInfo); 201 var matchIndices = %_RegExpExec(this, s, i, lastMatchInfo);
172 202
173 if (matchIndices == null) { 203 if (matchIndices == null) {
174 if (this.global) this.lastIndex = 0; 204 if (this.global) this.lastIndex = 0;
175 return matchIndices; // no match 205 cachedLastIndex = lastIndex;
206 cachedRegexp = this;
207 cachedSubject = s;
208 cachedAnswer = matchIndices; // Null.
209 return matchIndices; // No match.
176 } 210 }
177 211
178 var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1; 212 var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1;
179 var result; 213 var result;
180 if (numResults === 1) { 214 if (numResults === 1) {
181 var matchStart = lastMatchInfo[CAPTURE(0)]; 215 var matchStart = lastMatchInfo[CAPTURE(0)];
182 var matchEnd = lastMatchInfo[CAPTURE(1)]; 216 var matchEnd = lastMatchInfo[CAPTURE(1)];
183 result = [SubString(s, matchStart, matchEnd)]; 217 result = [SubString(s, matchStart, matchEnd)];
184 } else { 218 } else {
185 result = new $Array(numResults); 219 result = new $Array(numResults);
186 for (var i = 0; i < numResults; i++) { 220 for (var i = 0; i < numResults; i++) {
187 var matchStart = lastMatchInfo[CAPTURE(i << 1)]; 221 var matchStart = lastMatchInfo[CAPTURE(i << 1)];
188 var matchEnd = lastMatchInfo[CAPTURE((i << 1) + 1)]; 222 var matchEnd = lastMatchInfo[CAPTURE((i << 1) + 1)];
189 if (matchStart != -1 && matchEnd != -1) { 223 if (matchStart != -1 && matchEnd != -1) {
190 result[i] = SubString(s, matchStart, matchEnd); 224 result[i] = SubString(s, matchStart, matchEnd);
191 } else { 225 } else {
192 // Make sure the element is present. Avoid reading the undefined 226 // Make sure the element is present. Avoid reading the undefined
193 // property from the global object since this may change. 227 // property from the global object since this may change.
194 result[i] = void 0; 228 result[i] = void 0;
195 } 229 }
196 } 230 }
197 } 231 }
198 232
199 if (this.global)
200 this.lastIndex = lastMatchInfo[CAPTURE1];
201 result.index = lastMatchInfo[CAPTURE0]; 233 result.index = lastMatchInfo[CAPTURE0];
202 result.input = s; 234 result.input = s;
203 return result; 235 if (this.global) {
236 this.lastIndex = lastMatchInfo[CAPTURE1];
237 return result;
238 } else {
239 cachedRegexp = this;
240 cachedSubject = s;
241 cachedLastIndex = lastIndex;
242 cachedAnswer = result;
243 return CloneRegexpAnswer(result);
244 }
204 } 245 }
205 246
206 247
207 // Section 15.10.6.3 doesn't actually make sense, but the intention seems to be 248 // Section 15.10.6.3 doesn't actually make sense, but the intention seems to be
208 // that test is defined in terms of String.prototype.exec. However, it probably 249 // that test is defined in terms of String.prototype.exec. However, it probably
209 // means the original value of String.prototype.exec, which is what everybody 250 // means the original value of String.prototype.exec, which is what everybody
210 // else implements. 251 // else implements.
211 function RegExpTest(string) { 252 function RegExpTest(string) {
212 if (!IS_REGEXP(this)) { 253 if (!IS_REGEXP(this)) {
213 throw MakeTypeError('incompatible_method_receiver', 254 throw MakeTypeError('incompatible_method_receiver',
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE); 449 %DefineAccessor($RegExp, "$'", SETTER, NoOpSetter, DONT_ENUM | DONT_DELETE);
409 450
410 for (var i = 1; i < 10; ++i) { 451 for (var i = 1; i < 10; ++i) {
411 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE); 452 %DefineAccessor($RegExp, '$' + i, GETTER, RegExpMakeCaptureGetter(i), DONT_D ELETE);
412 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE); 453 %DefineAccessor($RegExp, '$' + i, SETTER, NoOpSetter, DONT_DELETE);
413 } 454 }
414 } 455 }
415 456
416 457
417 SetupRegExp(); 458 SetupRegExp();
OLDNEW
« 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