Use lowercase constants

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
You are viewing a wiki page. You are welcome to join the group and then edit it. Be bold!

Status: not approved

Synopsis

true is faster than TRUE, also applies to FALSE and NULL

Description

This is because when looking for constants PHP does a hash lookup for the name as is. And since names are always stored lowercased, by using them you avoid 2 hash lookups. (source)

Benchmarks

  • After replacing all instances of TRUE, FALSE, NULL with true, false, null the average page execution time decreased from approx. 335ms to 325ms (-10ms).
    • Drupal 5.1; Windows 2003; Apache 2; PHP 4.4.4
    • If this is reproducable, it's encouraging. But I've been unable to reproduce this. My KCacheGrind results are the same regardless of upper or lower case constants. But in the hopes that it's just me, and these are valid, I'd like to see: (a) submit an issue with a patch file for 6.x (b) use xdebug to create KCacheGrind profile files for before and after, then use KCacheGrind to generate the output results, include your results with the issue patch - Doug Green

if you do this and I can reproduce your results, I'll support and lobby for the patch.

I'm excited by the potential of a 3% increase in page load by simply changing constants. I was a little surprised by your results after my own tests, but can see that there are 1500 references in core (includes and
modules) and that potentially 500 of these are used on every page load.

Weblinks

Discussion

  • I am not yet convinced that this is an important optimization. I agree that it's a simple change and it has potential. But... I wrote a module named speedtest (for 6.x and checked into CVS). From this module, I ran a loop comparing the results of 10,000,000 calls to TRUE verses true, FALSE verses false, and NULL verses null. What I found was inconsistent results. I believe that the hash lookup only occurs once per parse tree instance of the constant. Thus, if FALSE Is only referenced once in the code (even if it is within a loop that is execute 10,000,000 times), we save only one hash lookup. There might be an argument that there are 1500 reference to this in core, and on any given page, 500 of them may be parsed. But I think that this optimization will have no effect when the php opcode cache is enabled (and IMO everyone should have the opcode cache enabled). - Doug Green

Coding standards

Group organizers

Group categories

Status

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week