From 9b4567f4145985dc9dd0653031e02118ce90bb18 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Tue, 14 Aug 2018 21:09:44 -0700 Subject: [PATCH] TitleValue: Don't use Assert for basic type checks Previously, `new TitleValue()` was roughly twice as slow as `Title::makeTitle()`. Switching to basic is_int/is_string checks makes TitleValue roughly twice as fast as Title! Tested with benchmarkTitleValue.php. Bug: T201801 Change-Id: Idccf159983145c1c11af9182292e1d5f704ba077 --- includes/title/TitleValue.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/includes/title/TitleValue.php b/includes/title/TitleValue.php index 3e133006d0..18e578dbb9 100644 --- a/includes/title/TitleValue.php +++ b/includes/title/TitleValue.php @@ -22,6 +22,7 @@ */ use MediaWiki\Linker\LinkTarget; use Wikimedia\Assert\Assert; +use Wikimedia\Assert\ParameterTypeException; /** * Represents a page (or page fragment) title within MediaWiki. @@ -76,10 +77,18 @@ class TitleValue implements LinkTarget { * @throws InvalidArgumentException */ public function __construct( $namespace, $dbkey, $fragment = '', $interwiki = '' ) { - Assert::parameterType( 'integer', $namespace, '$namespace' ); - Assert::parameterType( 'string', $dbkey, '$dbkey' ); - Assert::parameterType( 'string', $fragment, '$fragment' ); - Assert::parameterType( 'string', $interwiki, '$interwiki' ); + if ( !is_int( $namespace ) ) { + throw new ParameterTypeException( '$namespace', 'int' ); + } + if ( !is_string( $dbkey ) ) { + throw new ParameterTypeException( '$dbkey', 'string' ); + } + if ( !is_string( $fragment ) ) { + throw new ParameterTypeException( '$fragment', 'string' ); + } + if ( !is_string( $interwiki ) ) { + throw new ParameterTypeException( '$interwiki', 'string' ); + } // Sanity check, no full validation or normalization applied here! Assert::parameter( !preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ), '$dbkey', -- 2.20.1