Not sure if we can easily improve this but I notice that when comparing HHVM-level flame graphs (from hourly/daily aggregation of Xenon, sampling profiler) to flame graphs from a one-off run with X-Wikimedia-Debug (XHProf to XHGui), it starts relatively late.
E.g. The outer level is basically the mediawiki core entry point file. We're missing the loading of multiversion, wmf-config and vendor.
| X-Wikimedia-Debug (StartProfiler > XHProf > XHGui) | Xenon (HHVM)
|--|--
| {F10703077} | {F10703079}
| top-level files: core/vendor, core/LocalSettings (wmf-config) | top-level files: **core/load.php, core/WebStart, core/Setup, core/AutoLoader**, core/vendor, core/LocalSetting (wmf-config)
As far as I can tell, this is entirely dictated by when MediaWiki core "includes" the local StartProfiler file, which happens from Setup.php after including AutoLoader.
I'm curious if we could move this earlier because as far as I can tell, there is already nothing useful available to StartProfiler's execution context (no settings, no vendor, no global functions).
The only thing StartProfiler does (from MediaWiki's perspective) is assign a standalone scalar value to a global variable `$wgProfiler`. No classes or constants are used here. And yet it is the fourth thing included by Setup.php, which in turn is the first thing included by `WebStart.php`, which in turn is the first thing included by entry points.
```lang=php,name=Setup.php
require_once "$IP/includes/profiler/ProfilerFunctions.php";
require_once "$IP/includes/AutoLoader.php";
require_once "$IP/includes/Defines.php";
$wgProfiler = [];
if ( file_exists( "$IP/StartProfiler.php" ) ) {
require "$IP/StartProfiler.php";
}
```
Steps:
1. {icon check color=green} Moving the inclusion of `StartProfiler.php` in MediaWiki core from being the fourth include, to being the first include. – 25d3185dde7631a24ec5833f045658aa6aa2ad4c
2. {icon check color=green} Factor out the profiling code in wmf-config from StartProfile.php into a separate profiler.php file. – 96de34769d97
3. {icon check color=green} Call `xhprof_enable` directly from profiler.php, instead of currently where we wait for MediaWiki to initialise the Profiler class, which then enables Xhprof. This cannot feasibly be done from within MediaWiki and must be done within wmf-config. – 6f3214eafda6
4. [ ] Update Wikimedia's HHVM/PHP configuration to include profiler.php via a `auto_prepend_file`.
This last step will make it so that our profiles include mediawiki-core's Setup.php, WebStart.php, index.php, as well as the steps before that, from multiversion entrypoints and wmf-config glue.