Repository navigation
Conversation
Use the "lookup" algorithm of Keiser and Lemire, "Validating UTF-8 In Less Than One Instruction Per Byte", Software: Practice and Experience 51 (5), 2021, as implemented in the simdutf library, for mb_check_encoding() and the other functions which validate UTF-8: - AVX2: full lookup algorithm (the previous version only adopted part of it), errors accumulated over blocks, 64-byte ASCII fast path, and no copying for the tail of the string or for short strings. - AVX-512 (new): selected at runtime when available, on x86-64. - ARM64: NEON implementation of the same algorithm, replacing the emulated SSE2 code. Long strings are validated about twice as fast (UTF-8 text) to three times as fast (ASCII); short strings are not slower.
The new test called preg_match('//u') before mb_check_encoding().
That sets IS_STR_VALID_UTF8, and str_repeat($s, 1) returns the same
string, so valid input never reached the new kernels. Copy the
bytes with pack() first, and cover strings longer than 2048 bytes.
The AVX-512 alignment prologue loaded context through a pointer
before the zend_string. Read the three bytes from inside the string.
OR the NEON continuation checks, matching AVX2.
Record the speedup in NEWS.
Author
|
Opened by mistake. Closing. |
_mm512_setr_epi8 does not exist in GCC or clang. Go back to the _mm512_setr_epi64 constant, which has the same bytes.
Author
|
reopening, as it is now ready for review |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR updates the SIMD UTF-8 validation behind
mb_check_encoding(), and behind the functions that validate UTF-8 before taking a fast path (mb_strlen(),mb_substr(), ...). It uses the "lookup" algorithm fromas implemented in simdutf. The simdutf library is used by mainstream browsers and major JavaScript runtimes. It is thus quite solid (used in production for many years).
Changes
zend_stringheader before the content, which the old code already did, and those bytes are cleared.ext/standard/base64.c. For long strings it checks one unaligned block, then continues from the next 64-byte boundary. The three bytes of context for that boundary are read from inside the string.vqtbl1q_u8) replaces the SSE2 code emulated throughzend_simd.h.Performance
mb_check_encoding($s, 'UTF-8'), ns per call; the string is modified before each call so the cached validity flag is cleared.Intel Xeon Gold 6338 (Ice Lake), GCC 16:
Apple M4 Max, Apple clang:
None of the measured sizes (16 B to 1 MiB, ASCII, Latin, CJK, emoji) got slower.
Testing
ext/mbstring/tests/mb_check_encoding_utf8_blocks.phpt. It puts valid, invalid and truncated sequences at every offset of strings up to 140 bytes, and compares against PCRE's UTF-8 validation. The string passed tomb_check_encoding()is apack()copy, so the UTF-8 flag set bypreg_match('//u')is clear and the SIMD path actually runs. It also checks strings of 2048 bytes and longer, which is when the AVX-512 kernel aligns its loads.preg_match('//u', ...).#24362 also changes the AVX2 dispatch code in
mbstring.c, for i386 clang; whichever lands second will need a small rebase.