String search performance improvements:
* Using memchr for first/only char lookup is faster than hand-coded loop. It processes one machine word per iteration so it helps even more on x64. * Tweaked badness computation in simple search. We pay only for the first char memchr scans, not for all of them. Review URL: http://codereview.chromium.org/1100002 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@4173 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
parent
e090f46a55
commit
b148282b56
@ -2353,6 +2353,14 @@ template <typename schar>
|
|||||||
static inline int SingleCharIndexOf(Vector<const schar> string,
|
static inline int SingleCharIndexOf(Vector<const schar> string,
|
||||||
schar pattern_char,
|
schar pattern_char,
|
||||||
int start_index) {
|
int start_index) {
|
||||||
|
if (sizeof(schar) == 1) {
|
||||||
|
schar* pos = reinterpret_cast<schar*>(
|
||||||
|
memchr(string.start() + start_index,
|
||||||
|
pattern_char,
|
||||||
|
string.length() - start_index));
|
||||||
|
if (pos == NULL) return -1;
|
||||||
|
return pos - string.start();
|
||||||
|
}
|
||||||
for (int i = start_index, n = string.length(); i < n; i++) {
|
for (int i = start_index, n = string.length(); i < n; i++) {
|
||||||
if (pattern_char == string[i]) {
|
if (pattern_char == string[i]) {
|
||||||
return i;
|
return i;
|
||||||
@ -2400,7 +2408,18 @@ static int SimpleIndexOf(Vector<const schar> subject,
|
|||||||
*complete = false;
|
*complete = false;
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
if (subject[i] != pattern_first_char) continue;
|
if (sizeof(schar) == 1 && sizeof(pchar) == 1) {
|
||||||
|
schar* pos = reinterpret_cast<schar*>(memchr(subject.start() + i,
|
||||||
|
pattern_first_char,
|
||||||
|
n - i + 1));
|
||||||
|
if (pos == NULL) {
|
||||||
|
*complete = true;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
i = pos - subject.start();
|
||||||
|
} else {
|
||||||
|
if (subject[i] != pattern_first_char) continue;
|
||||||
|
}
|
||||||
int j = 1;
|
int j = 1;
|
||||||
do {
|
do {
|
||||||
if (pattern[j] != subject[i+j]) {
|
if (pattern[j] != subject[i+j]) {
|
||||||
@ -2425,7 +2444,15 @@ static int SimpleIndexOf(Vector<const schar> subject,
|
|||||||
int idx) {
|
int idx) {
|
||||||
pchar pattern_first_char = pattern[0];
|
pchar pattern_first_char = pattern[0];
|
||||||
for (int i = idx, n = subject.length() - pattern.length(); i <= n; i++) {
|
for (int i = idx, n = subject.length() - pattern.length(); i <= n; i++) {
|
||||||
if (subject[i] != pattern_first_char) continue;
|
if (sizeof(schar) == 1 && sizeof(pchar) == 1) {
|
||||||
|
schar* pos = reinterpret_cast<schar*>(memchr(subject.start() + i,
|
||||||
|
pattern_first_char,
|
||||||
|
n - i + 1));
|
||||||
|
if (pos == NULL) return -1;
|
||||||
|
i = pos - subject.start();
|
||||||
|
} else {
|
||||||
|
if (subject[i] != pattern_first_char) continue;
|
||||||
|
}
|
||||||
int j = 1;
|
int j = 1;
|
||||||
do {
|
do {
|
||||||
if (pattern[j] != subject[i+j]) {
|
if (pattern[j] != subject[i+j]) {
|
||||||
|
Loading…
Reference in New Issue
Block a user