php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47657 str_replace() should not call replace-functions when needle missing
Submitted: 2009-03-14 17:41 UTC Modified: 2009-03-14 19:26 UTC
From: h dot reindl at thelounge dot net Assigned:
Status: Not a bug Package: Performance problem
PHP Version: 5.2.9 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: h dot reindl at thelounge dot net
New email:
PHP Version: OS:

 

 [2009-03-14 17:41 UTC] h dot reindl at thelounge dot net
Description:
------------
If you use str_replace and the needle is not in the string there should be no call to the replacement-function.

This can produce much overhead if the function is complex
At the moment you must manually check if you call the replace with strpos - I think in many cases this will not happen and there is running much overhead-code without any need in existing applications

Even if you know the problem it needs ugly php-code for get best performance - If the php-core makes this check all exististing applications can get better performance

Reproduce code:
---------------
<?php
 $str = 'Test [f1] Replace';
 $str = str_replace('[f1]', f1(), $str);
 $str = str_replace('[f2]', f2(), $str);

 function f1()
 {
  echo 'F1<br />';
 }

 function f2()
 {
  echo 'F2<br />';
 }
?>

Expected result:
----------------
Only f1() should be called

Actual result:
--------------
f1() AND f2() are called

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-03-14 17:53 UTC] jani@php.net
str_replace() does not accept functions as the parameters like you 
expect it to so there is no bug here either.
 [2009-03-14 18:03 UTC] h dot reindl at thelounge dot net
And you think it is best practice every possible optimzing
that is suggestet by users throw against a wall?

Sorry but for me it seems since a long time that
php-developers are not interested in any help
and thinking about making things better from
the community
 [2009-03-14 19:18 UTC] mattwil@php.net
str_replace() is not "calling the replacement function." Your f1() and f2() functions are called BEFORE str_replace() is run. Same in any language, so there's certainly nothing str_replace() can do about it.

Once called, str_replace() doesn't do anything unnecessary when the needle is missing. In other words, it's doing the same work as strpos(), with the exception that it's also making a copy of the input string to return, which can't be avoided either -- although you may be able to measure the small amount of overhead (it's propotional to the input length).
 [2009-03-14 19:26 UTC] h dot reindl at thelounge dot net
Thank for your interal details
I understand this well and its ok 
I do now know about the internals of php and thought i suggest this in hope its possible a small change to make some things faster

The arrogant replies from jani are my really problem
Because he always sets everything as "bogus" without thinking and without explaining which gives me the feeling "i do not need input from stupid users"

It would be a good idea if he let open reports and wait if someone else gives a lesser arrogant answer instead of making really stupid things like http://bugs.php.net/bug.php?id=42077 and spit the reporter in the face with it!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC