From 626aede99bf83733d1aeb4dfd636129494d2888e Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Wed, 17 Dec 2014 13:16:06 -0800 Subject: [PATCH] Made a new SectionProfileCallback class that extends ScopedCallback * This is now used by SectionProfiler and avoids the high overhead of call_user_func_array(). Change-Id: I7ff2c9a35c7cd8ee462f2368b655e766ad33dd63 --- autoload.php | 1 + includes/libs/ScopedCallback.php | 4 ++-- includes/profiler/ProfilerStub.php | 4 +--- includes/profiler/SectionProfiler.php | 29 +++++++++++++++++++++++++-- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/autoload.php b/autoload.php index fd2781c2b4..d4a152a023 100644 --- a/autoload.php +++ b/autoload.php @@ -1018,6 +1018,7 @@ $wgAutoloadLocalClasses = array( 'SearchResultSet' => __DIR__ . '/includes/search/SearchResultSet.php', 'SearchSqlite' => __DIR__ . '/includes/search/SearchSqlite.php', 'SearchUpdate' => __DIR__ . '/includes/deferred/SearchUpdate.php', + 'SectionProfileCallback' => __DIR__ . '/includes/profiler/SectionProfiler.php', 'SectionProfiler' => __DIR__ . '/includes/profiler/SectionProfiler.php', 'SevenZipStream' => __DIR__ . '/maintenance/7zip.inc', 'ShiConverter' => __DIR__ . '/languages/classes/LanguageShi.php', diff --git a/includes/libs/ScopedCallback.php b/includes/libs/ScopedCallback.php index 629c26907c..1ec9eaa627 100644 --- a/includes/libs/ScopedCallback.php +++ b/includes/libs/ScopedCallback.php @@ -32,12 +32,12 @@ class ScopedCallback { protected $params; /** - * @param callable $callback + * @param callable|null $callback * @param array $params Callback arguments (since 1.25) * @throws Exception */ public function __construct( $callback, array $params = array() ) { - if ( !is_callable( $callback ) ) { + if ( $callback !== null && !is_callable( $callback ) ) { throw new InvalidArgumentException( "Provided callback is not valid." ); } $this->callback = $callback; diff --git a/includes/profiler/ProfilerStub.php b/includes/profiler/ProfilerStub.php index 9a7ec8c3ff..1d77cc0fb5 100644 --- a/includes/profiler/ProfilerStub.php +++ b/includes/profiler/ProfilerStub.php @@ -34,9 +34,7 @@ class ProfilerStub extends Profiler { } public function scopedProfileIn( $section ) { - return new ScopedCallback( function () { - // no-op - } ); + return new ScopedCallback( null ); // no-op } public function getFunctionStats() { diff --git a/includes/profiler/SectionProfiler.php b/includes/profiler/SectionProfiler.php index 2c36b68c15..d5da928957 100644 --- a/includes/profiler/SectionProfiler.php +++ b/includes/profiler/SectionProfiler.php @@ -67,8 +67,7 @@ class SectionProfiler { public function scopedProfileIn( $section ) { $this->profileInInternal( $section ); - $that = $this; - return new ScopedCallback( $this->profileOutCallback, array( $that, $section ) ); + return new SectionProfileCallback( $this, $section ); } /** @@ -502,3 +501,29 @@ class SectionProfiler { } } } + +/** + * Subclass ScopedCallback to avoid call_user_func_array(), which is slow + * + * This class should not be used outside of SectionProfiler + */ +class SectionProfileCallback extends ScopedCallback { + /** @var SectionProfiler */ + protected $profiler; + /** @var string */ + protected $section; + + /** + * @param SectionProfiler $profiler + * @param string $section + */ + public function __construct( SectionProfiler $profiler, $section ) { + parent::__construct( null ); + $this->profiler = $profiler; + $this->section = $section; + } + + function __destruct() { + $this->profiler->profileOutInternal( $this->section ); + } +} -- 2.20.1