Bug #81243 [NEW]: Too much memory is allocated for the return value of preg_replace

From: Date: Sun, 11 Jul 2021 08:14:54 +0000
Subject: Bug #81243 [NEW]: Too much memory is allocated for the return value of preg_replace
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-234953@lists.php.net to get a copy of this message
From: abecker at mailbox dot org Operating system: Linux PHP version: 8.0.8 Package: PCRE related Bug Type: Bug Bug description:Too much memory is allocated for the return value of preg_replace Description: ------------ The string returned by preg_replace uses too much memory - multiples of the actual string size in bytes. For big strings, this can be a significant performance issue. mb_ereg_replace does not have this problem. When I copy the returned string using some of the PHP string functions, the storage size is reduced in case the memory for the string is reallocated. Test script: --------------- $base_memory = memory_get_usage(); $test_string = str_repeat('Eins zwei drei', 2000); var_dump(memory_get_usage() - $base_memory); $replaced = preg_replace('/\s/', '-', $test_string); var_dump(memory_get_usage() - $base_memory); $replaced = str_repeat($replaced, 1); #$replaced = str_replace('e', 'e', $replaced); #$replaced = $replaced[0] . substr($replaced, 1); var_dump(memory_get_usage() - $base_memory); Expected result: ---------------- int(28672) int(57728) int(57728) Actual result: -------------- int(28672) int(106880) int(57728) -- Edit bug report at https://bugs.php.net/bug.php?id=81243&edit=1 -- Fix committed: https://bugs.php.net/fix.php?id=81243&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=81243&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=81243&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=81243&r=needscript Try newer version: https://bugs.php.net/fix.php?id=81243&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=81243&r=support Expected behavior: https://bugs.php.net/fix.php?id=81243&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=81243&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=81243&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=81243&r=globals PHP version support discontinued: https://bugs.php.net/fix.php?id=81243&r=phptooold Daylight Savings: https://bugs.php.net/fix.php?id=81243&r=dst IIS Stability: https://bugs.php.net/fix.php?id=81243&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=81243&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=81243&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=81243&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=81243&r=mysqlcfg

« previous php.bugs (#234953) next »