url() currently uses a drupal_static() to try to optimize determining the $script variable (either empty string or 'index.php') when outputting urls when clean urls are disabled. But even the "fast" drupal_static() pattern has some overhead, which nullifies the benefit of a saved call to strpos(). Worse, the penalty is paid even when clean urls are enabled.

This patch removes it as a static variable entirely and moves the determination of $script to only happen when it's needed.

Microbenchmarking url('node/1') 100,000 times:
Clean URLs enabled:
HEAD: 2.77s
Patch: 2.66s

Clean URLs disabled:
HEAD: 3.46s
Patch: 3.49s

Another option is to apply the patch, but then change the variable to fully static (non-resettable). This has no change relative to the patch when clean urls are enabled (as one would expect) and 3.38s when clean urls are disabled (overhead of a PHP static is less than the overhead of strpos()). If we want to capture this small performance gain, we should roll a patch using a PHP static. Since SERVER_SOFTWARE shouldn't change during a page request, this would be fine in normal circumstances, but would not allow a unit test that wanted to test using different SERVER_SOFTWARE in the same page request.

There's also an @todo in the patch for whether $script is even needed any more at all. See #437228-8: Remove hard-coded check for "Apache" for deciding whether to add "index.php" within url().

CommentFileSizeAuthor
url-dont_cache_script.patch2.09 KBeffulgentsia
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

effulgentsia’s picture

Latest patch in #437228: Remove hard-coded check for "Apache" for deciding whether to add "index.php" within url() may make this one obsolete. Not marking as a dupe quite yet though.

Dries’s picture

Status: Needs review » Reviewed & tested by the community

I think this is a nice code clean-up.

catch’s picture

Looks good to me too, I don't think it's worth micro-optimizing for non-clean urls.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks!

effulgentsia’s picture

Thanks! I re-rolled #437228: Remove hard-coded check for "Apache" for deciding whether to add "index.php" within url() which addresses the @todo added by the patch committed.

Status: Fixed » Closed (fixed)
Issue tags: -Performance

Automatically closed -- issue fixed for 2 weeks with no activity.