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

Side by Side 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, 5 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 | « 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 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller ( mueller@kde.org ) 4 * (C) 2001 Dirk Mueller ( mueller@kde.org )
5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2013 Apple Inc. All r ights reserved. 5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2013 Apple Inc. All r ights reserved.
6 * Copyright (C) 2006 Andrew Wellington (proton@wiretapped.net) 6 * Copyright (C) 2006 Andrew Wellington (proton@wiretapped.net)
7 * 7 *
8 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public 9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either 10 * License as published by the Free Software Foundation; either
(...skipping 1228 matching lines...) Expand 10 before | Expand all | Expand 10 after
1239 1239
1240 template <typename SearchCharacterType, typename MatchCharacterType> 1240 template <typename SearchCharacterType, typename MatchCharacterType>
1241 ALWAYS_INLINE static size_t findInternal(const SearchCharacterType* searchCharac ters, const MatchCharacterType* matchCharacters, unsigned index, unsigned search Length, unsigned matchLength) 1241 ALWAYS_INLINE static size_t findInternal(const SearchCharacterType* searchCharac ters, const MatchCharacterType* matchCharacters, unsigned index, unsigned search Length, unsigned matchLength)
1242 { 1242 {
1243 // Optimization: keep a running hash of the strings, 1243 // Optimization: keep a running hash of the strings,
1244 // only call equal() if the hashes match. 1244 // only call equal() if the hashes match.
1245 1245
1246 // delta is the number of additional times to test; delta == 0 means test on ly once. 1246 // delta is the number of additional times to test; delta == 0 means test on ly once.
1247 unsigned delta = searchLength - matchLength; 1247 unsigned delta = searchLength - matchLength;
1248 1248
1249 // Find on a single character is very fast, so first do that to figure out
1250 // where to start.
1251 size_t start = WTF::find(searchCharacters, searchLength, matchCharacters[0]) ;
1252 if (start == kNotFound || start > delta)
1253 return kNotFound;
1254
1255 // TODO(esprehn): Consider using Boyer-Moore-Horspool or a hybrid approach
1256 // like v8/src/string-search.h, also investigate the badness factor used
1257 // by v8 where they use single char find() repeatedly until they think
1258 // it's too costly then switch to Boyer-Moore-Horspool.
1259
1249 unsigned searchHash = 0; 1260 unsigned searchHash = 0;
1250 unsigned matchHash = 0; 1261 unsigned matchHash = 0;
1251 1262
1252 for (unsigned i = 0; i < matchLength; ++i) { 1263 for (unsigned i = 0; i < matchLength; ++i) {
1253 searchHash += searchCharacters[i]; 1264 searchHash += searchCharacters[start + i];
1254 matchHash += matchCharacters[i]; 1265 matchHash += matchCharacters[i];
1255 } 1266 }
1256 1267
1257 unsigned i = 0; 1268 unsigned i = start;
1258 // keep looping until we match 1269 // keep looping until we match
1259 while (searchHash != matchHash || !equal(searchCharacters + i, matchCharacte rs, matchLength)) { 1270 while (searchHash != matchHash || !equal(searchCharacters + i, matchCharacte rs, matchLength)) {
1260 if (i == delta) 1271 if (i == delta)
1261 return kNotFound; 1272 return kNotFound;
1262 searchHash += searchCharacters[i + matchLength]; 1273 searchHash += searchCharacters[i + matchLength];
1263 searchHash -= searchCharacters[i]; 1274 searchHash -= searchCharacters[i];
1264 ++i; 1275 ++i;
1265 } 1276 }
1266 return index + i; 1277 return index + i;
1267 } 1278 }
(...skipping 1090 matching lines...) Expand 10 before | Expand all | Expand 10 after
2358 } else if (localeIdMatchesLang(localeIdentifier, "lt")) { 2369 } else if (localeIdMatchesLang(localeIdentifier, "lt")) {
2359 // TODO(rob.buis) implement upper-casing rules for lt 2370 // TODO(rob.buis) implement upper-casing rules for lt
2360 // like in StringImpl::upper(locale). 2371 // like in StringImpl::upper(locale).
2361 } 2372 }
2362 } 2373 }
2363 2374
2364 return toUpper(c); 2375 return toUpper(c);
2365 } 2376 }
2366 2377
2367 } // namespace WTF 2378 } // namespace WTF
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