perf(libc): scan wide strings sixteen bytes at a time #17326
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/17326"
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-08T17:57:59Z.
What
wcslen,wcsnlen_s,wmemchr,wcschrandwcsrchrlooked at one UTF-16 unit per step. They now compare eight units per SSE2 step.n. The helpers that load are markedno_sanitize("address"), as inGuestAllocations.cpp.wcschrstops at the first match or terminator.wcsrchrkeeps the last match in each block up to the terminator.wmemchrandwcsnlen_sshare one bounded scan. A count past the end of the address space is clamped, soSIZE_MAXstill works when the unit is present.__SSE2__(the Emscripten test build compiles this file), keep main's loops.The results are unchanged, and 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. These include the guard-page testsguest_wide_char_boundary,guest_wmemchr_boundary,guest_wcslen_boundaryandguest_wide_length_protected.The new
guest_wide_scan_blockstest runs 20000 random cases against naive references:wmemchr/wcsnlen_slimits below, at and past the terminator, plusSIZE_MAXand a null pointer withn == 0.Mutation check. Each change below makes the test suite fail:
wcsrchrmatches past the terminator;wcsrchrlanes before the start;wcschrreturning the terminator;wcsnlen_sreturning 0 when no terminator is found;SIZE_MAXclamp.Ending the bounded loop at
< 16instead of<= 16passes the new test, because it only reads one extra aligned block. The guard-page tests fail on it.Windows:
Strings.cppand the new test pass a syntax-only build forx86_64-windows-gnu(zig/clang), where__SSE2__is defined. 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, at1393eea62dagainst mainbfdb58119e. Medians of 31 samples, in ns per call, on a string ofawith the target absent (wcsrchrsearches fora):wmemchrover 7 units costs about +1.7 ns. Every other case is equal or faster.Order with other open PRs
pr_overlaplists #15807, #16210, #8965 and #5021 onStrings.cpp, and others onlibc/CMakeLists.txt. None of them change these functions. For every listed PR,git merge-treeshows the same conflicting files against this branch as againstmain, so this branch adds no conflict in either order.Sibling PRs from me in the same area: #17336 (wide compares), #17345 (
memrchr/memccpy), #17354 (strnlen/stpncpy) and #17394 (strcasecmp). All of them merge with this branch and each other cleanly in any order (git merge-tree).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:34Z.
Review: COMMENTED
Windows run of head
1393eea62d, 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_scan_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 ofwcslen,wcschr,wcsrchr,wmemchrandwcsnlen_s, 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.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-08T19:51:54Z.
Review: DISMISSED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
1393eea62d:tools/check_conventions.pypassed;git diff --checkclean.guest_wide_scan_blocks_testsbuilt cleanly.guest_wide_scan_blocks):Requesting changes for the points above.