2

For some reason, simply adding the PCRE_UTF8 modifier to the regex input for preg_match() roughly decuples (x10) the execution time even if no multibyte characters are used. I can't figure out why this is the case and how to best reduce the time. The script used to test is:

$s = microtime(true);
for ($i = 0; $i < 1000; $i++) {
    preg_match('/ /u', str_repeat(' ', 50000), $match);
}
$e = microtime(true);
echo "u Modifier:\t".(($e-$s)/$i)."\n";
$s = microtime(true);
for ($i = 0; $i < 1000; $i++) {
    preg_match('/ /', str_repeat(' ', 50000), $match);
}
$e = microtime(true);
echo "No Modifier:\t".(($e-$s)/$i)."\n";

Try it online here.

The results were:

u Modifier: 2.5037050247192E-5
No Modifier:    2.4969577789307E-6

I tried to see if this was a known bug online, but supposedly, it is not a problem with PHP.

What is this caused by and what would be the best method to execute the match1 quicker?


1"the match" refers to any match. The example used is simply a minimal example and could obviously be matched in much better ways.

4
  • Matching utf-8 characters is slower than bytes.
    – zerkms
    Mar 11, 2015 at 23:45
  • I figured, but a space is not multibyte, so theoretically, shouldn't it be roughly the same time? I just don't understand why there is such a significant hit even in this case.
    – Anonymous
    Mar 11, 2015 at 23:46
  • 1
    How would PCRE know it's only spaces there? You stated that it's UTF-8 - it treats it as UTF-8.
    – zerkms
    Mar 11, 2015 at 23:47
  • Fair enough. It just seemed odd that it would take 10x as long.
    – Anonymous
    Mar 11, 2015 at 23:49

1 Answer 1

3

PCRE checks for UTF validity before any other processing takes place.

From the PCRE docs:

When the PCRE2_UTF option is set, the strings passed as patterns and subjects are (by default) checked for validity on entry to the relevant functions. If an invalid UTF string is passed, an negative error code is returned. The code unit offset to the offending character can be extracted from the match data block by calling pcre2_get_startchar(), which is used for this purpose after a UTF error.

...

The entire string is checked before any other processing takes place. In addition to checking the format of the string, there is a check to ensure that all code points lie in the range U+0 to U+10FFFF, excluding the surrogate area. The so-called "non-character" code points are not excluded because Unicode corrigendum #9 makes it clear that they should not be.

...

In some situations, you may already know that your strings are valid, and therefore want to skip these checks in order to improve performance, for example in the case of a long subject string that is being scanned repeatedly. If you set the PCRE2_NO_UTF_CHECK option at compile time or at match time, PCRE2 assumes that the pattern or subject it is given (respectively) contains only valid UTF code unit sequences.

(Note: These docs are quoted from PCRE2, but the PCRE behavior is the same)

Unfortunately, I don't think there's a way to set the PCRE2_NO_UTF_CHECK option from PHP.

Anyway, your benchmark should go over much more iterations to be meaningful. You should measure the time over several seconds worth of computing time to get a better sense of the impact this feature has.

1
  • That sounds about right. Checking the entire string seems like exactly the reason why there is such a drastic difference.
    – Anonymous
    Mar 11, 2015 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.