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

Side by Side Diff: src/runtime.cc

Issue 14505: * Changed simplified index-of to use correct BMH algorithm, for a theoretical better performance. (Closed)
Patch Set: Created 12 years 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 | « no previous file | 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-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 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 1133 matching lines...) Expand 10 before | Expand all | Expand 10 after
1144 } 1144 }
1145 if (sizeof(pchar) == 1) { 1145 if (sizeof(pchar) == 1) {
1146 if (char_code > String::kMaxAsciiCharCode) { 1146 if (char_code > String::kMaxAsciiCharCode) {
1147 return -1; 1147 return -1;
1148 } 1148 }
1149 return bad_char_occurence[char_code]; 1149 return bad_char_occurence[char_code];
1150 } 1150 }
1151 return bad_char_occurence[char_code % kBMAlphabetSize]; 1151 return bad_char_occurence[char_code % kBMAlphabetSize];
1152 } 1152 }
1153 1153
1154 // Restricted simplified Boyer-Moore string matching. Restricts tables to a 1154 // Restricted simplified Boyer-Moore string matching.
1155 // suffix of long pattern strings and handles only equivalence classes 1155 // Uses only the bad-shift table of Boyer-Moore and only uses it
1156 // of the full alphabet. This allows us to ensure that tables take only 1156 // for the character compared to the last character of the needle.
1157 // a fixed amount of space.
1158 template <typename schar, typename pchar> 1157 template <typename schar, typename pchar>
1159 static int BoyerMooreSimplified(Vector<const schar> subject, 1158 static int BoyerMooreHorsepool(Vector<const schar> subject,
1160 Vector<const pchar> pattern, 1159 Vector<const pchar> pattern,
1161 int start_index, 1160 int start_index,
1162 bool* complete) { 1161 bool* complete) {
1163 int n = subject.length(); 1162 int n = subject.length();
1164 int m = pattern.length(); 1163 int m = pattern.length();
1165 // Only preprocess at most kBMMaxShift last characters of pattern. 1164 // Only preprocess at most kBMMaxShift last characters of pattern.
1166 int start = m < kBMMaxShift ? 0 : m - kBMMaxShift; 1165 int start = m < kBMMaxShift ? 0 : m - kBMMaxShift;
1167 1166
1168 BoyerMoorePopulateBadCharTable(pattern, start); 1167 BoyerMoorePopulateBadCharTable(pattern, start);
1169 1168
1170 int badness = -m; // How bad we are doing without a good-suffix table. 1169 int badness = -m; // How bad we are doing without a good-suffix table.
1171 int idx; // No matches found prior to this index. 1170 int idx; // No matches found prior to this index.
1172 pchar last_char = pattern[m - 1]; 1171 pchar last_char = pattern[m - 1];
1172 int last_char_shift = m - 1 - CharOccurence<schar, pchar>(last_char);
Erik Corry 2008/12/17 09:07:18 Noun. occurence. Common misspelling of occurrence.
1173 // Perform search 1173 // Perform search
1174 for (idx = start_index; idx <= n - m;) { 1174 for (idx = start_index; idx <= n - m;) {
1175 int j = m - 1; 1175 int j = m - 1;
1176 int c; 1176 int c;
1177 while (last_char != (c = subject[idx + j])) { 1177 while (last_char != (c = subject[idx + j])) {
1178 int bc_occ = CharOccurence<schar, pchar>(c); 1178 int bc_occ = CharOccurence<schar, pchar>(c);
1179 int shift = j - bc_occ; 1179 int shift = j - bc_occ;
1180 idx += shift; 1180 idx += shift;
1181 badness += 1 - shift; // at most zero, so badness cannot increase. 1181 badness += 1 - shift; // at most zero, so badness cannot increase.
1182 if (idx > n - m) { 1182 if (idx > n - m) {
1183 *complete = true; 1183 *complete = true;
1184 return -1; 1184 return -1;
1185 } 1185 }
1186 } 1186 }
1187 j--; 1187 j--;
1188 while (j >= 0 && pattern[j] == (c = subject[idx + j])) j--; 1188 while (j >= 0 && pattern[j] == (subject[idx + j])) j--;
1189 if (j < 0) { 1189 if (j < 0) {
1190 *complete = true; 1190 *complete = true;
1191 return idx; 1191 return idx;
1192 } else { 1192 } else {
1193 int bc_occ = CharOccurence<schar, pchar>(c); 1193 idx += last_char_shift;
1194 int shift = bc_occ < j ? j - bc_occ : 1;
1195 idx += shift;
1196 // Badness increases by the number of characters we have 1194 // Badness increases by the number of characters we have
1197 // checked, and decreases by the number of characters we 1195 // checked, and decreases by the number of characters we
1198 // can skip by shifting. It's a measure of how we are doing 1196 // can skip by shifting. It's a measure of how we are doing
1199 // compared to reading each character exactly once. 1197 // compared to reading each character exactly once.
1200 badness += (m - j) - shift; 1198 badness += (m - j) - last_char_shift;
1201 if (badness > 0) { 1199 if (badness > 0) {
1202 *complete = false; 1200 *complete = false;
1203 return idx; 1201 return idx;
1204 } 1202 }
1205 } 1203 }
1206 } 1204 }
1207 *complete = true; 1205 *complete = true;
1208 return -1; 1206 return -1;
1209 } 1207 }
1210 1208
(...skipping 19 matching lines...) Expand all
1230 idx += shift; 1228 idx += shift;
1231 if (idx > n - m) { 1229 if (idx > n - m) {
1232 return -1; 1230 return -1;
1233 } 1231 }
1234 } 1232 }
1235 while (j >= 0 && pattern[j] == (c = subject[idx + j])) j--; 1233 while (j >= 0 && pattern[j] == (c = subject[idx + j])) j--;
1236 if (j < 0) { 1234 if (j < 0) {
1237 return idx; 1235 return idx;
1238 } else if (j < start) { 1236 } else if (j < start) {
1239 // we have matched more than our tables allow us to be smart about. 1237 // we have matched more than our tables allow us to be smart about.
1240 idx += 1; 1238 // Fall back on BMH shift.
1239 idx += m - 1 - CharOccurence<schar, pchar>(last_char);
1241 } else { 1240 } else {
1242 int gs_shift = bmgs_buffers.shift(j + 1); // Good suffix shift. 1241 int gs_shift = bmgs_buffers.shift(j + 1); // Good suffix shift.
1243 int bc_occ = CharOccurence<schar, pchar>(c); 1242 int bc_occ = CharOccurence<schar, pchar>(c);
1244 int shift = j - bc_occ; // Bad-char shift. 1243 int shift = j - bc_occ; // Bad-char shift.
1245 shift = (gs_shift > shift) ? gs_shift : shift; 1244 if (gs_shift > shift) {
1245 shift = gs_shift;
1246 }
1246 idx += shift; 1247 idx += shift;
1247 } 1248 }
1248 } while (idx <= n - m); 1249 } while (idx <= n - m);
1249 1250
1250 return -1; 1251 return -1;
1251 } 1252 }
1252 1253
1253 1254
1254 template <typename schar> 1255 template <typename schar>
1255 static int SingleCharIndexOf(Vector<const schar> string, 1256 static int SingleCharIndexOf(Vector<const schar> string,
(...skipping 23 matching lines...) Expand all
1279 // algorithm. 1280 // algorithm.
1280 int badness = -10 - (pattern.length() << 2); 1281 int badness = -10 - (pattern.length() << 2);
1281 // We know our pattern is at least 2 characters, we cache the first so 1282 // We know our pattern is at least 2 characters, we cache the first so
1282 // the common case of the first character not matching is faster. 1283 // the common case of the first character not matching is faster.
1283 pchar pattern_first_char = pattern[0]; 1284 pchar pattern_first_char = pattern[0];
1284 1285
1285 for (int i = idx, n = subject.length() - pattern.length(); i <= n; i++) { 1286 for (int i = idx, n = subject.length() - pattern.length(); i <= n; i++) {
1286 badness++; 1287 badness++;
1287 if (badness > 0) { 1288 if (badness > 0) {
1288 *complete = false; 1289 *complete = false;
1289 return (i); 1290 return i;
1290 } 1291 }
1291 if (subject[i] != pattern_first_char) continue; 1292 if (subject[i] != pattern_first_char) continue;
1292 int j = 1; 1293 int j = 1;
1293 do { 1294 do {
1294 if (pattern[j] != subject[i+j]) { 1295 if (pattern[j] != subject[i+j]) {
1295 break; 1296 break;
1296 } 1297 }
1297 j++; 1298 j++;
1298 } while (j < pattern.length()); 1299 } while (j < pattern.length());
1299 if (j == pattern.length()) { 1300 if (j == pattern.length()) {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1350 if (pat.length() < kBMMinPatternLength) { 1351 if (pat.length() < kBMMinPatternLength) {
1351 // We don't believe fancy searching can ever be more efficient. 1352 // We don't believe fancy searching can ever be more efficient.
1352 // The max shift of Boyer-Moore on a pattern of this length does 1353 // The max shift of Boyer-Moore on a pattern of this length does
1353 // not compensate for the overhead. 1354 // not compensate for the overhead.
1354 return SimpleIndexOf(sub, pat, start_index); 1355 return SimpleIndexOf(sub, pat, start_index);
1355 } 1356 }
1356 // Try algorithms in order of increasing setup cost and expected performance. 1357 // Try algorithms in order of increasing setup cost and expected performance.
1357 bool complete; 1358 bool complete;
1358 int idx = SimpleIndexOf(sub, pat, start_index, &complete); 1359 int idx = SimpleIndexOf(sub, pat, start_index, &complete);
1359 if (complete) return idx; 1360 if (complete) return idx;
1360 idx = BoyerMooreSimplified(sub, pat, idx, &complete); 1361 idx = BoyerMooreHorsepool(sub, pat, idx, &complete);
1361 if (complete) return idx; 1362 if (complete) return idx;
1362 return BoyerMooreIndexOf(sub, pat, idx); 1363 return BoyerMooreIndexOf(sub, pat, idx);
1363 } 1364 }
1364 1365
1365 // Perform string match of pattern on subject, starting at start index. 1366 // Perform string match of pattern on subject, starting at start index.
1366 // Caller must ensure that 0 <= start_index <= sub->length(), 1367 // Caller must ensure that 0 <= start_index <= sub->length(),
1367 // and should check that pat->length() + start_index <= sub->length() 1368 // and should check that pat->length() + start_index <= sub->length()
1368 int Runtime::StringMatch(Handle<String> sub, 1369 int Runtime::StringMatch(Handle<String> sub,
1369 Handle<String> pat, 1370 Handle<String> pat,
1370 int start_index) { 1371 int start_index) {
(...skipping 4500 matching lines...) Expand 10 before | Expand all | Expand 10 after
5871 } else { 5872 } else {
5872 // Handle last resort GC and make sure to allow future allocations 5873 // Handle last resort GC and make sure to allow future allocations
5873 // to grow the heap without causing GCs (if possible). 5874 // to grow the heap without causing GCs (if possible).
5874 Counters::gc_last_resort_from_js.Increment(); 5875 Counters::gc_last_resort_from_js.Increment();
5875 Heap::CollectAllGarbage(); 5876 Heap::CollectAllGarbage();
5876 } 5877 }
5877 } 5878 }
5878 5879
5879 5880
5880 } } // namespace v8::internal 5881 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698