Stub globals are an idea I came up with to migrate from global dependant code to lazy module initialisation in a fairly painless way. The general idea is:
class StubClass { function __call( $name, $args ) { global $wgSomeObject; if ( get_class( $wgSomeObject ) != 'RealClass' ) { $wgSomeObject = new RealClass; } return call_user_func_array( array( $wgSomeObject, $name ), $args ); } } $wgSomeObject = new StubClass;
This turns out to be quite robust and effective, and I've implemented it in my working copy for $wgLoadBalancer, $wgContLang, $wgUser, $wgOut, $wgParser, $wgMessageCache and $wgAuth. I've managed to arrange it so that all of these globals make it out of Setup.php without being accessed. $wgTitle and $wgArticle have simply been replaced by null until after Setup.php, the exception handling code can now deal with this. This provides a massive time saving for lightweight requests on systems with no opcode cache. My own desktop computer is one such system, and 304 "not modified" responses are 2-3 times faster than they were before. Other requests which benefit are query.php, action=raw and command line scripts such as eval.php. Parser cache hits load pretty much everything, so they don't really benefit.
In my post title "Ideas for faster MediaWiki startup", I also described a change to modules and extensions, allowing their initialisation to be deferred. This has not been done yet, so at the moment, many extensions will access global variables on startup and thus load large amounts of code. The benefit above thus mainly applies to installations with no extensions.
However, everything benefits from the message cache that I described in my previous post. Here is the current working diff with both of those changes included:
http://noc.wikimedia.org/~tstarling/messages-and-stub-globals.diff
File size is 196 KB.
-- Tim Starling
wikitech-l@lists.wikimedia.org