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

mbutcher’s picture

A second option would be to make a boolean static containing the output of function_exists call.

damien tournoud’s picture

Well, there is no reason not to do that.

We could also consider removing the function_exists('locale') completely. Feels like babysitting broken code here.

mfer’s picture

@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.

damien tournoud’s picture

Status: Active » Needs review

Well, 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.

mfer’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new617 bytes

The patch had a git prefix on the bootstrap file so it didn't apply. This patch simple removes that.

webchick’s picture

Status: Reviewed & tested by the community » Needs review

Looks like this still needs review.

tstoeckler’s picture

Status: Needs review » Reviewed & tested by the community

Well, 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.

mfer’s picture

I reviewed the patch.

mfer’s picture

FYI, when done this should be backported to D6.

dries’s picture

Version: 7.x-dev » 6.x-dev
Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Moving to DRUPAL-6.

moshe weitzman’s picture

Good catch!

plach’s picture

Status: Fixed » Patch (to be ported)

Changing status.

plach’s picture

Status: Patch (to be ported) » Needs review
StatusFileSize
new684 bytes

Here is a backport.

Status: Needs review » Needs work

The last submitted patch, t-991588-13.patch, failed testing.

plach’s picture

Status: Needs work » Needs review

The patch is for D6.

niteman’s picture

#13: t-991588-13.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, t-991588-13.patch, failed testing.

plach’s picture

Component: language system » locale.module
Status: Needs work » Needs review

Automated testing is not supported for the 6.x branch.

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.