__builtin_expect on CPython's fastsearch skip test makes clang short-needle find 3-4x faster
In Clang builds of CPython (python-build-standalone, i.e. uv's Pythons), stringlib default_find's bloom-filter skip (if (i + 1 <= w && !STRINGLIB_BLOOM(mask, ss[i+1])) i = i + m;) is if-converted to bt+cmov, which makes short absent-needle str.find / in / count about 3x slower than GCC builds. Wrapping that condition in __builtin_expect(..., 1) at both sites (default_find and adaptive_find in Objects/stringlib/fastsearch.h) makes Clang 23 emit a branch. In a Clang 23 PGO build of the 3.14 branch, absent 2-4 char needles in 220 KB of ASCII go from 105-174 us to 26-51 us (0.25-0.31x), and count('the') goes to 0.61x. 12-char, 1-char and UCS2 cases are unchanged, and nothing got slower. Under gcc PGO most cases also improve (0.67-0.97x), but bytes.count(b'the'), replace and split came out 3-11% slower, possibly profile noise between separately trained builds. Gotcha: when rebuilding a CC=clang CPython tree whose clang isn't on PATH by default, rerun make with the same PATH. Otherwise make fails with Error 127 and you end up benchmarking the stale binary.
Follow-up on the gcc side. Most of the gcc PGO results for this hint are code placement. In 5 + 3 independently trained gcc 13 PGO+LTO builds of the same 3.14 tree, default_find is byte-identical within each variant, and only its start address mod 64 moves. Copying the exact bytes into a buffer and sweeping the offset from 0 to 63 changes count('the') by 1.40x (56-78 us). Under gcc the hint is robustly 0.67-0.80x only for absent or rarely matching str needles. For hit-heavy count('the')/('th') it is 0.69-1.07x depending on where the function lands. The clang result (0.25-0.31x) is far outside that range.
Independently reproduced the headline result. CPython 3.14.0 release tarball, clang 23.1.4 (LLVM release binaries),
configure CC=clang --enable-optimizations(PGO, no LTO; I checked the profile run, a 3.3 MB merged .profdata and -fprofile-instr-use in the second pass). One stock build, and one with __builtin_expect(..., 1) at the 4 bloom-skip sites (2 in default_find, 2 in adaptive_find). 220 KB random-word ASCII text, min of 7x200, two runs each. Stock to hinted: find('zq') 158.5 to 39.8 us (0.25x), find('xyz') 119.1 to 29.6 (0.25x), 'wxyz' in text 94.7 to 23.9 (0.25x). Unchanged: 12-char absent 72.0/72.1, 1-char absent 1.9/1.9. For scale, Ubuntu's gcc-built 3.12.3 does find('zq') on the same text in 51.6 us, so stock clang PGO is ~3x behind gcc here, as you say. One difference: count('the') did NOT improve in my build (232 to 244 us, ~1.05x, stable across reruns), whereas you saw 0.61x. Given your gcc follow-up on placement, I'd treat the hit-heavy count number as build-dependent; the absent-needle effect is robust.