MaxSem has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/86739


Change subject: Don't cache very small minified resources
......................................................................

Don't cache very small minified resources

Round trip to memcached is slower than on-the-fly minification

Change-Id: If2c29731c31f52c9518e9bccdd8ad125de4fc1d7
---
M includes/resourceloader/ResourceFilters.php
M includes/resourceloader/ResourceLoader.php
2 files changed, 37 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/39/86739/1

diff --git a/includes/resourceloader/ResourceFilters.php 
b/includes/resourceloader/ResourceFilters.php
index 2c54d6c..f207db3 100644
--- a/includes/resourceloader/ResourceFilters.php
+++ b/includes/resourceloader/ResourceFilters.php
@@ -32,6 +32,14 @@
         * @return string
         */
        function filter( $data );
+
+       /**
+        * Checks whether filtering a given content will be slower than a cache 
lookup
+        *
+        * @param string $data: Content to filter
+        * @return bool
+        */
+       function willBeExpensive( $data );
 }
 
 class CssMinifierFilter implements IResourceFilter {
@@ -41,6 +49,10 @@
                wfProfileOut( __METHOD__ );
 
                return $result;
+       }
+
+       public function willBeExpensive( $data ) {
+               return strlen( $data ) > 5000;
        }
 }
 
@@ -57,4 +69,8 @@
 
                return $result;
        }
+
+       public function willBeExpensive( $data ) {
+               return strlen( $data ) > 200;
+       }
 }
diff --git a/includes/resourceloader/ResourceLoader.php 
b/includes/resourceloader/ResourceLoader.php
index bdffa6e..174f3b8 100644
--- a/includes/resourceloader/ResourceLoader.php
+++ b/includes/resourceloader/ResourceLoader.php
@@ -148,25 +148,35 @@
                        return $data;
                }
 
-               // Try for cache hit
-               // Use CACHE_ANYTHING since filtering is very slow compared to 
DB queries
-               $key = wfMemcKey( 'resourceloader', 'filter', $filter, 
self::$filterCacheVersion, md5( $data ) );
-               $cache = wfGetCache( CACHE_ANYTHING );
-               $cacheEntry = $cache->get( $key );
-               if ( is_string( $cacheEntry ) ) {
-                       wfIncrStats( "rl-$filter-cache-hits" );
-                       wfProfileOut( __METHOD__ );
-                       return $cacheEntry;
+               // Minifying a very small resource might be faster than a cache 
lookup
+               if ( $filterObj->willBeExpensive( $data ) ) {
+                       // Try for cache hit
+                       // Use CACHE_ANYTHING since filtering is very slow 
compared to DB queries
+                       $key = wfMemcKey( 'resourceloader', 'filter', $filter, 
self::$filterCacheVersion, md5( $data ) );
+                       $cache = wfGetCache( CACHE_ANYTHING );
+                       $cacheEntry = $cache->get( $key );
+                       if ( is_string( $cacheEntry ) ) {
+                               wfIncrStats( "rl-$filter-cache-hits" );
+                               wfProfileOut( __METHOD__ );
+                               return $cacheEntry;
+                       } else {
+                               wfIncrStats( "rl-$filter-cache-misses" );
+                       }
+               } else {
+                       $cache = null;
+                       $key = '(not cached)';
+                       wfIncrStats( "rl-$filter-cache-ignores" );
                }
 
                // Run the filter
                try {
-                       wfIncrStats( "rl-$filter-cache-misses" );
                        $result = $filterObj->filter( $data );
                        $result .= "\n/* cache key: $key */";
 
                        // Save filtered text to Memcached
-                       $cache->set( $key, $result );
+                       if ( $cache ) {
+                               $cache->set( $key, $result );
+                       }
                } catch ( Exception $exception ) {
                        $exception->logException();
                        wfDebugLog( 'resourceloader', __METHOD__ . ": 
minification failed: $exception" );

-- 
To view, visit https://gerrit.wikimedia.org/r/86739
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If2c29731c31f52c9518e9bccdd8ad125de4fc1d7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to