Repository navigation
Conversation
When both the input and output encodings are UTF-8, php_unicode_convert_case() now converts the leading ASCII characters 16 bytes at a time (SSE2 or NEON, with a scalar fallback), and stops at the first non-ASCII byte. The general loop handles the rest of the string. Title case is left to the general loop. There is no separate scan of the whole string: the fast loop does the conversion as it goes, and a non-ASCII string costs at most 63 bytes of work that the general loop redoes. The general loop converts 64 codepoints at a time, and the special casing of the final Greek sigma looks back into the previous block. So the handover happens at a multiple of 64 bytes, with the last 64 converted characters already in converted_buf, and the output is the same as before. This speeds up mb_strtolower(), mb_strtoupper(), mb_convert_case() and the case-insensitive searches (mb_stripos() and co.) on ASCII and mostly-ASCII UTF-8 strings.
|
Very nice work, @lemire. Thank you! We have been wanting something like this for a long time. (@mvorisek, do you remember that quite some time ago, I respectfully declined one of your patches which aimed to speed up case-conversion via SIMD? This new patch is exactly the kind of thing I was hoping to see at that time.) Now, about the use of In Your ARM64/NEON branch may be slightly more efficient than the code we would get from your SSE2 branch using the shims. So if you want to retain that small bit of efficiency, you could put the ARM64/NEON branch first, so it takes priority over the XSSE2 branch. Or, if performance is too close either way to even care about the difference, you could delete your ARM64/NEON branch and just rely on XSSE2. Any other thoughts? |
|
Thanks for the review. I think it is a nice little change worth doing.
Is this true? On Windows, PHP's build system seems to define
I tried it. On my macBook, it is measurably slower. |
|
@lemire Thanks very much for correcting my misconception. In that case, no objection to merging the patch as written. |
youkidearitai
left a comment
There was a problem hiding this comment.
@alexdowad Nothing comment. LGTM
|
Merged into Personally, I'm a fan of your work and hope to see more interesting research from you and your colleagues in the future. |
|
@alexdowad Thanks for the good words. |
Follow-up to the discussion in #25473 about using SIMD in mbstring's case conversion.
When both the input and output encodings are UTF-8,
php_unicode_convert_case()now converts the leading ASCII characters 16 bytes at a time (SSE2 on x86-64, NEON on arm64, with a scalar loop elsewhere and for the tail). We use the fact that all x64 processors support SSE2. It stops at the first non-ASCII byte, and the existing loop handles the rest of the string.The string is not scanned first: the fast loop converts as it goes. On a non-ASCII string, the existing loop redoes at most 63 bytes of that work.
Final sigma. The existing loop converts 64 codepoints at a time, and the special casing of the final Greek sigma looks back into the previous block's output (
converted_buf). So the fast loop hands over at a multiple of 64 bytes, with the last 64 converted characters copied intoconverted_buf. The output is therefore exactly what the existing loop alone produces, including for long runs of case-ignorable characters.Scope. Upper, lower and fold, both full and simple. Title case still goes through the existing loop. This covers
mb_strtolower(),mb_strtoupper(),mb_convert_case()and the case-insensitive searches (mb_stripos(),mb_stristr(), ...) when the string is UTF-8.Benchmarks
Best of 5 runs, ns per call, UTF-8 input.
éat 50%, 1 KB, loweréfirst, 1 KB, upper"Café", upperThe worst case is a short string with a non-ASCII character near the start, such as
"Café": about 2–3 ns slower, from the function call and the discarded prefix work. The fast loop is kept out of line (zend_never_inline). Inlining it changed GCC's code generation for the existing loop and slowed non-ASCII uppercase by about 15% on x86.Testing
mb_convert_case_ascii_prefix.phpt. It puts the first non-ASCII character at every offset from 0 to 200, for every mode, including final-sigma cases around the 64-byte handover.mb_stripos(). About 100,000 strings, identical output.ext/mbstringtests pass on arm64 (macOS) and x86-64 (Linux).