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

Unified Diff: src/runtime.cc

Issue 596122: Some string optimizations: (Closed)
Patch Set: Undo unnecessary optimizations. Created 10 years, 10 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
« src/macros.py ('K') | « src/mips/codegen-mips.cc ('k') | src/runtime.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/runtime.cc
diff --git a/src/runtime.cc b/src/runtime.cc
index b1d6024e0ae7eb9544ddcf1dec1f2f11c32e51b4..9e4ebe6ea4226087bf19518f1213605dc77991c4 100644
--- a/src/runtime.cc
+++ b/src/runtime.cc
@@ -2276,6 +2276,20 @@ static int SingleCharIndexOf(Vector<const schar> string,
return -1;
}
+
+template <typename schar>
+static int SingleCharLastIndexOf(Vector<const schar> string,
+ schar pattern_char,
+ int start_index) {
+ for (int i = start_index; i >= 0; i--) {
+ if (pattern_char == string[i]) {
+ return i;
+ }
+ }
+ return -1;
+}
+
+
// Trivial string search for shorter strings.
// On return, if "complete" is set to true, the return value is the
// final result of searching for the patter in the subject.
@@ -2459,16 +2473,17 @@ static Object* Runtime_StringLastIndexOf(Arguments args) {
NoHandleAllocation ha;
ASSERT(args.length() == 3);
- CONVERT_CHECKED(String, sub, args[0]);
- CONVERT_CHECKED(String, pat, args[1]);
+ CONVERT_ARG_CHECKED(String, sub, 0);
+ CONVERT_ARG_CHECKED(String, pat, 1);
Object* index = args[2];
- sub->TryFlattenIfNotFlat();
- pat->TryFlattenIfNotFlat();
-
uint32_t start_index;
if (!Array::IndexFromObject(index, &start_index)) return Smi::FromInt(-1);
+ if (!sub->IsFlat()) {
+ FlattenString(sub);
+ }
+
uint32_t pattern_length = pat->length();
uint32_t sub_length = sub->length();
@@ -2476,6 +2491,25 @@ static Object* Runtime_StringLastIndexOf(Arguments args) {
start_index = sub_length - pattern_length;
}
+ if (pattern_length == 1) {
+ AssertNoAllocation no_heap_allocation; // ensure vectors stay valid
+ if (sub->IsAsciiRepresentation()) {
+ uc16 pchar = pat->Get(0);
+ if (pchar > String::kMaxAsciiCharCode) {
+ return Smi::FromInt(-1);
+ }
+ return Smi::FromInt(SingleCharLastIndexOf(sub->ToAsciiVector(),
+ static_cast<char>(pat->Get(0)),
+ start_index));
+ } else {
+ return Smi::FromInt(SingleCharLastIndexOf(sub->ToUC16Vector(),
+ pat->Get(0),
+ start_index));
+ }
+ }
+
+ pat->TryFlattenIfNotFlat();
+
for (int i = start_index; i >= 0; i--) {
bool found = true;
for (uint32_t j = 0; j < pattern_length; j++) {
« src/macros.py ('K') | « src/mips/codegen-mips.cc ('k') | src/runtime.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698