Bug #81243 [Asn->Csd]: Too much memory is allocated for the return value of preg_replace

From: Date: Mon, 12 Jul 2021 16:39:34 +0000
Subject: Bug #81243 [Asn->Csd]: Too much memory is allocated for the return value of preg_replace
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-234986@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81243&edit=1 ID: 81243 Updated by: git@php.net Reported by: abecker at mailbox dot org Summary: Too much memory is allocated for the return value of preg_replace -Status: Assigned +Status: Closed Type: Bug Package: PCRE related Operating System: Linux PHP Version: 8.0.8 Assigned To: cmb Block user comment: N Private report: N New Comment: Automatic comment on behalf of cmb69 Revision: https://github.com/php/php-src/commit/a6b43086e6799eedf02d36ccea103e2b10c1005a Log: Fix #81243: Too much memory is allocated for preg_replace() Previous Comments: ------------------------------------------------------------------------ [2021-07-12 11:25:23] cmb@php.net The following pull request has been associated: Patch Name: Fix #81243: Too much memory is allocated for preg_replace() On GitHub: https://github.com/php/php-src/pull/7231 Patch: https://github.com/php/php-src/pull/7231.patch ------------------------------------------------------------------------ [2021-07-11 08:14:54] abecker at mailbox dot org 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 this bug report at https://bugs.php.net/bug.php?id=81243&edit=1

« previous php.bugs (#234986) next »