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().
Comment | File | Size | Author |
---|---|---|---|
url-dont_cache_script.patch | 2.09 KB | effulgentsia | |
Comments
Comment #1
effulgentsia CreditAttribution: effulgentsia commentedLatest 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.
Comment #2
Dries CreditAttribution: Dries commentedI think this is a nice code clean-up.
Comment #3
catchLooks good to me too, I don't think it's worth micro-optimizing for non-clean urls.
Comment #4
Dries CreditAttribution: Dries commentedCommitted to CVS HEAD. Thanks!
Comment #5
effulgentsia CreditAttribution: effulgentsia commentedThanks! 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.