Skip to content

fmt/pl: optimize pl_strstr, pl_strchr and pl_strrchr#1577

Draft
sreimers wants to merge 2 commits into
mainfrom
optimize_pl_strstr
Draft

fmt/pl: optimize pl_strstr, pl_strchr and pl_strrchr#1577
sreimers wants to merge 2 commits into
mainfrom
optimize_pl_strstr

Conversation

@sreimers

@sreimers sreimers commented May 9, 2026

Copy link
Copy Markdown
Member

Replace byte-by-byte scan with optimized memchr first character matching and confirm with memcmp.

Replace byte-by-byte scan with optimized `memchr` first character
matching and confirm with `memcmp`.
@sreimers sreimers force-pushed the optimize_pl_strstr branch from 2f80490 to 363f11b Compare May 9, 2026 21:25
Replace loop with optimized `memchr`
@sreimers sreimers force-pushed the optimize_pl_strstr branch from 9d7cad4 to 58e831d Compare May 10, 2026 07:20
@sreimers sreimers changed the title fmt/pl: optimize pl_strstr fmt/pl: optimize pl_strstr, pl_strchr and pl_strrchr May 10, 2026
@alfredh

alfredh commented May 12, 2026

Copy link
Copy Markdown
Contributor

unrelated comment, this hand-crafted function can probably also be replaced
by system functions:

#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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants