array_key_exists on $GLOBALS takes linear time in PHP 8.1.0+
まだ誰も着手していません。
- 主要言語
- C
- スター
- 40.4k
- フォーク
- 8.2k
- 平均マージ
- 2日 13時間
- マージ済み PR(30日)
- 96
説明
Description
Compare the following in PHP 8.0.22 vs 8.1.9 (https://3v4l.org/91CFl)
<?php
for ($i = 0; $i < 100000; $i++) {
$GLOBALS["a$i"] = rand(0, 1000000);
}
$start = microtime(true);
for ($runs = 1000; $runs > 0; $runs--) {
array_key_exists('a1111111', $GLOBALS);
}
$end = microtime(true);
printf("[GLOBALS]array_key_exists() took %.5f seconds\n", $end - $start);
It takes 0.00002 seconds in 8.0.22, and 1.9 seconds in 8.1.9.
Further testing makes it clear that the speed of array_key_exists on $GLOBALS increases linearly with the size of $GLOBALS.
This regression doesn't affect array_key_exists on non-$GLOBALS arrays, and doesn't affect the speed of isset($GLOBALS[$foo]).
The commit that introduced the slowdown was https://github.com/php/php-src/commit/3c68f38fdaec7a6f81f986a639bdd29716cf8f22, which restricted usage of $GLOBALS in various ways. I'm not sure if this particular side effect was known or intended.
array_key_exists is used somewhat often on $GLOBALS in MediaWiki, most prominently here.
The behavior can be mostly replaced by calling isset($GLOBALS[$foo]), which remains fast, although that has different semantics when the value is null. I'm not aware of a better way to get the full behavior of array_key_exists($foo, $GLOBALS) in constant-time in 8.1+.
PHP Version
PHP 8.1.9
Operating System
Ubuntu 20.04
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Issue にある PHP 8.0.22/8.1.9 のベンチマークを再現し、その後、$GLOBALS の使用を制限した commit 3c68f38fdaec7a6f81f986a639bdd29716cf8f22 を調査してください。array_key_exists($foo, $GLOBALS) が $GLOBALS のサイズに対して線形にスケールしなくなり、同時に null 値に関するセマンティクスを維持していれば完了です。結果を isset の動作と比較してください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- c, php
- 領域
- backend, performance
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100