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

Unified Diff: third_party/WebKit/Source/wtf/text/StringImpl.cpp

Issue 2116533005: Optimize StringImpl::find when searching for patterns with length > 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: correct. Created 4 years, 6 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 | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/wtf/text/StringImpl.cpp
diff --git a/third_party/WebKit/Source/wtf/text/StringImpl.cpp b/third_party/WebKit/Source/wtf/text/StringImpl.cpp
index e7004b9244bd8d3feaf910f8135488ca4c93a090..b1b14798ee8d937110cc3a550066b33d833cba93 100644
--- a/third_party/WebKit/Source/wtf/text/StringImpl.cpp
+++ b/third_party/WebKit/Source/wtf/text/StringImpl.cpp
@@ -1246,15 +1246,26 @@ ALWAYS_INLINE static size_t findInternal(const SearchCharacterType* searchCharac
// delta is the number of additional times to test; delta == 0 means test only once.
unsigned delta = searchLength - matchLength;
+ // Find on a single character is very fast, so first do that to figure out
+ // where to start.
+ size_t start = WTF::find(searchCharacters, searchLength, matchCharacters[0]);
+ if (start == kNotFound || start > delta)
+ return kNotFound;
+
+ // TODO(esprehn): Consider using Boyer-Moore-Horspool or a hybrid approach
+ // like v8/src/string-search.h, also investigate the badness factor used
+ // by v8 where they use single char find() repeatedly until they think
+ // it's too costly then switch to Boyer-Moore-Horspool.
+
unsigned searchHash = 0;
unsigned matchHash = 0;
for (unsigned i = 0; i < matchLength; ++i) {
- searchHash += searchCharacters[i];
+ searchHash += searchCharacters[start + i];
matchHash += matchCharacters[i];
}
- unsigned i = 0;
+ unsigned i = start;
// keep looping until we match
while (searchHash != matchHash || !equal(searchCharacters + i, matchCharacters, matchLength)) {
if (i == delta)
« 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