perf(libc): search memrchr by block and memccpy with memchr #17345
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/17345"
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:22:53Z.
What
memrchrandmemccpyhandled one byte per step.memrchrscans backward in 16-byte aligned blocks with SSE2. It starts at the block that holds the last byte, masks bytes past the end and before the start, and returns the highest match. Aligned loads stay inside the pages that hold the buffer. The loading helper is markedno_sanitize("address"), as inGuestAllocations.cpp. Builds without__SSE2__(the Emscripten test build compiles this file), and ranges that would wrap the address space, keep main's loop.memccpyfinds the stop byte with the hostmemchrand then copies the prefix in onememmove. Since C11,memchrbehaves as if it reads sequentially and stops at the first match. So like the old loop, it reads no further than the stop byte. Overlapping buffers are undefined formemccpy;memmovekeeps that case well defined on the host.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.guest_memrchr,guest_memccpy_bounds,guest_bounded_search_guards,guest_stringsandguest_string_comparepass.The new
guest_memrchr_blockstest runs 40000 random cases against naive references:PROT_NONE(PAGE_NOACCESS) page on both sides;cvalues that need theunsigned charconversion.For
memccpy, the test checks the return value and the whole destination page, so a byte written past the stop byte fails.Mutation check. Each change below makes the new test fail:
Removing the
n == 0early return inmemccpystill passes, becausememchrandmemmovealready accept a zero count.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, at22f330a971against mainbfdb58119e. Medians of 31 samples, in ns per call, on a buffer ofawithout the target byte:memrchron 7 bytes costs about +0.9 ns. Every other case is equal or faster.Order with other open PRs
Strings.cppandlibc/CMakeLists.txt. All three branches merge with each other cleanly in any order (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:17:32Z.
Review: COMMENTED
Windows run of head
22f330a971, 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_memrchr_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 ofmemrchrandmemccpy, 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.
From reading:
memccpynow copies withmemmoveafter amemchr. That differs from the old byte loop only when the buffers overlap, which is undefined formemccpy, so it's fine.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T10:19:45Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this; the points below are optional.
What I ran
Head
82e042f83b76618fcc789d7791593cc1311c9d88, basemain. No build, no tests, no compiler. Reading only, through the repository reader (repo_read.py):The changed hunks in
diff.patchwere read in full for both reviewed files.Results
Strings.cppCMakeLists.txt<bit>no_sanitizeclaim in the descriptionGuestAllocations.cppuses the same attributeGuestMemrchrBlocks.cppFindings
Blocking
None.
Non-blocking
core/libs/prx/libc/src/Strings.cpp:301:FindLastBytehas no[[gnu::no_sanitize("address")]], while the siblingFindLastUnit(line 181) does. The raw loads happen inMatchBytes(line 296), which has the attribute, so this is probably fine. ASan was not run to confirm it.core/libs/prx/libc/src/Strings.cpp:301-317: the block walk duplicates the shape ofFindLastUnit. This is a style point only; the logic inFindLastBytechecks out for the cases I traced (single byte, match in the start block, match in an earlier block, and the wrap guard inmemrchr_nid_postfixat line 631).Not verified
core/libs/tests/GuestMemrchrBlocks.cppwas not read. It is in the excluded list because the rule*lock*matches the word "Blocks" in its name. This is a side effect of the rule, not a repository problem. The 40000-case test, the mutation claims, and the test behaviour are therefore not verified.core/libs/prx/libc/CMakeLists.txt:582-590) was read as a diff hunk only.memchrreads no further than the stop byte: this rests on the C standard wording. The real implementation's reads were not checked.Files read:
core/libs/prx/libc/CMakeLists.txt(diff hunks only),core/libs/prx/libc/src/Strings.cpp(diff hunks and two head ranges). Files excluded:core/libs/tests/GuestMemrchrBlocks.cpp.To get this to a good state
Nothing is required. The points under Findings are optional.