Performance: Shortcut Language::truncate if there's no need to truncate
authorJesús Martínez Novo <martineznovo@gmail.com>
Sat, 17 Jun 2017 11:56:14 +0000 (13:56 +0200)
committerUmherirrender <umherirrender_de.wp@web.de>
Fri, 12 Jan 2018 21:05:17 +0000 (21:05 +0000)
Return the unmodified string if there's no need to truncate it without
doing a not-so-trivial round of getting a message from the message
cache.

Change-Id: I11ac88672aeb9d1c4f5709b79ad2d17223bd64d8

languages/Language.php

index fdf2d05..27c9faf 100644 (file)
@@ -3489,15 +3489,16 @@ class Language {
         * @return string
         */
        function truncate( $string, $length, $ellipsis = '...', $adjustLength = true ) {
+               # Check if there is no need to truncate
+               if ( strlen( $string ) <= abs( $length ) ) {
+                       return $string; // no need to truncate
+               }
                # Use the localized ellipsis character
                if ( $ellipsis == '...' ) {
                        $ellipsis = wfMessage( 'ellipsis' )->inLanguage( $this )->escaped();
                }
-               # Check if there is no need to truncate
                if ( $length == 0 ) {
                        return $ellipsis; // convention
-               } elseif ( strlen( $string ) <= abs( $length ) ) {
-                       return $string; // no need to truncate
                }
                $stringOriginal = $string;
                # If ellipsis length is >= $length then we can't apply $adjustLength