[php-src] PR #12337: Add fast mb_strcut implementation for UTF-8
| From: | alexdowad | Date: | Sun, 01 Oct 2023 12:56:33 +0000 |
| Subject: | [php-src] PR #12337: Add fast mb_strcut implementation for UTF-8 | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-23758@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/12337
Author: alexdowad
The old implementation decodes the entire string to pick out the part which should be returned by
mb_strcut. This creates significant performance overhead. The new specialized
implementation of mb_strcut for UTF-8 usually only examines a few bytes around the
starting and ending cut points, meaning it generally runs in constant time.
For UTF-8 strings just a few bytes long, the new implementation is around 10% faster (according to
microbenchmarks which I ran locally). For strings around 10,000 bytes in length, it is 50-300x
faster. (Yes, that is 300x and not 300%.)
At the same time, I also added many more unit tests for mb_strcut. This will help to avoid
unintended behavior changes as the function undergoes further performance work.
The new implementation behaves identically to the old one on *valid* UTF-8 strings; a fuzzer was
used to help ensure this is the case. On invalid UTF-8 strings, there is a difference: the old
implementation would convert invalid UTF-8 byte sequences to error markers
('?'), but the new implementation just cuts a subsequence of bytes out of the
source string without performing any conversion on it.
Any comments will be much appreciated! @Girgias @cmb69 @youkidearitai @kamil-tekiela