perf(libc): skip tolower for equal bytes in strcasecmp #17394

Merged
rocklouis055 merged 1 commit from upstream-pr/17394 into main 2026-11-09 11:45:56 +00:00
rocklouis055 commented 2026-11-08 18:47:54 +00:00 (Migrated from github.com)

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


What

strcasecmp and strncasecmp called tolower on both bytes at every position. Each tolower call goes through ConvertCharacter, which validates the value and looks up the classic ctype<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 tolower runs 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_strings and guest_fnmatch pass.

The new guest_case_compare_random test runs 100000 random pairs against a reference that folds A–Z like the classic locale:

  • lengths 0–80, using letters, the bytes around A–Z and a–z, 0x7f, and bytes 0x80 and above;
  • the second string copies the first, then changes some bytes to random values or flips their case;
  • an early terminator in either string;
  • limits from 0 up to past the terminator, plus SIZE_MAX;
  • both argument orders.

The test also checks that tolower matches that fold for all 256 byte values, so the reference stays honest.

Mutation check. Each change below makes the test fail:

  • not stopping at a shared terminator, in either function;
  • returning non-zero for equal strings.

These still pass, as expected:

  • sending some equal bytes through tolower anyway, which gives the same result;
  • dropping the *s1 == 0 check 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, at 964eb803d6 against main bfdb58119e. 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.

bytes strcasecmp same strncasecmp same strcasecmp case strncasecmp case
7 109 → 3.4 110 → 3.7 109 → 89 110 → 89
31 387 → 34 389 → 34 387 → 396 389 → 396
255 1601–2753 → 61–116 1609–1756 → 62–118 1598–2984 → 1801–2169 2433–2994 → 1705–3165
4095 29171–47326 → 804–1561 31560–47341 → 826–1551 25727–26765 → 49913–49946 25046–47333 → 26998–27299

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 two tolower calls.

#14022 (romainhedouin) makes tolower itself read from the existing case tables. That cuts the cost of the positions that still fold. It changes LocaleSupport.cpp only, so the two PRs combine and merge in either order.

Order with other open PRs

  • #17326, #17336, #17345 and #17354 (mine) touch other functions and other places in libc/CMakeLists.txt. All five branches merge with each other cleanly (git merge-tree).
  • For every PR listed by pr_overlap, git merge-tree shows the same conflicting files against this branch as against main.

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:5214073675 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17394). Originally posted by [rocklouis055](https://github.com/rocklouis055) on 2026-11-08T18:47:54Z. --- ### What `strcasecmp` and `strncasecmp` called `tolower` on both bytes at every position. Each `tolower` call goes through `ConvertCharacter`, which validates the value and looks up the classic `ctype<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 `tolower` runs 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_strings` and `guest_fnmatch` pass. The new `guest_case_compare_random` test runs 100000 random pairs against a reference that folds `A`–`Z` like the classic locale: - lengths 0–80, using letters, the bytes around `A`–`Z` and `a`–`z`, `0x7f`, and bytes `0x80` and above; - the second string copies the first, then changes some bytes to random values or flips their case; - an early terminator in either string; - limits from 0 up to past the terminator, plus `SIZE_MAX`; - both argument orders. The test also checks that `tolower` matches that fold for all 256 byte values, so the reference stays honest. Mutation check. Each change below makes the test fail: - not stopping at a shared terminator, in either function; - returning non-zero for equal strings. These still pass, as expected: - sending some equal bytes through `tolower` anyway, which gives the same result; - dropping the `*s1 == 0` check 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`, at `964eb803d6` against main `bfdb58119e`. 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. | bytes | strcasecmp same | strncasecmp same | strcasecmp case | strncasecmp case | |---|---|---|---|---| | 7 | 109 → 3.4 | 110 → 3.7 | 109 → 89 | 110 → 89 | | 31 | 387 → 34 | 389 → 34 | 387 → 396 | 389 → 396 | | 255 | 1601–2753 → 61–116 | 1609–1756 → 62–118 | 1598–2984 → 1801–2169 | 2433–2994 → 1705–3165 | | 4095 | 29171–47326 → 804–1561 | 31560–47341 → 826–1551 | 25727–26765 → 49913–49946 | 25046–47333 → 26998–27299 | 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 two `tolower` calls. #14022 (romainhedouin) makes `tolower` itself read from the existing case tables. That cuts the cost of the positions that still fold. It changes `LocaleSupport.cpp` only, so the two PRs combine and merge in either order. ### Order with other open PRs - #17326, #17336, #17345 and #17354 (mine) touch other functions and other places in `libc/CMakeLists.txt`. All five branches merge with each other cleanly (`git merge-tree`). - For every PR listed by `pr_overlap`, `git merge-tree` shows the same conflicting files against this branch as against `main`. ### 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:39:15 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Windows run of head 964eb803d6, merged onto main 095ff72367. 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_random with --repeat until-fail:5: 5 / 5 pass.
  • With main's Strings.cpp and 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 tolower values, and the *s1 == 0 check inside it returns 0 at the shared terminator, as the old loop did. I didn't measure the speed.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5819365245 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17394#pullrequestreview-5819365245). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-08T19:39:15Z. **Review:** COMMENTED --- Windows run of head `964eb803d6`, merged onto `main` `095ff72367`. 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_random` with `--repeat until-fail:5`: 5 / 5 pass. - With `main`'s `Strings.cpp` and 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 `tolower` values, and the `*s1 == 0` check inside it returns 0 at the shared terminator, as the old loop did. 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!17394
No description provided.