From b5d3bca4b47d258fee1773ae52627c79803f1e50 Mon Sep 17 00:00:00 2001 From: Stegmujo Date: Mon, 24 Oct 2022 10:55:50 +0000 Subject: [PATCH] Fix Perfomance Issues of TexUtil TexUtil gets instanciated and loads a json for each node in Literal and Fun1. This is not necessary since it takes much RAM and time to load the corresponding JSON from files. Bug: T321599 Change-Id: I91855ebc4437f075302ae13d367ca4902cc0f3ba --- src/TexVC/Nodes/Fun1.php | 2 +- src/TexVC/Nodes/Literal.php | 3 +-- src/TexVC/Parser.php | 4 ++-- src/TexVC/TexUtil.php | 15 ++++++++++++++- src/TexVC/TexVC.php | 2 +- src/TexVC/parser.pegjs | 3 ++- tests/phpunit/unit/TexVC/TexUtilTest.php | 7 ++++--- 7 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/TexVC/Nodes/Fun1.php b/src/TexVC/Nodes/Fun1.php index 6cf7ecebc..234dc5cdb 100644 --- a/src/TexVC/Nodes/Fun1.php +++ b/src/TexVC/Nodes/Fun1.php @@ -19,7 +19,7 @@ class Fun1 extends TexNode { parent::__construct( $fname, $arg ); $this->fname = $fname; $this->arg = $arg; - $this->tu = new TexUtil(); + $this->tu = TexUtil::getInstance(); } /** diff --git a/src/TexVC/Nodes/Literal.php b/src/TexVC/Nodes/Literal.php index 741fc4b5a..22338e0d7 100644 --- a/src/TexVC/Nodes/Literal.php +++ b/src/TexVC/Nodes/Literal.php @@ -16,8 +16,7 @@ class Literal extends TexNode { public function __construct( string $arg ) { parent::__construct( $arg ); $this->arg = $arg; - $tu = new TexUtil(); - $this->literals = array_keys( $tu->getBaseElements()['is_literal'] ); + $this->literals = array_keys( TexUtil::getInstance()->getBaseElements()['is_literal'] ); $this->extendedLiterals = $this->literals; array_push( $this->extendedLiterals, '\\infty', '\\emptyset' ); } diff --git a/src/TexVC/Parser.php b/src/TexVC/Parser.php index 1dc58c0b5..8523fb347 100644 --- a/src/TexVC/Parser.php +++ b/src/TexVC/Parser.php @@ -9536,7 +9536,7 @@ class Parser { /* BEGIN initializer code */ - $this->tu = new TexUtil(); + $this->tu = TexUtil::getInstance(); # get reference of the options for usage in functions. @@ -9566,4 +9566,4 @@ class Parser { } } -}; \ No newline at end of file +}; diff --git a/src/TexVC/TexUtil.php b/src/TexVC/TexUtil.php index 13e0a7beb..54df09899 100644 --- a/src/TexVC/TexUtil.php +++ b/src/TexVC/TexUtil.php @@ -7,6 +7,7 @@ namespace MediaWiki\Extension\Math\TexVC; use MWException; class TexUtil { + private static $instance = null; private $allFunctions; private $baseElements; @@ -15,7 +16,7 @@ class TexUtil { * allFunctions holds the root-level function keys * other objects are second level elements and hold all functions which are assigned to this second level elements */ - public function __construct() { + private function __construct() { $jsonContent = $this->getJSON(); // dynamically create functions from the content $this->allFunctions = []; @@ -41,6 +42,18 @@ class TexUtil { } } + public static function removeInstance() { + self::$instance = null; + } + + public static function getInstance() { + if ( self::$instance == null ) { + self::$instance = new TexUtil(); + } + + return self::$instance; + } + /** * Returning the base elements array. * This is only used for testing in TexUtilTest.php. diff --git a/src/TexVC/TexVC.php b/src/TexVC/TexVC.php index 8a1d15da8..88d6fee77 100644 --- a/src/TexVC/TexVC.php +++ b/src/TexVC/TexVC.php @@ -24,7 +24,7 @@ class TexVC { public function __construct() { $this->parser = new Parser(); - $this->tu = new TexUtil(); + $this->tu = TexUtil::getInstance(); } private function strStartsWith( $haystack, $needle ): bool { diff --git a/src/TexVC/parser.pegjs b/src/TexVC/parser.pegjs index 795ea6d64..52293d299 100644 --- a/src/TexVC/parser.pegjs +++ b/src/TexVC/parser.pegjs @@ -1,6 +1,7 @@ /** PEGjs lexer/parser */ { -$this->tu = new TexUtil(); +$this->tu = TexUtil::getInstance(); + # get reference of the options for usage in functions. diff --git a/tests/phpunit/unit/TexVC/TexUtilTest.php b/tests/phpunit/unit/TexVC/TexUtilTest.php index 78b27ae9a..cf15ffaf3 100644 --- a/tests/phpunit/unit/TexVC/TexUtilTest.php +++ b/tests/phpunit/unit/TexVC/TexUtilTest.php @@ -11,10 +11,11 @@ use MediaWikiUnitTestCase; class TexUtilTest extends MediaWikiUnitTestCase { /** - * Basic test for tex util. + * Basic test for TexUtil */ public function testTexUtil() { - $tu = new TexUtil(); + TexUtil::removeInstance(); + $tu = TexUtil::getInstance(); // Testing all functions $this->assertTrue( $tu->getAllFunctionsAt( "\\AA" ) ); $this->assertFalse( $tu->getAllFunctionsAt( "\\notlisted" ) ); @@ -28,7 +29,7 @@ class TexUtilTest extends MediaWikiUnitTestCase { * @return void */ public function testChecksum() { - $tu = new TexUtil(); + $tu = TexUtil::getInstance(); $out = []; $sets = [