Page MenuHomePhabricator

Adjust MediaWiki to avoid the slowness of array_key_exists on `$GLOBALS` in PHP 8.1+
Closed, ResolvedPublic

Description

FYI: PHP 8.1 made major changes to how $GLOBALS works, and one of the side effects is that using array_key_exists on $GLOBALS is now a linear-time operation instead of constant-time. MediaWiki uses this construct pretty extensively in GlobalVarConfig, which is ~1000 times slower in 8.1 than it is in 8.0, due to $GLOBALS having 1000-2000 entries in it.

The effect of this (in our testing on runescape.wiki, at least) is that total parse time silently went up about 50% when switching from 8.0 to 8.1, due to many thousands of calls to GlobalVarConfig::get, which now takes about 20 microseconds each.

I have filed a bug against PHP core about this. In the mean time, changing GlobalVarConfig::get to

isset( $GLOBALS[$var] ) || array_key_exists( $var, $GLOBALS );

seems to reduce the time spent in array_key_exists back down to pre-8.1 levels (note that just using isset on its own won't quite work if the value is null). If you are currently using PHP 8.1 in production, I recommend trying this out.

Filing as a stand-alone task so we don't forget.

Event Timeline

Change 832619 had a related patch set uploaded (by Jforrester; author: Jforrester):

[mediawiki/core@master] GlobalVarConfig: Avoid slowness of array_key_exists on $GLOBALS in PHP 8.1+

https://gerrit.wikimedia.org/r/832619

Change 832619 merged by jenkins-bot:

[mediawiki/core@master] GlobalVarConfig: Avoid slowness of array_key_exists on $GLOBALS in PHP 8.1+

https://gerrit.wikimedia.org/r/832619

Change 833028 had a related patch set uploaded (by Jforrester; author: Jforrester):

[mediawiki/core@REL1_39] GlobalVarConfig: Avoid slowness of array_key_exists on $GLOBALS in PHP 8.1+

https://gerrit.wikimedia.org/r/833028

Change 833028 merged by jenkins-bot:

[mediawiki/core@REL1_39] GlobalVarConfig: Avoid slowness of array_key_exists on $GLOBALS in PHP 8.1+

https://gerrit.wikimedia.org/r/833028