In the t() function, translation can be sped up by making a trivial change to the order of evaluation of one `if` statement.
Line 1429 of bootstrap.inc has this:
elseif (function_exists('locale') && $options['langcode'] != 'en') {
String comparison is almost twice as fast as a function_exists() call. So a trivially easy change would reduce the time taken by t() for any case where the language is English, which I'd venture to guess is a substantial portion of the Drupal population.
I noticed that on the main site I am working on, t() is called over 6,000 times on an only moderately complex page. Making the switch makes a very real performance difference.
Example benchmarking script (`t_test.php`):
$langcode = "en";
function locale(){}
$start = microtime(TRUE);
for ($i = 0; $i < 600000; ++$i) $langcode != "en" && function_exists("locale");
$end = microtime(TRUE);
printf("strcmp first: %0.4f\n", $end - $start);
$start = microtime(TRUE);
for ($i = 0; $i < 600000; ++$i) function_exists("locale") && $langcode != "en";
$end = microtime(TRUE);
printf("function_exists first: %0.4f\n", $end - $start);
Sample output:
$ php ./t_test.php
strcmp first: 0.2000
function_exists first: 0.9985
Environment:
$ uname -a
Linux dev-mbutcher 2.6.26-2-686 #1 SMP Mon Jun 21 05:58:44 UTC 2010 i686 GNU/Linux
$ php --version
PHP 5.2.6-1+lenny9 with Suhosin-Patch 0.9.6.2 (cli) (built: Aug 4 2010 03:25:57)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
with eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator
with Xdebug v2.0.3, Copyright (c) 2002-2007, by Derick Rethans
Comments
Comment #1
mbutcher commentedA second option would be to make a boolean static containing the output of function_exists call.
Comment #2
damien tournoud commentedWell, there is no reason not to do that.
We could also consider removing the
function_exists('locale')completely. Feels like babysitting broken code here.Comment #3
mfer commented@Damien the function_exists bit is for the case the locale module is installed (or another module implementing locale for that). Do you have another way to optionally handle this?
In any case, for D6/7 the flip should do. A major change should happen in D8 and have its own issue if someone wants to champion it.
Comment #4
damien tournoud commentedWell, if you pass and explicit language to t() while the locale module is not enabled, it's really your problem :) That conditions feels like babysitting broken code.
That said, I would not be surprised if the world felt apart if we do that.
Could we make that as a proper patch (you need --no-prefix for git)? This one will fail the test bot.
Comment #5
mfer commentedThe patch had a git prefix on the bootstrap file so it didn't apply. This patch simple removes that.
Comment #6
webchickLooks like this still needs review.
Comment #7
tstoecklerWell, it's really a no-brainer.
No matter, whether the performance stats above are actually reproducible, we should do this.
It makes the code a bit faster with no tradeoff whatsoever.
Comment #8
mfer commentedI reviewed the patch.
Comment #9
mfer commentedFYI, when done this should be backported to D6.
Comment #10
dries commentedCommitted to CVS HEAD. Moving to DRUPAL-6.
Comment #11
moshe weitzman commentedGood catch!
Comment #12
plachChanging status.
Comment #13
plachHere is a backport.
Comment #15
plachThe patch is for D6.
Comment #16
niteman commented#13: t-991588-13.patch queued for re-testing.
Comment #18
plachAutomated testing is not supported for the 6.x branch.