From ede5e0db1ecd553f0cb2bfb72a1e16f3d74e15f4 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Fri, 3 May 2019 15:03:50 -0700 Subject: [PATCH] Optimize WikiMap::getWikiFromUrl() for the common local wiki case MediaWiki::getUrlDomainDistance was showing up as taking 8-14ms in xhgui Change-Id: I55d1c1a0e79eda9045c3487a06f5fbb967747ec6 --- includes/WikiMap.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/includes/WikiMap.php b/includes/WikiMap.php index 8b000f2063..23b0e3edb2 100644 --- a/includes/WikiMap.php +++ b/includes/WikiMap.php @@ -221,19 +221,29 @@ class WikiMap { * @since 1.30 */ public static function getWikiFromUrl( $url ) { + global $wgCanonicalServer; + + if ( strpos( $url, "$wgCanonicalServer/" ) === 0 ) { + // Optimisation: Handle the the common case. + // (Duplicates self::getCanonicalServerInfoForAllWikis) + return self::getWikiIdFromDbDomain( self::getCurrentWikiDbDomain() ); + } + $urlPartsCheck = wfParseUrl( $url ); if ( $urlPartsCheck === false ) { return false; } - $urlPartsCheck = array_intersect_key( $urlPartsCheck, [ 'host' => 1, 'port' => 1 ] ); + static $relevantKeys = [ 'host' => 1, 'port' => 1 ]; + $urlPartsCheck = array_intersect_key( $urlPartsCheck, $relevantKeys ); + foreach ( self::getCanonicalServerInfoForAllWikis() as $wikiId => $info ) { $urlParts = $info['parts']; if ( $urlParts === false ) { continue; // sanity } - $urlParts = array_intersect_key( $urlParts, [ 'host' => 1, 'port' => 1 ] ); + $urlParts = array_intersect_key( $urlParts, $relevantKeys ); if ( $urlParts == $urlPartsCheck ) { return $wikiId; } -- 2.20.1