From f294fe2a391fb7d6768dcb5dd5b8732104a6ee32 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Wed, 29 Jan 2025 18:27:01 -0500 Subject: [PATCH] Reintroduce aligned-pointers optimization for strcmp. It was initially added by tqh in ac827a2baa5e0c486b554cdee56c72624b499a40, but was later removed in 39a81e5ac632d3356ebd4c0b40fb24aa3228833d due to being rather broken. This new version is loosely based on that design, but removes the comparison logic out of the loop and lets the unaligned version deal with the actual per-character comparison. Saves around 0.1s out of 3.5s in the best case in a "jam -q HaikuDepot" run with nothing to do (and that's only comparing with different libroots on the same kernel; as this strcmp is used in the kernel as well, we will benefit from this optimization there, too.) --- src/system/libroot/posix/string/strcmp.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/system/libroot/posix/string/strcmp.c b/src/system/libroot/posix/string/strcmp.c index 56e058e2cb..8ac20d9499 100644 --- a/src/system/libroot/posix/string/strcmp.c +++ b/src/system/libroot/posix/string/strcmp.c @@ -15,6 +15,18 @@ int strcmp(char const *a, char const *b) { + if ((((addr_t)a) & 3) == 0 && (((addr_t)b) & 3) == 0) { + uint32* a32 = (uint32*)a; + uint32* b32 = (uint32*)b; + + while (*a32 == *b32 && LACKS_ZERO_BYTE((*a32))) { + a32++; + b32++; + } + a = (const char *)a32; + b = (const char *)b32; + } + while (true) { int cmp = (unsigned char)*a - (unsigned char)*b++; if (cmp != 0 || *a++ == '\0')