Object store growth inside the counted call adds 128 KiB and up to 3 % instructions
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 48/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- php
- 领域
- performance, testing
调研方向
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 分钟
- 30 天内合并 PR
- 11
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
phpstan/tally 的其他 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 63/100
phpstan/phpstan-phpunit#361 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
HE-Arc/le-panier-du-coin#49 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 2 天内回复
-
15.0
难度 2/5 1-3 小时 新手友好度 65/100
-
难度 2/5 1-3 小时 新手友好度 78/100
woocommerce/woocommerce-subscriptions-importer-exporter#298 ·