Pastakhov has uploaded a new change for review.

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


Change subject: performance optimization (version 0.5.2)
......................................................................

performance optimization (version 0.5.2)

* remove function getOperatorPrecedence

Is faster up to 10 percent

Change-Id: I7320d0df8c9263787ac4cb07fb2c87e1e1192710
---
M Foxway.php
M includes/Runtime.php
2 files changed, 16 insertions(+), 21 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Foxway 
refs/changes/66/71766/1

diff --git a/Foxway.php b/Foxway.php
index aa3f28e..efcdc10 100644
--- a/Foxway.php
+++ b/Foxway.php
@@ -15,7 +15,7 @@
        die( 'This file is an extension to MediaWiki and thus not a valid entry 
point.' );
 }
 
-define( 'Foxway_VERSION' , '0.5.1' );
+define( 'Foxway_VERSION' , '0.5.2' );
 
 // Register this extension on Special:Version
 $wgExtensionCredits['parserhook'][] = array(
diff --git a/includes/Runtime.php b/includes/Runtime.php
index 4c4fac6..26d2930 100644
--- a/includes/Runtime.php
+++ b/includes/Runtime.php
@@ -65,7 +65,8 @@
                array(T_LOGICAL_OR), // or
                array(','),
        );
-       private $countPrecedences;
+       private static $precedencesCount;
+       private static $precedencesMatrix=array();
 
        public function __construct( array $args, $scope ) {
                $this->args = $args;
@@ -77,7 +78,12 @@
                $this->thisVariables['argv'] = $args;
                $this->thisVariables['argc'] = count($args);
                $this->thisVariables['GLOBALS'] = &self::$globalVariables;
-               $this->countPrecedences = count(self::$operatorsPrecedence)-1;
+               if( empty(self::$precedencesCount) ) {
+                       foreach (self::$operatorsPrecedence as $key => &$value) 
{
+                               self::$precedencesMatrix += 
array_fill_keys($value, $key);
+                       }
+                       self::$precedencesCount = $key;
+               }
        }
 
        public function getOperators() {
@@ -88,17 +94,6 @@
                        }
                }
                return $operators;
-       }
-
-       protected function getOperatorPrecedence( $operator ) {
-               $precendence = 0;
-               foreach (self::$operatorsPrecedence as &$operators) {
-                       if( in_array($operator, $operators) ) {
-                               break;
-                       }
-                       $precendence ++;
-               }
-               return $precendence;
        }
 
        protected function pushStack() {
@@ -126,7 +121,7 @@
 
        public function addCommand( $name ) {
                if( $this->lastOperator ) {
-                       $precedence = $this->getOperatorPrecedence( 
$this->lastOperator );
+                       $precedence = 
self::$precedencesMatrix[$this->lastOperator];
                        $this->mathMemory[$precedence] = 
array($this->lastOperator, $this->lastParam);
                        $this->lastOperator = false;
                }
@@ -179,7 +174,7 @@
 
        protected function addParam(RValue $param) {
                if( $this->lastOperator ) {
-                       $precedence = $this->getOperatorPrecedence( 
$this->lastOperator );
+                       $precedence = 
self::$precedencesMatrix[$this->lastOperator];
                        $this->mathMemory[$precedence] = 
array($this->lastOperator, $this->lastParam);
                        $this->lastOperator = false;
                }
@@ -190,7 +185,7 @@
                global $wgFoxwayPassByReference;
 
                if( $this->lastOperator ) {
-                       $precedence = $this->getOperatorPrecedence( 
$this->lastOperator );
+                       $precedence = 
self::$precedencesMatrix[$this->lastOperator];
                        $this->mathMemory[$precedence] = 
array($this->lastOperator, $this->lastParam);
                        $this->lastOperator = false;
                }
@@ -221,7 +216,7 @@
        public function addOperator( $operator ) {
                switch ($operator) {
                        case ',':
-                               $this->doMath( 
$this->getOperatorPrecedence($operator) );
+                               $this->doMath( 
self::$precedencesMatrix[$operator] );
                                if( $this->lastOperator == T_DOUBLE_ARROW ) {
                                        $this->lastOperator = false;
                                }else{
@@ -237,7 +232,7 @@
                                $this->lastParam = null;
                                break;
                        case '?':
-                               $this->doMath( 
$this->getOperatorPrecedence($operator) );
+                               $this->doMath( 
self::$precedencesMatrix[$operator] );
                                return $this->lastParam->getValue();
                                break;
                        case '"(':
@@ -300,7 +295,7 @@
                                $this->popStack();
                                break;
                        default:
-                               $precedence = $this->getOperatorPrecedence( 
$operator );
+                               $precedence = 
self::$precedencesMatrix[$operator];
                                //                                              
For negative operator
                                if( $precedence == 0 || $this->lastOperator || 
is_null($this->lastParam) ) {
                                        switch ($operator) {
@@ -347,7 +342,7 @@
                        unset($this->mathMemory[0]);
                }
                if($precedence === false){
-                       $precedence = $this->countPrecedences;
+                       $precedence = self::$precedencesCount;
                }
                for($n = 1; $n <= $precedence; $n++) {
                        if( isset($this->mathMemory[$n]) ) {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7320d0df8c9263787ac4cb07fb2c87e1e1192710
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Foxway
Gerrit-Branch: master
Gerrit-Owner: Pastakhov <pastak...@yandex.ru>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to