Remember when large arrays don't contain any collectable elements, and avoid wasting GC time on them
まだ誰も着手していません。
- 主要言語
- C
- スター
- 40.4k
- フォーク
- 8.1k
- 平均マージ
- 2日 13時間
- マージ済み PR(30日)
- 96
説明
Description
Large arrays pay a very large and frequent GC penalty in long-lived applications.
It should be possible to significantly reduce this impact by introducing a flag like GC_MAY_HAVE_COLLECTABLE_ELEMENTS for arrays, and not scanning the array if no such elements are present.
In the following test case, GC runs take several hundred ms with these arrays containing only primitives. If $test is repeatedly ref'd and unref'd, as is the case for long-lived objects in some applications, these huge arrays will repeatedly get scanned for GC-able elements, wasting CPU time.
Of particular interest is the fact that the readonly array is obviously not modifiable, so it can't possibly contain any GC-able elements if it didn't have any the first time it was scanned. Yet if you modify the test case you can easily tell from the length of time the GC spends that it does still repeatedly scan this array.
class Test{
public function __construct(
private readonly array $readonlyArray,
public array $writableArray = []
){}
}
ini_set('memory_limit', '-1');
$test = new Test(array_fill(0, 50_000_000, 0), array_fill(0, 50_000_000, 0));
$ref2 = new \stdClass();
$ref2->test = $test; //increase refcount
unset($test); //decrease refcount, object is now in gc root buffer
$start = hrtime(true);
gc_collect_cycles();
var_dump(number_format(hrtime(true) - $start));
$test = $ref2->test; //increase refcount
unset($ref2->test); //decrease refcount
$start = hrtime(true);
gc_collect_cycles();
var_dump(number_format(hrtime(true) - $start));
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
issueではソースファイルもテストも指定されていません。まず、提供されたPHPスクリプトを再現し、readonly配列と書き込み可能な配列について gc_collect_cycles() を測定します。次に、ガベージコレクターのスキャンに関係する配列処理を追跡します。配列に回収可能な要素が含まれていないことが既知の場合に、収集動作を変更せずに繰り返しスキャンを回避できることを実証できれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- c, php
- 領域
- backend
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100