resourceloader: Optimise getMimeType() for common case
authorTimo Tijhof <krinklemail@gmail.com>
Sat, 25 Mar 2017 03:20:44 +0000 (20:20 -0700)
committerTimo Tijhof <krinklemail@gmail.com>
Sat, 25 Mar 2017 03:23:11 +0000 (20:23 -0700)
About 0.74% of load.php is spent in CSSMin::getMimeType/finfo_file.

We don't apply this to user-generated content of external urls,
only local files in version control. And in terms of correctness,
we don't support misleading file names, so it's not a problem that
a foo.png containing XML/SVG content would get the wrong mime-type
when base64 embedding it in CSS.

Change-Id: I17686467c897984117671098e94db8732446dc75

includes/libs/CSSMin.php

index b1cece8..bba07e2 100644 (file)
@@ -176,6 +176,12 @@ class CSSMin {
         * @return bool|string
         */
        public static function getMimeType( $file ) {
+               // Infer the MIME-type from the file extension
+               $ext = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );
+               if ( isset( self::$mimeTypes[$ext] ) ) {
+                       return self::$mimeTypes[$ext];
+               }
+
                $realpath = realpath( $file );
                if (
                        $realpath
@@ -186,12 +192,6 @@ class CSSMin {
                        return finfo_file( finfo_open( FILEINFO_MIME_TYPE ), $realpath );
                }
 
-               // Infer the MIME-type from the file extension
-               $ext = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );
-               if ( isset( self::$mimeTypes[$ext] ) ) {
-                       return self::$mimeTypes[$ext];
-               }
-
                return false;
        }