Bug #72685 [NEW]: No support for PCRE2_NO_UTF_CHECK flag
| From: | ju1ius at laposte dot net | Date: | Wed, 27 Jul 2016 02:13:07 +0000 |
| Subject: | Bug #72685 [NEW]: No support for PCRE2_NO_UTF_CHECK flag | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-202623@lists.php.net to get a copy of this message | ||
From: ju1ius at laposte dot net
Operating system: Debian Sid
PHP version: Irrelevant
Package: PCRE related
Bug Type: Bug
Bug description:No support for PCRE2_NO_UTF_CHECK flag
Description:
------------
When matching in UTF8 mode ('u' flag), the
preg_match function checks
the validity of the entire input string on every call.
PCRE2 has a flag to disable this behavior: PCRE2_NO_UTF_CHECK, but PHP
does not expose it.
This means that when matching a lot of times against the same
(potentially long) input string (for example for lexical analysis), a
lot of unnecessary computations are performed, leading to catastrophic
performance (O(n²) instead of expected O(n)).
Test script:
---------------
// see online at https://3v4l.org/Duv7g
$input_size = 1e4;
$str = str_repeat('a', $input_size);
$start = microtime(true);
$pos = 0;
while(preg_match('/\G\w/', $str, $m, 0, $pos)) ++$pos;
$end = microtime(true);
echo '>>> NO u flag: ', number_format(($end - $start)*1000, 6), 'ms',
PHP_EOL;
$str = str_repeat('e', $input_size);
$start = microtime(true);
$pos = 0;
while(preg_match('/\G\w/u', $str, $m, 0, $pos)) ++$pos;
$end = microtime(true);
echo '>>> WITH u flag: ', number_format(($end - $start)*1000, 6), 'ms',
PHP_EOL;
Expected result:
----------------
I expect the two loops to take roughly the same amount of time, with the
second one being slower by a very short margin.
The performance should be O(n) for the two.
Actual result:
--------------
With $input_size === 1e4:
>>> NO u flag: 9.632111ms
>>> WITH u flag: 109.670877ms
With $input_size === 1e5:
>>> NO u flag: 96.043110ms
>>> WITH u flag: 10,151.215076ms
With $input_size === 2*1e5:
>>> NO u flag: 188.354015ms
>>> WITH u flag: 40,387.295008ms
Looks like we have the expected O(n) in first case,
and O(n²) with the /u flag.
--
Edit bug report at https://bugs.php.net/bug.php?id=72685&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=72685&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=72685&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=72685&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=72685&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=72685&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=72685&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=72685&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=72685&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=72685&r=support
Expected behavior: https://bugs.php.net/fix.php?id=72685&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=72685&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=72685&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=72685&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72685&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=72685&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=72685&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=72685&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=72685&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=72685&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=72685&r=mysqlcfg