Object store growth inside the counted call adds 128 KiB and up to 3 % instructions

オープン
#11 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
php

調査の方向性

Read the benchmark worker flow around BenchContainer::create() and TypeCombinatorBench::boot(), then trace how the counted worker chooses and loads its container. Reproduce the differing object-store growth with the provided tally worker commands and compare the container files and environment. Done when the counted run uses consistent setup and the affected @types-64 memory results no longer shift by 128 KiB across worker environments.

索引モデルが issue の本文から書いたものです。

説明

When PHP's object store grows during the counted call, the case counts 128 KiB more memory and up to 3 % more instructions. Whether a case crosses that step depends on how many objects the worker created before the call. So the result can change when the benchmark does not.

I found this in phpstan-src at 526b6688e. I ran tally run on x86_64 PHP 8.5.12 (Ubuntu 24.04, ppa:ondrej/php). 456 of 459 cases passed against the committed numbers. Three failed:

Case Memory (CI) Memory (local) Instructions vs CI
TypeCombinatorBench::benchUnionTaggedConstantArrays@types-64 138,544 269,616 +2.16 %
TypeCombinatorBench::benchUnionConstantArrays@types-64 143,440 274,512 +2.87 %
TypeCombinatorBench::benchUnionGeneralArrayAndConstantArrays@types-64 216,576 347,648 +1.59 %
What grows

I loaded a small extension that logs every Zend heap allocation of 3 KiB or more, with the PHP function and line. I ran benchUnionTaggedConstantArrays@types-64 with tally worker, natively, in a passing and in a failing state (see below). In the failing run, the call reallocates a 128 KiB block to 256 KiB at src/Type/TypeCombinator.php:1322, and the block moves:

$nonEmpty = new NonEmptyArrayType();

The same block went from 64 KiB to 128 KiB earlier, while the container was built. In the passing runs, the call has no such reallocation. This suggests that the block is the object handle table. The reallocation happens at a new. The extra memory is also gone when the worker first creates and frees 40,000 objects.

To check this under Valgrind, I added this line after BenchContainer::create() in TypeCombinatorBench::boot() and ran tally run on the @types-64 cases:

$grow = []; for ($i = 0; $i < 40000; $i++) { $grow[] = new \stdClass(); } unset($grow);
Case Memory (CI) Memory (pre-grown) Instructions vs CI
benchUnionTaggedConstantArrays@types-64 138,544 138,544 +0.07 %
benchUnionConstantArrays@types-64 143,440 143,440 +0.08 %
benchUnionGeneralArrayAndConstantArrays@types-64 216,576 216,576 +0.38 %
benchRemoveEnumCases@types-64 370,968 239,896 -1.24 %

So the growth also falls inside the call for benchRemoveEnumCases@types-64 on the CI runner: the committed number is exactly 131,072 bytes above the pre-grown one. The memory of the other TypeCombinatorBench @types-64 cases did not change, apart from benchUnionConstantArraysWithOwnKeys, which dropped by 131,120 bytes. I did not look into that case further.

Why the object count differs

The counted worker does not load the container that the native worker compiled. I logged the container files in boot() during tally run. The counted worker found the native worker's Container_*.php and compiled a second one with another hash. After that, the scratch directory held 5 PHPStan cache files (cache/PHPStan/*.dat). The native worker had none at the same point of its run, so they most likely come from later in the native run.

PHPStan puts the environment variables into the container. The only difference I saw between the two workers is LD_PRELOAD, which Valgrind sets in the counted worker. I did not confirm that this variable alone changes the hash. This also goes against the BenchContainer docblock, which says the counted run loads the container from the scratch directory.

The same switch happens without Valgrind. I ran tally worker three times on one scratch directory: first on an empty directory, then with a different environment, then with that environment again.

                                                  empty   other env   same env again
benchUnionTaggedConstantArrays@types-64           138544  269616      138544
benchUnionConstantArrays@types-64                 143440  274512      143440
benchUnionGeneralArrayAndConstantArrays@types-64  216544  347616      216544
benchRemoveEnumCases@types-64                     239840  370912      239840

These are native numbers, so some differ by a few bytes from the counted ones above. The commands were env -i PATH=/usr/bin:/bin A=1 php vendor/bin/tally worker --case=<id> --scratch=<dir> --result=<file> -- tests/bench/TypeCombinatorBench.php, then the same with B=2, then B=2 again. In the logs, the run on the empty directory grows the table to 256 KiB while it parses, before the call. The run with the other environment compiles the container without parsing, and its table is still 128 KiB when the call starts.

What I did not find

I did not find why the CI runner does not cross the step for the first three cases. The step depends on the object count before the call. So I expect that a new service in the container, or any other change in that count, can move these numbers by 128 KiB. I did not test that.

Possible fixes
  • In Tally: grow the object store before the counted spans, for example after the before methods. This protects all benchmarks, but only up to the size it grows to.
  • In phpstan-src: make the counted worker load the native worker's container, for example with a container hash that does not depend on LD_PRELOAD. This removes the difference between the two workers, but a case can still cross a step when the code before the call changes.

I can open a pull request for either one.

主要言語
PHP
スター
83
フォーク
0
平均マージ
1時間 34分
マージ済み PR(30日)
11

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

phpstan/tally のほかの issue

phpstan/tally の issue をすべて見る

似ている issue

PHP の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。