Attached patch uses a single instance of str_replace which is fed pre-processed find/replace arrays extrapolated from the database prefix setting. Speed increase is found by using array_walk() to modify the prefix array instead of looping through the array using PHP.
For sites where the database prefix is an array of values the speed improvement for this function is about 40-50% depending on size of the prefix array. Where the prefix is a string there is a very small performance hit of about 5%.
Also attached is a reduced test showing speed improvements over the old method (extension renamed).
I have a similar patch for Drupal 6 if anyone is interested in that too.
Comments
Comment #1
sambauers commentedAttached patch is actually slower than the old method when there are no replacements to be made in the string, but how often does that happen? Not much I'm guessing. On the other hand, patch gets comparatively faster the more replacements there are to be done in the string.
Comment #2
Crell commentedActually, DB prefixes are the minority case by a huge margin. That's the case we should optimize for.
If you can provide a patch that speeds up the multi-prefix case but doesn't harm the normal case we can consider it.
Comment #3
sambauers commentedIn further testing and tweaking I've reduced this down to a simple replacement of strtr() with str_replace().
This showed small loses in the simplest cases of approximately 5% - 10% on what are already very fast cases. So the loss over 100 iterations is a maximum of 1/100000th of a second on my test bed running PHP 5.3.
On the other hand, the improvement in all cases where there are values in the prefix array is between 25% and 1500%, in some cases saving 1/2 a second over 100 iterations.
I'm uploading a new patch (faster-prefixtables-2.patch) and more thorough isolated test script that compares speed over several cases (faster-prefixtables-speedtest-2.php_.txt).
This is a pretty big deal for our organisation due to the way that our CiviCRM install is setup using an array of prefixes.
Comment #5
sambauers commented#3: faster-prefixtables-2.patch queued for re-testing.
Comment #6
bfroehle commentedPlenty of benchmarks for strtr vs str_replace: http://www.google.com/search?q=strtr+vs+str_replace
Comment #7
andypostDuplicate of #561422: Replace strtr() with str_replace() for db prefixing
Currently str_replace() is used - code
using PHP 5.3 - str_replace is much faster 2k times then php 5.2 which are the same