perf(libc): compare wide strings eight units at a time #17336

Merged
rocklouis055 merged 1 commit from upstream-pr/17336 into main 2026-11-09 01:55:27 +00:00
rocklouis055 commented 2026-11-08 18:16:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rocklouis055 on 2026-11-08T18:16:14Z.


What

wcscmp, wcsncmp and wmemcmp compared 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.

  • wmemcmp only loads inside [s, s + n).
  • wcscmp and wcsncmp can 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 marked no_sanitize("address"), as in GuestAllocations.cpp.
  • Builds without __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 18 guest_wide*, guest_wcs*, guest_wmemchr* and guest_string* tests pass, including guest_wcscmp_boundary.

The new guest_wide_compare_blocks test runs 40000 random cases against a naive reference:

  • both strings sit in pages followed by a PROT_NONE (PAGE_NOACCESS) page;
  • each string ends either exactly at the guard page or up to 129 bytes before it, with odd offsets included, so the two strings reach their page ends at different units;
  • lengths 0–600;
  • zero to two changed units, sometimes an early terminator;
  • wcsncmp limits below and past the terminator, plus SIZE_MAX;
  • wmemcmp in both argument orders.

Mutation check. Each change below makes the new test fail (crash, abort or hang):

  • checking only the first string's page;
  • allowing one more block per page;
  • not stopping at a terminator in the per-unit step;
  • not stopping at a terminator in the vector step;
  • an off-by-one in the mismatch position;
  • rounding the block count up;
  • not reducing n after the skip.

Dropping the & 0xffff on the mismatch mask still passes. It only makes the skip stop after one block, so the result is still right.

Windows: Strings.cpp and the new test pass a syntax-only build for x86_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, at 060f5eb7ea against main bfdb58119e. Medians of 31 samples, in ns per call, on two equal strings of a:

units wcscmp wcsncmp wmemcmp
7 3.2 → 4.8 4.1 → 4.3 9.5 → 9.6
31 15.0 → 5.2 15.7 → 5.2 15.4 → 4.3
255 86 → 26 110 → 26 82 → 19
4095 1078 → 456 1540 → 457 1064 → 289
65535 20634 → 6975 24530 → 6976 20163 → 6245

wcscmp on a 7-unit string costs about +1.6 ns. Every other case is equal or faster.

Order with other open PRs

  • #17326 (mine, wide scans) changes other functions in Strings.cpp and adds a test in another place in libc/CMakeLists.txt. The two branches merge cleanly in either order (git merge-tree).
  • For every other PR listed by pr_overlap (#15807, #16210, #8965 and #5021 on Strings.cpp, and others on libc/CMakeLists.txt), git merge-tree shows the same conflicting files against this branch as against main.

Later sibling PRs from me: #17345, #17354 and #17394. They also merge cleanly with this branch in any order.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file. This PR adds no unimplemented paths.
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source. This PR adds no third-party code.
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5213841923 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17336). Originally posted by [rocklouis055](https://github.com/rocklouis055) on 2026-11-08T18:16:14Z. --- ### What `wcscmp`, `wcsncmp` and `wmemcmp` compared 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. - `wmemcmp` only loads inside `[s, s + n)`. - `wcscmp` and `wcsncmp` can 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 marked `no_sanitize("address")`, as in `GuestAllocations.cpp`. - Builds without `__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 18 `guest_wide*`, `guest_wcs*`, `guest_wmemchr*` and `guest_string*` tests pass, including `guest_wcscmp_boundary`. The new `guest_wide_compare_blocks` test runs 40000 random cases against a naive reference: - both strings sit in pages followed by a `PROT_NONE` (`PAGE_NOACCESS`) page; - each string ends either exactly at the guard page or up to 129 bytes before it, with odd offsets included, so the two strings reach their page ends at different units; - lengths 0–600; - zero to two changed units, sometimes an early terminator; - `wcsncmp` limits below and past the terminator, plus `SIZE_MAX`; - `wmemcmp` in both argument orders. Mutation check. Each change below makes the new test fail (crash, abort or hang): - checking only the first string's page; - allowing one more block per page; - not stopping at a terminator in the per-unit step; - not stopping at a terminator in the vector step; - an off-by-one in the mismatch position; - rounding the block count up; - not reducing `n` after the skip. Dropping the `& 0xffff` on the mismatch mask still passes. It only makes the skip stop after one block, so the result is still right. Windows: `Strings.cpp` and the new test pass a syntax-only build for `x86_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`, at `060f5eb7ea` against main `bfdb58119e`. Medians of 31 samples, in ns per call, on two equal strings of `a`: | units | wcscmp | wcsncmp | wmemcmp | |---|---|---|---| | 7 | 3.2 → 4.8 | 4.1 → 4.3 | 9.5 → 9.6 | | 31 | 15.0 → 5.2 | 15.7 → 5.2 | 15.4 → 4.3 | | 255 | 86 → 26 | 110 → 26 | 82 → 19 | | 4095 | 1078 → 456 | 1540 → 457 | 1064 → 289 | | 65535 | 20634 → 6975 | 24530 → 6976 | 20163 → 6245 | `wcscmp` on a 7-unit string costs about +1.6 ns. Every other case is equal or faster. ### Order with other open PRs - #17326 (mine, wide scans) changes other functions in `Strings.cpp` and adds a test in another place in `libc/CMakeLists.txt`. The two branches merge cleanly in either order (`git merge-tree`). - For every other PR listed by `pr_overlap` (#15807, #16210, #8965 and #5021 on `Strings.cpp`, and others on `libc/CMakeLists.txt`), `git merge-tree` shows the same conflicting files against this branch as against `main`. Later sibling PRs from me: #17345, #17354 and #17394. They also merge cleanly with this branch in any order. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file. This PR adds no unimplemented paths. - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source. This PR adds no third-party code. - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
gabichuuuuu commented 2026-11-08 19:17:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-08T19:17:33Z.

Review: COMMENTED


Windows run of head 060f5eb7ea, merged onto main 9fabd4aa7e. 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_blocks with --repeat until-fail:5: 5 / 5 pass.
  • Extra differential check, not part of this PR. I wrote a fuzz test that compares strnlen, stpncpy, memrchr, memccpy, wcslen, wcschr, wcsrchr, wmemchr, wcsnlen_s, wcscmp, wcsncmp and wmemcmp with 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 a PAGE_NOACCESS page, and memrchr also 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.
  • With main's Strings.cpp and StringExtensions.cpp, the same tests and the fuzz pass too, so the new test pins the result of wcscmp, wcsncmp and wmemcmp, 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5819285632 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17336#pullrequestreview-5819285632). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-08T19:17:33Z. **Review:** COMMENTED --- Windows run of head `060f5eb7ea`, merged onto `main` `9fabd4aa7e`. 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_blocks` with `--repeat until-fail:5`: 5 / 5 pass. - **Extra differential check, not part of this PR.** I wrote a fuzz test that compares `strnlen`, `stpncpy`, `memrchr`, `memccpy`, `wcslen`, `wcschr`, `wcsrchr`, `wmemchr`, `wcsnlen_s`, `wcscmp`, `wcsncmp` and `wmemcmp` with 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 a `PAGE_NOACCESS` page, and `memrchr` also 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. - With `main`'s `Strings.cpp` and `StringExtensions.cpp`, the same tests and the fuzz pass too, so the new test pins the result of `wcscmp`, `wcsncmp` and `wmemcmp`, 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!17336
No description provided.