#27176 [Asn->Csd]: str_replace tries to use obscene amounts of ram

From: Date: Mon, 23 Feb 2004 20:00:10 +0000
Subject: #27176 [Asn->Csd]: str_replace tries to use obscene amounts of ram
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-55403@lists.php.net to get a copy of this message
ID: 27176 Updated by: pollita@php.net Reported By: gaz at fission dot org dot uk -Status: Assigned +Status: Closed Bug Type: Performance problem Operating System: Slackware 9.1 Linux 2.6.1 kernel PHP Version: 5.0.0b3 (beta3) Assigned To: pollita New Comment: This bug has been fixed in CVS. Snapshots of the sources are packaged every three hours; this change will be in the next snapshot. You can grab the snapshot at http://snaps.php.net/. Thank you for the report, and for helping us make PHP better. Previous Comments: ------------------------------------------------------------------------ [2004-02-23 14:34:55] derick@php.net <?php ini_set("memory_limit", "12m"); $replacement = str_repeat("x", 12444); $string = str_repeat("x", 9432); $key = "{BLURPS}"; $string = str_replace($key, $replacement, $string); ?> ------------------------------------------------------------------------ [2004-02-13 10:15:05] sniper@php.net No feedback was provided. The bug is being suspended because we assume that you are no longer experiencing the problem. If this is not the case and you are able to provide the information that was requested earlier, please do so and change the status of the bug back to "Open". Thank you. preg_replace() might be good alternative too.. ------------------------------------------------------------------------ [2004-02-07 14:01:57] sniper@php.net Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with <?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If possible, make the script source available online and provide an URL to it here. Try avoid embedding huge scripts into the report. ------------------------------------------------------------------------ [2004-02-07 06:29:06] gaz at fission dot org dot uk Description: ------------ When str_replace is called with the 'mixed replace' value set to a string with a large (eg, 16,000) characters in it, it suddenly tries to asign an absolutly obscene amount of memory. Reproduce code: --------------- /* In the code that initially showed this up, $somefile was a 20kb html file, and $replacefile was a 6kb html file */ $input_text = file_get_contents($somefile); $replace = file_get_contents($replacefile); $match = "__RECENT__"; $output = str_replace($match,$replce,$input_text); Actual result: -------------- The actual code tries to allocate about 34MB of memory to do this str_replace. I think it keeps trying to reorder the char[] array, and so a large replacement string ties it up for a while. Changing to use explode() and foreach() tends to do the same thing but in no time, and without the huge memory tie-up. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=27176&edit=1

« previous php.bugs (#55403) next »