perf(libc): compare wide strings eight units at a time #17336
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/17336"
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:16:14Z.
What
wcscmp,wcsncmpandwmemcmpcompared one UTF-16 unit per step. They now skip the equal prefix eight units at a time with SSE2, then the existing loop compares the remaining units and returns the result. Results are unchanged.wmemcmponly loads inside[s, s + n).wcscmpandwcsncmpcan read past a terminator. Before each run of blocks they count how many 16-byte blocks end inside the current 4 KiB page of both strings. They load only those blocks, then step one unit at a time across the page boundary. A load never crosses into an unmapped page. The loading helper is markedno_sanitize("address"), as inGuestAllocations.cpp.__SSE2__(the Emscripten test build compiles this file) keep main's loops unchanged.No exported functions or NIDs are added or changed.
Tested
Ubuntu 26.04, GCC 15.2, Release,
-DBUILD_TESTING=ON. All 18guest_wide*,guest_wcs*,guest_wmemchr*andguest_string*tests pass, includingguest_wcscmp_boundary.The new
guest_wide_compare_blockstest runs 40000 random cases against a naive reference:PROT_NONE(PAGE_NOACCESS) page;wcsncmplimits below and past the terminator, plusSIZE_MAX;wmemcmpin both argument orders.Mutation check. Each change below makes the new test fail (crash, abort or hang):
nafter the skip.Dropping the
& 0xffffon the mismatch mask still passes. It only makes the skip stop after one block, so the result is still right.Windows:
Strings.cppand the new test pass a syntax-only build forx86_64-windows-gnu(zig/clang). Not built with MinGW or run on Windows.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, at060f5eb7eaagainst mainbfdb58119e. Medians of 31 samples, in ns per call, on two equal strings ofa:wcscmpon a 7-unit string costs about +1.6 ns. Every other case is equal or faster.Order with other open PRs
Strings.cppand adds a test in another place inlibc/CMakeLists.txt. The two branches merge cleanly in either order (git merge-tree).pr_overlap(#15807, #16210, #8965 and #5021 onStrings.cpp, and others onlibc/CMakeLists.txt),git merge-treeshows the same conflicting files against this branch as againstmain.Later sibling PRs from me: #17345, #17354 and #17394. They also merge cleanly with this branch in any order.
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:17:33Z.
Review: COMMENTED
Windows run of head
060f5eb7ea, merged ontomain9fabd4aa7e. To save builds, this ran in one tree together with 11 other open PRs that touch different code (four libc string speed-ups and eight test-only PRs); all merged cleanly. Setup: Windows 11, WinLibs GCC 15.2 (x86_64-ucrt-posix-seh), Release,BUILD_TESTING=ON, i7-12800H.guest_wide_compare_blockswith--repeat until-fail:5: 5 / 5 pass.strnlen,stpncpy,memrchr,memccpy,wcslen,wcschr,wcsrchr,wmemchr,wcsnlen_s,wcscmp,wcsncmpandwmemcmpwith byte-by-byte reference loops. It runs 400,000 rounds with random lengths, alphabets and odd or even alignments. Half of the strings end right before aPAGE_NOACCESSpage, andmemrchralso starts right after one, so any read past the bounds faults. With this PR's code (all four speed-ups together), 5,600,000 checks agree and nothing faults.main'sStrings.cppandStringExtensions.cpp, the same tests and the fuzz pass too, so the new test pins the result ofwcscmp,wcsncmpandwmemcmp, not the speed-up. That's expected for a speed change, but it means the new code paths are covered only by the equivalence checks.I didn't measure the speed.