perf(libc): skip tolower for equal bytes in strcasecmp #17394
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/17394"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by rocklouis055 on 2026-11-08T18:47:54Z.
What
strcasecmpandstrncasecmpcalledtoloweron both bytes at every position. Eachtolowercall goes throughConvertCharacter, which validates the value and looks up the classicctype<char>facet, so it costs several nanoseconds per call.Two equal bytes always fold to the same value. So when the bytes match, the loops now only check for the terminator and move on, and
tolowerruns only where the bytes differ. The result and the return values (-1/0/1) are unchanged.No exported functions or NIDs are added or changed.
Tested
Ubuntu 26.04, GCC 15.2, Release,
-DBUILD_TESTING=ON.guest_stringsandguest_fnmatchpass.The new
guest_case_compare_randomtest runs 100000 random pairs against a reference that foldsA–Zlike the classic locale:A–Zanda–z,0x7f, and bytes0x80and above;SIZE_MAX;The test also checks that
tolowermatches that fold for all 256 byte values, so the reference stays honest.Mutation check. Each change below makes the test fail:
These still pass, as expected:
toloweranyway, which gives the same result;*s1 == 0check after folding. It can't be reached in the classic locale, where no other byte folds to 0, so it is kept as in main.Performance: I have no title capture. I can't run a title, so there are no
main/PR summary lines.Micro-benchmark through the built
libc.prx, at964eb803d6against mainbfdb58119e. Two runs each, in ns per call (medians of 31 samples). "same" compares equal strings; "case" compares strings that differ in case at every byte.Where most bytes match, the compare gets much faster. Where every byte differs only in case, both versions spend nearly all their time in
tolower. Those rows vary a lot between runs on both builds, and I can't separate them from noise. The extra byte compare costs nothing next to twotolowercalls.#14022 (romainhedouin) makes
toloweritself read from the existing case tables. That cuts the cost of the positions that still fold. It changesLocaleSupport.cpponly, so the two PRs combine and merge in either order.Order with other open PRs
libc/CMakeLists.txt. All five branches merge with each other cleanly (git merge-tree).pr_overlap,git merge-treeshows the same conflicting files against this branch as againstmain.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file. This PR adds no unimplemented paths..mdfiles or images added to the repository🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-08T19:39:15Z.
Review: COMMENTED
Windows run of head
964eb803d6, merged ontomain095ff72367. To save builds, this ran in one tree together with 24 other open PRs that touch different code; all 25 merged cleanly. Setup: Windows 11, WinLibs GCC 15.2 (x86_64-ucrt-posix-seh), Release,BUILD_TESTING=ON.guest_case_compare_randomwith--repeat until-fail:5: 5 / 5 pass.main'sStrings.cppand this PR's test, it still passes 5 / 5. That's expected for a speed change: the new random test checks the result, not the shortcut.The shortcut is sound. Equal bytes have equal
tolowervalues, and the*s1 == 0check inside it returns 0 at the shared terminator, as the old loop did. I didn't measure the speed.