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
This commit is contained in:
Stegmujo 2022-10-24 10:55:50 +00:00
parent 2d56f19e3c
commit b5d3bca4b4
7 changed files with 25 additions and 11 deletions

View file

@ -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();
}
/**

View file

@ -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' );
}

View file

@ -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 {
}
}
};
};

View file

@ -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.

View file

@ -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 {

View file

@ -1,6 +1,7 @@
/** PEGjs lexer/parser */
{
$this->tu = new TexUtil();
$this->tu = TexUtil::getInstance();
# get reference of the options for usage in functions.

View file

@ -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 = [