objectcache: Optimise array_map in MemcachedBagOStuff::makeKey()
authorTimo Tijhof <krinklemail@gmail.com>
Sat, 31 Aug 2019 21:38:42 +0000 (22:38 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Sat, 31 Aug 2019 21:43:21 +0000 (22:43 +0100)
This can get called a lot on an average page load, optimise
a bit by using a referenced foreach iteration instead.

Using a simplified test case, I found this saves about 70% on PHP 7.2.
For 100 iterations, from ~1.5ms to ~0.4 ms.

```lang=php
$args = [ 'FooBar', 'thisthat', 4, 'foo', 12 ];
$left = 100;
foreach ($args as &$arg) {
    $arg = strtr( $arg, ' ', '_' );
    if ( strlen( $arg ) < $left || true ) {
        $arg = '#' . $arg;
    }
    $left--;
}
```

Change-Id: Ie779c4661306a6d3dc08c08671f1a36682ca1afb

includes/libs/objectcache/MemcachedBagOStuff.php

index dc40931..40f2836 100644 (file)
@@ -49,29 +49,25 @@ abstract class MemcachedBagOStuff extends MediumSpecificBagOStuff {
                // custom prefixes used by thing like WANObjectCache, limit to 205.
                $charsLeft = 205 - strlen( $keyspace ) - count( $args );
 
-               $args = array_map(
-                       function ( $arg ) use ( &$charsLeft ) {
-                               $arg = strtr( $arg, ' ', '_' );
+               foreach ( $args as &$arg ) {
+                       $arg = strtr( $arg, ' ', '_' );
 
-                               // Make sure %, #, and non-ASCII chars are escaped
-                               $arg = preg_replace_callback(
-                                       '/[^\x21-\x22\x24\x26-\x39\x3b-\x7e]+/',
-                                       function ( $m ) {
-                                               return rawurlencode( $m[0] );
-                                       },
-                                       $arg
-                               );
+                       // Make sure %, #, and non-ASCII chars are escaped
+                       $arg = preg_replace_callback(
+                               '/[^\x21-\x22\x24\x26-\x39\x3b-\x7e]+/',
+                               function ( $m ) {
+                                       return rawurlencode( $m[0] );
+                               },
+                               $arg
+                       );
 
-                               // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
-                               if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
-                                       $arg = '#' . md5( $arg );
-                               }
+                       // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
+                       if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
+                               $arg = '#' . md5( $arg );
+                       }
 
-                               $charsLeft -= strlen( $arg );
-                               return $arg;
-                       },
-                       $args
-               );
+                       $charsLeft -= strlen( $arg );
+               }
 
                if ( $charsLeft < 0 ) {
                        return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $args ) );