Last modified: 2012-01-30 13:38:33 UTC

Wikimedia Bugzilla is closed!

Wikimedia migrated from Bugzilla to Phabricator. Bug reports are handled in Wikimedia Phabricator.
This static website is read-only and for historical purposes. It is not possible to log in and except for displaying bug reports and their history, links might be broken. See T28605, the corresponding Phabricator task for complete and up-to-date bug report information.
Bug 26605 - Use strtr instead of str_replace when possible
Use strtr instead of str_replace when possible
Status: NEW
Product: MediaWiki
Classification: Unclassified
General/Unknown (Other open bugs)
unspecified
All All
: Normal enhancement (vote)
: ---
Assigned To: Nobody - You can work on this!
http://php.net/manual/en/function.str...
:
Depends on:
Blocks: code_quality
  Show dependency treegraph
 
Reported: 2011-01-06 12:17 UTC by The Evil IP address
Modified: 2012-01-30 13:38 UTC (History)
3 users (show)

See Also:
Web browser: ---
Mobile Platform: ---
Assignee Huggle Beta Tester: ---


Attachments

Description The Evil IP address 2011-01-06 12:17:54 UTC
The PHP function strtr is like 4 times faster than str_replace and would be pretty useful to use in MediaWiki in many places.

In the source code, I've noticed many replacements of underscores to spaces and vice versa, often in the form str_replace("_", " ", $foo), whereas strtr($foo, "_", " ") is much faster.
Comment 1 Sam Reed (reedy) 2011-01-06 16:57:01 UTC
Is there any proof of this performance gain? (Benchmarks or alike)

Is it in all cases, or only some?
Comment 2 Sam Reed (reedy) 2011-01-06 21:03:02 UTC
reedy@ubuntu64-esxi:~/mediawiki/trunk/phase3/maintenance/benchmarks$ php bench_strtr_str_replace.php 
100 times: function bench_strtr_str_replace->benchstrtr() :
     0.22ms (  0.00ms each)
100 times: function bench_strtr_str_replace->benchstr_replace() :
     0.24ms (  0.00ms each)


I'm not sure if that's worthwhile enough a gain...?

r79755
Comment 3 Brion Vibber 2011-01-08 01:19:48 UTC
I did some tweaks to the benchmark in r79854; results on my test box (MacBook Pro, 2.4GHz Core 2 Duo, Mac OS X 10.6.6, 64-bit PHP 5.3.4 built via MacPorts) with 10,000 reps:

$ php bench_strtr_str_replace.php --count=10000
10000 times: function bench_strtr_str_replace->benchstrtr() :
19.67ms ( 0.00ms each)
10000 times: function bench_strtr_str_replace->benchstr_replace() :
22.05ms ( 0.00ms each)
10000 times: function bench_strtr_str_replace->benchstrtr_indirect() :
22.53ms ( 0.00ms each)
10000 times: function bench_strtr_str_replace->benchstr_replace_indirect() :
26.29ms ( 0.00ms each)


It takes at least 3000 reps to make a 1ms difference in execution time, so it's not likely to make a big performance difference to make this change.

But -- as happy coincidence, the difference in execution time is about the same as the difference from adding a level of function-call indirection. This means that replacing the str_replace() calls with calls to a nice global function, and having it use strtr, would have almost no performance impact, but would allow for changing the underscore/space normalization rules globally MUCH more easily.

So, if you *are* inclined to go changing all those bits, I'd recommend encapsulating them in a nice function and concentrating on how it makes maintenance and customization easier.
Comment 4 Antoine "hashar" Musso (WMF) 2012-01-30 13:38:33 UTC
I got different result where indirect calls are both slower:

$ php bench_strtr_str_replace.php --count=10000
10000 times: function bench_strtr_str_replace->benchstrtr() :
    57.79ms (  0.01ms each)
10000 times: function bench_strtr_str_replace->benchstr_replace() :
    58.82ms (  0.01ms each)
10000 times: function bench_strtr_str_replace->benchstrtr_indirect() :
    73.34ms (  0.01ms each)
10000 times: function bench_strtr_str_replace->benchstr_replace_indirect() :
    75.39ms (  0.01ms each)


On a Macbook Air ( 1.7 GHz Intel Core i5 ) with Mac OS X 10.7.2 PHP:
$ php -version
PHP 5.3.6 with Suhosin-Patch (cli) (built: Sep  8 2011 19:34:00) 
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
    with Xdebug v2.1.2, Copyright (c) 2002-2011, by Derick Rethans
$


A nice function will probably makes code nicer though.

Note You need to log in before you can comment on or make changes to this bug.


Navigation
Links