fmt/pl: optimize pl_strstr, pl_strchr and pl_strrchr#1577
Draft
sreimers wants to merge 2 commits into
Draft
Conversation
Replace byte-by-byte scan with optimized `memchr` first character matching and confirm with `memcmp`.
2f80490 to
363f11b
Compare
Replace loop with optimized `memchr`
9d7cad4 to
58e831d
Compare
Contributor
|
unrelated comment, this hand-crafted function can probably also be replaced #ifndef HAVE_STRINGS_H
static int casecmp(const struct pl *pl, const char *str)
{
size_t i = 0;
#define LOWER(d) ((d) | 0x20202020)
const uint32_t *p1 = (uint32_t *)pl->p;
const uint32_t *p2 = (uint32_t *)str;
const size_t len = pl->l & ~0x3;
/* Skip any unaligned pointers */
if (((size_t)pl->p) & (sizeof(void *) - 1))
goto next;
if (((size_t)str) & (sizeof(void *) - 1))
goto next;
/* Compare word-wise */
for (; i<len; i+=4) {
if (LOWER(*p1++) != LOWER(*p2++))
return EINVAL;
}
next:
/* Compare byte-wise */
for (; i<pl->l; i++) {
if (tolower(pl->p[i]) != tolower(str[i]))
return EINVAL;
}
return 0;
}
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace byte-by-byte scan with optimized
memchrfirst character matching and confirm withmemcmp.