-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Optimize stripos #7852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize stripos #7852
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -919,6 +919,67 @@ static zend_always_inline void zend_unwrap_reference(zval *op) /* {{{ */ | |
} | ||
/* }}} */ | ||
|
||
static zend_always_inline bool zend_strnieq(const char *ptr1, const char *ptr2, size_t num) | ||
{ | ||
const char *end = ptr1 + num; | ||
while (ptr1 < end) { | ||
if (zend_tolower_ascii(*ptr1++) != zend_tolower_ascii(*ptr2++)) { | ||
return 0; | ||
} | ||
} | ||
return 1; | ||
} | ||
|
||
static zend_always_inline const char * | ||
zend_memnistr(const char *haystack, const char *needle, size_t needle_len, const char *end) | ||
{ | ||
ZEND_ASSERT(end >= haystack); | ||
|
||
if (UNEXPECTED(needle_len > (end - haystack))) { | ||
return NULL; | ||
} | ||
|
||
if (UNEXPECTED(needle_len == 0)) { | ||
return haystack; | ||
} | ||
|
||
const char first_lower = zend_tolower_ascii(*needle); | ||
const char first_upper = zend_toupper_ascii(*needle); | ||
const char *p_lower = (const char *)memchr(haystack, first_lower, end - haystack); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For future scope: I think there may be a way to optimize this with SSE for really long strings, but it may not be worth it, especially with stripos not being used that often. (both checking if the first character exists case-insensitively, and checking if long needles match) Definitely not something to add to this PR.
For a block of 8/16/32 bytes, that could be checked by:
This would make the worst case of case-insensitive search faster ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know nothing about SSE, but as you mentioned, the new implementation should already be much better for large strings, we can always improve it at a later point. |
||
const char *p_upper = NULL; | ||
if (first_lower != first_upper) { | ||
// If the needle length is 1 we don't need to look beyond p_lower as it is a guaranteed match | ||
size_t upper_search_length = end - (needle_len == 1 && p_lower != NULL ? p_lower : haystack); | ||
p_upper = (const char *)memchr(haystack, first_upper, upper_search_length); | ||
} | ||
const char *p = !p_upper || (p_lower && p_lower < p_upper) ? p_lower : p_upper; | ||
|
||
if (needle_len == 1) { | ||
return p; | ||
} | ||
|
||
const char needle_end_lower = zend_tolower_ascii(needle[needle_len - 1]); | ||
const char needle_end_upper = zend_toupper_ascii(needle[needle_len - 1]); | ||
end -= needle_len; | ||
|
||
while (p && p <= end) { | ||
if (needle_end_lower == p[needle_len - 1] || needle_end_upper == p[needle_len - 1]) { | ||
if (zend_strnieq(needle + 1, p + 1, needle_len - 2)) { | ||
return p; | ||
} | ||
} | ||
if (p_lower == p) { | ||
p_lower = (const char *)memchr(p_lower + 1, first_lower, end - p_lower); | ||
} | ||
if (p_upper == p) { | ||
p_upper = (const char *)memchr(p_upper + 1, first_upper, end - p_upper); | ||
} | ||
p = !p_upper || (p_lower && p_lower < p_upper) ? p_lower : p_upper; | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
|
||
END_EXTERN_C() | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.