Made a new SectionProfileCallback class that extends ScopedCallback
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 17 Dec 2014 21:16:06 +0000 (13:16 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 17 Dec 2014 21:16:06 +0000 (13:16 -0800)
* This is now used by SectionProfiler and avoids the high overhead of call_user_func_array().

Change-Id: I7ff2c9a35c7cd8ee462f2368b655e766ad33dd63

autoload.php
includes/libs/ScopedCallback.php
includes/profiler/ProfilerStub.php
includes/profiler/SectionProfiler.php

index fd2781c..d4a152a 100644 (file)
@@ -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',
index 629c269..1ec9eaa 100644 (file)
@@ -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;
index 9a7ec8c..1d77cc0 100644 (file)
@@ -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() {
index 2c36b68..d5da928 100644 (file)
@@ -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 );
+       }
+}