Bug #74933 [ReO->Csd]: MBstring functions are much slower when called with encoding parameter
| From: | nikic@php.net | Date: | Sun, 23 Jul 2017 10:26:15 +0000 |
| Subject: | Bug #74933 [ReO->Csd]: MBstring functions are much slower when called with encoding parameter | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-210227@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=74933&edit=1
ID: 74933
Updated by: nikic@php.net
Reported by: reinir dot puradinata at gmail dot com
Summary: MBstring functions are much slower when called with
encoding parameter
-Status: Re-Opened
+Status: Closed
Type: Bug
Package: mbstring related
Operating System: Windows
PHP Version: 7.1.7
Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
This is fixed in master with the introduction of an encoding cache, which avoids the expensive
lookup if it's the same as the encoding that was used last, which should be the norm. Yet to be
seen if these changes will be in PHP 7.2 as well.
Previous Comments:
------------------------------------------------------------------------
[2017-07-18 08:15:42] nikic@php.net
There are better ways to look up an encoding than doing an O(n) search: https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/mbstring/libmbfl/mbfl/mbfl_encoding.c#L224
This should be turned into a lookup table. Additionally caching the last used encoding probably
makes sense.
------------------------------------------------------------------------
[2017-07-18 07:53:24] jhdxr@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
Whenever you passed in a string, php has to converted it into internal enum type, and it's why
the second case is so slow in your test file.
------------------------------------------------------------------------
[2017-07-17 01:08:57] reinir dot puradinata at gmail dot com
Description:
------------
Several MBstring functions are much slower when called with encoding parameter than without encoding
parameter.
Functions that exhibit this behavior are mb_strlen, mb_substr, mb_strpos, mb_strrpos.
For more information see:
https://stackoverflow.com/questions/45028018/a/45107408
Test script:
---------------
mb_internal_encoding("UTF-8");
echo "without encoding parameter:\n";
$a = microtime(true);
for($i=0; $i<100000; $i++){
$n = mb_strlen("ããããã");
}
$a = microtime(true)-$a;
echo "- mb_strlen: ".number_format($a*1000)." ms\n";
echo "\nwith encoding parameter:\n";
$b = microtime(true);
for($i=0; $i<100000; $i++){
$n = mb_strlen("ããããã", "UTF-8");
}
$b = microtime(true)-$b;
echo "- mb_strlen: ".number_format($b*1000)." ms
(".number_format(($b-$a)*100/$a)."% slower)\n";
Expected result:
----------------
Because the character encoding is UTF-8 in both cases, I expected to see similar performance.
In other words, 0% slower or close to it.
Actual result:
--------------
When called with encoding parameter, performance drops greatly.
Example output from the test script:
without encoding parameter:
- mb_strlen: 14 ms
with encoding parameter:
- mb_strlen: 585 ms (4,186% slower)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74933&edit=1