From: frode at coretrek dot com
Operating system: Debian GNU/Linux 4.0r3
PHP version: 5.2.6RC1
PHP Bug Type: PCRE related
Bug description: preg_replace with /u (unicode/UTF-8) and many hits = very bad
performance
Description:
------------
The "/u" modifier with preg_replace() yields extraordinarily poor
performance when there are a lot of matches.
I tried to run php through valgrind/callgrind and kcachegrind, and it
seems that the time is mostly spent in "php__pcre_valid_utf8()", Perhaps
this method is (unnecessarily) called over and over again, once for each
substring match/replace?
This happens at least in both PHP 5.2.5 and PHP 5.2.6RC1
Reproduce code:
---------------
<?php
$goodstring = str_repeat('Test', 50000);
$badstring = str_repeat('Test ', 50000);
$t = microtime(true);
preg_replace('/\\s+/', ' ', $goodstring);
echo "matches: NO unicode: NO ".(microtime(true)-$t)." sec\n";
$t = microtime(true);
preg_replace('/\\s+/u', ' ', $goodstring);
echo "matches: NO unicode: YES ".(microtime(true)-$t)." sec\n";
$t = microtime(true);
preg_replace('/\\s+/', ' ', $badstring);
echo "matches: YES unicode: NO ".(microtime(true)-$t)." sec\n";
$t = microtime(true);
preg_replace('/\\s+/u', ' ', $badstring);
echo "matches: YES unicode: YES ".(microtime(true)-$t)." sec\n";
Expected result:
----------------
Similar performance for all runs (less than 1 second)
Actual result:
--------------
matches: NO unicode: NO 0.020231962204 sec
matches: NO unicode: YES 0.0206818580627 sec
matches: YES unicode: NO 0.0361981391907 sec
matches: YES unicode: YES 27.6555769444 sec
--
Edit bug report at http://bugs.php.net/?id=44336&edit=1
--
Try a CVS snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=44336&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=44336&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=44336&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=44336&r=fixedcvs
Fixed in release:
http://bugs.php.net/fix.php?id=44336&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=44336&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=44336&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=44336&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=44336&r=support
Expected behavior: http://bugs.php.net/fix.php?id=44336&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=44336&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=44336&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=44336&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=44336&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=44336&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=44336&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=44336&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=44336&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=44336&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=44336&r=mysqlcfg