Physikerwelt has uploaded a new change for review.

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

Change subject: Improve performance of UpdateMath.php
......................................................................

Improve performance of UpdateMath.php

* Speed up hook
* Skip MathML verification, when purge == false

Change-Id: I722d596b3785b273c85d5b5892f83ab6efb05b35
---
M MathSearch.hooks.php
M maintenance/UpdateMath.php
2 files changed, 53 insertions(+), 42 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MathSearch 
refs/changes/76/185676/1

diff --git a/MathSearch.hooks.php b/MathSearch.hooks.php
index 9892e4a..e91a737 100644
--- a/MathSearch.hooks.php
+++ b/MathSearch.hooks.php
@@ -91,19 +91,7 @@
                        if ( $exists ) {
                                wfDebugLog( "MathSearch", 'Index $' . $tex . '$ 
already in database.' );
                        } else {
-                               wfDebugLog( "MathSearch", 'Store index for $' . 
$tex . '$ in database' );
-                               $dbw = wfGetDB( DB_MASTER );
-                               $dbw->onTransactionIdle(
-                                       function () use ( $oldID, $eid, 
$inputHash, $dbw ) {
-                                               $dbw->replace( 'mathindex',
-                                                       array( 
'mathindex_revision_id', 'mathindex_anchor' ),
-                                                       array(
-                                                               
'mathindex_revision_id' => $oldID,
-                                                               
'mathindex_anchor' =>  $eid ,
-                                                               
'mathindex_inputhash' => $inputHash
-                                                       ) );
-                                       }
-                               );
+                               self::writeMathIndex( $eid, $inputHash, $tex, 
$oldID );
                        }
                } catch ( Exception $e ) {
                        wfDebugLog( "MathSearch", 'Problem writing to math 
index!'
@@ -197,9 +185,6 @@
         * @param null $Result
         * @param int $pid
         * @param int $eid
-        * @internal param $content
-        * @internal param $attributes
-        * @internal param \Parser $parser
         * @return boolean (true)
         */
        static function onMathFormulaRenderedNoLink( $Renderer, &$Result = 
null, $pid = 0, $eid = 0 ) {
@@ -230,4 +215,22 @@
        static function generateMathAnchorString($pageID, $anchorID, $prefix = 
"#"){
                return "{$prefix}math.$pageID.$anchorID";
        }
+
+       /**
+        * @param $eid
+        * @param $inputHash
+        * @param $tex
+        * @param $oldID
+        */
+       public static function writeMathIndex( $eid, $inputHash, $tex, $oldID ) 
{
+               wfDebugLog( "MathSearch", 'Store index for $' . $tex . '$ in 
database' );
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->onTransactionIdle( function () use ( $oldID, $eid, 
$inputHash, $dbw ) {
+                       $dbw->replace( 'mathindex', array( 
'mathindex_revision_id', 'mathindex_anchor' ), array(
+                                       'mathindex_revision_id' => $oldID,
+                                       'mathindex_anchor' => $eid,
+                                       'mathindex_inputhash' => $inputHash
+                               ) );
+               } );
+       }
 }
diff --git a/maintenance/UpdateMath.php b/maintenance/UpdateMath.php
index e31926c..5f9ac25 100644
--- a/maintenance/UpdateMath.php
+++ b/maintenance/UpdateMath.php
@@ -30,9 +30,9 @@
        public $dbw = null;
        /** @var MathRenderer  */
        private $current;
-       private $time = 0;//microtime( true );
+       private $time = 0.0;//microtime( true );
        private $performance = array();
-       private $renderingMode =  MW_MATH_LATEXML;
+       private $renderingMode = 7; // MW_MATH_MATHML
 
        /**
         * @var DatabaseBase
@@ -56,7 +56,7 @@
        }
        private function time($category='default'){
                global $wgMathDebug;
-               $delta = (microtime(true) - $this->time)*1000;
+               $delta = ( microtime( true ) - $this->time );
                if (isset ($this->performance[$category] ))
                        $this->performance[$category] += $delta;
                else
@@ -86,16 +86,16 @@
                }
                $this->output( "Rebuilding index fields for {$count} pages with 
option {$this->purge}...\n" );
                $fcount = 0;
-
+               //return;
                while ( $n < $count ) {
                        if ( $n ) {
                                $this->output( $n . " of $count \n" );
                        }
-                       $end = $n + self::RTI_CHUNK_SIZE - 1;
+                       $end = min( $n + self::RTI_CHUNK_SIZE - 1, $count );
 
                        $res = $this->db->select( array( 'page', 'revision', 
'text' ),
-                                       array( 'page_id', 'page_namespace', 
'page_title', 'old_flags', 'old_text' ),
-                                       array( "page_id BETWEEN $n AND $end", 
'page_latest = rev_id', 'rev_text_id = old_id' ),
+                                       array( 'page_id', 'page_namespace', 
'page_title', 'old_flags', 'old_text', 'rev_id' ),
+                                       array( "rev_id BETWEEN $n AND $end", 
'page_latest = rev_id', 'rev_text_id = old_id' ),
                                        __METHOD__
                        );
                        $this->dbw->begin();
@@ -104,14 +104,14 @@
                        foreach ( $res as $s ) {
                                echo "\np$i:";
                                $revtext = Revision::getRevisionText( $s );
-                               $fcount += $this->doUpdate( $s->page_id, 
$revtext, $s->page_title);
+                               $fcount += $this->doUpdate( $s->page_id, 
$revtext, $s->page_title, $s->rev_id );
                                $i++;
                        }
                        // echo "before" +$this->dbw->selectField('mathindex', 
'count(*)')."\n";
                        $start = microtime( true );
                        $this->dbw->commit();
                        echo " committed in " . ( microtime( true ) -$start ) . 
"s\n\n";
-                       var_export($this->performance);
+                       var_dump($this->performance);
                        // echo "after" +$this->dbw->selectField('mathindex', 
'count(*)')."\n";
                        $n += self::RTI_CHUNK_SIZE;
                }
@@ -119,14 +119,14 @@
        }
 
        /**
-        * @param $pid
-        * @param unknown $pText
-        * @param string $pTitle
-        * @internal param unknown $pId
-        * @internal param string $purge
+        * @param int     $pid
+        * @param string  $pText
+        * @param string  $pTitle
+        * @param int     $revId
+        *
         * @return number
         */
-       private function doUpdate( $pid, $pText, $pTitle = "") {
+       private function doUpdate( $pid, $pText, $pTitle = "", $revId = 0) {
                $notused = '';
                // TODO: fix link id problem
                $anchorID = 0;
@@ -146,19 +146,23 @@
                                        $this->time("checkTex");
                                }
                                if ( $checked ) {
-                                       $renderer->render( $this->purge );
-                                       if( $renderer->getMathml() ){
-                                               $this->time("Rendering");
-                                       } else {
-                                               $this->time("Failing");
-                                       }
-                                       if ( $this->getOption( "SVG", false ) ) 
{
-                                               $svg = $renderer->getSvg();
-                                               if ( $svg ) {
-                                                       $this->time( 
"SVG-Rendering" );
+                                       if( ! $renderer->isInDatabase() || 
$this->purge ) {
+                                               $renderer->render( $this->purge 
);
+                                               if( $renderer->getMathml() ){
+                                                       $this->time("render");
                                                } else {
-                                                       $this->time( "SVG-Fail" 
);
+                                                       $this->time("Failing");
                                                }
+                                               if ( $this->getOption( "SVG", 
false ) ) {
+                                                       $svg = 
$renderer->getSvg();
+                                                       if ( $svg ) {
+                                                               $this->time( 
"SVG-Rendering" );
+                                                       } else {
+                                                               $this->time( 
"SVG-Fail" );
+                                                       }
+                                               }
+                                       } else {
+                                               $this->time('checkInDB');
                                        }
                                } else {
                                        $this->time("checkTex-Fail");
@@ -169,6 +173,10 @@
                                        wfRunHooks( 'MathFormulaRendered', 
array( &$renderer, &$notused, $pid, $anchorID ) );
                                        $this->time( "hooks" );
                                        $anchorID++;
+                               } else {
+                                       MathSearchHooks::writeMathIndex( 
$anchorID, $pid, $revId , $renderer->getInputHash() );
+                                       $this->time( "index" );
+                                       $anchorID++;
                                }
                                $renderer->writeCache($this->dbw);
                                $this->time("write Cache");

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I722d596b3785b273c85d5b5892f83ab6efb05b35
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MathSearch
Gerrit-Branch: master
Gerrit-Owner: Physikerwelt <[email protected]>

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

Reply via email to