jenkins-bot has submitted this change and it was merged.

Change subject: Speed up PrefixedGlobalFunctionsSniff
......................................................................


Speed up PrefixedGlobalFunctionsSniff

This causes 1 scan of all tokens in a file looking
for namespaces where as before one would have been
done for EVERY method in the file.

While profiling on the Title file in mwcore
the tokenIsNamespaced accounted for 22% of execution
time while running phpcs on the file.

After this refactoring tokenIsNamespaced only accounts
for 0.2% of the time in this file.

This is from 2313ms to 23ms.

In its current form of course the static arrays will grow
throughout the phpcs run but I don't really see that becoming
a major issue.
Auto cleanup of the arrays through the phpcs run would be easy
but best not to both until this is needed as we want speed here!

Change-Id: I4cd5cbed47d24a6c3a655e0225d0722f764f7c69
---
M MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php
1 file changed, 50 insertions(+), 8 deletions(-)

Approvals:
  WMDE-Fisch: Looks good to me, but someone else must approve
  Legoktm: Looks good to me, approved
  Gabriel Birke: Looks good to me, but someone else must approve
  jenkins-bot: Verified



diff --git 
a/MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php 
b/MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php
index 5075d5c..aa27121 100644
--- a/MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php
+++ b/MediaWiki/Sniffs/NamingConventions/PrefixedGlobalFunctionsSniff.php
@@ -7,36 +7,78 @@
 // @codingStandardsIgnoreStart
 class MediaWiki_Sniffs_NamingConventions_PrefixedGlobalFunctionsSniff
        implements PHP_CodeSniffer_Sniff {
+
        // @codingStandardsIgnoreEnd
 
        public $ignoreList = [];
 
        public function register() {
+
                return [ T_FUNCTION ];
        }
 
        /**
+        * @var int[] array containing the first locations of namespaces in 
files that we have seen so far.
+        */
+       private static $firstNamespaceLocations = [];
+
+       /**
+        * @var string[] array containing a list of files that contain no 
namespace statements.
+        */
+       private static $noNamespaceFiles = [];
+
+       /**
         * @param PHP_CodeSniffer_File $phpcsFile
         * @param int $ptr
+        *
         * @return bool Does a namespace statement exist before this position 
in the file?
         */
        private function tokenIsNamespaced( PHP_CodeSniffer_File $phpcsFile, 
$ptr ) {
-               $tokens = $phpcsFile->getTokens();
-               while ( $ptr > 0 ) {
-                       $token = $tokens[$ptr];
-                       if ( $token['type'] === "T_NAMESPACE" && !isset( 
$token['scope_opener'] ) ) {
-                               // In the format of "namespace Foo;", which 
applies to the entire file
-                               return true;
-                       }
-                       $ptr--;
+
+               $fileName = $phpcsFile->getFilename();
+               // Check if we already know if the token is namespaced or not 
and return early if possible.
+               if (
+                       isset( self::$firstNamespaceLocations[$fileName] ) &&
+                       $ptr > self::$firstNamespaceLocations[$fileName] )
+               {
+                       return true;
                }
+               if ( isset( self::$noNamespaceFiles[$fileName] ) ) {
+                       return false;
+               }
+
+               // If not scan the whole file at once looking for namespacing 
or lack of and set in the statics.
+               $tokens = $phpcsFile->getTokens();
+               $tokenIndex = max( array_keys( $tokens ) );
+               while ( $tokenIndex > 0 ) {
+                       $token = $tokens[$tokenIndex];
+                       if ( $token['type'] === "T_NAMESPACE" && !isset( 
$token['scope_opener'] ) ) {
+                               // In the format of "namespace Foo;", which 
applies to everything below
+                               self::$firstNamespaceLocations[$fileName] = 
$tokenIndex;
+                       }
+                       $tokenIndex--;
+               }
+               if ( !isset( self::$firstNamespaceLocations[$fileName] ) ) {
+                       self::$noNamespaceFiles[$fileName] = true;
+               }
+
+               // Return if the token was namespaced.
+               if (
+                       isset( self::$firstNamespaceLocations[$fileName] ) &&
+                       $ptr > self::$firstNamespaceLocations[$fileName] )
+               {
+                       return true;
+               }
+
                return false;
        }
 
        public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
+
                if ( $this->tokenIsNamespaced( $phpcsFile, $stackPtr ) ) {
                        return;
                }
+
                $tokens = $phpcsFile->getTokens();
                $token = $tokens[$stackPtr];
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I4cd5cbed47d24a6c3a655e0225d0722f764f7c69
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Gabriel Birke <[email protected]>
Gerrit-Reviewer: Hashar <[email protected]>
Gerrit-Reviewer: Jakob <[email protected]>
Gerrit-Reviewer: Kai Nissen (WMDE) <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: Polybuildr <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: WMDE-Fisch <[email protected]>
Gerrit-Reviewer: WMDE-leszek <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to