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

Aperta
#11 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
php

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
PHP
Stelle
83
Fork
0
Merge medio
1h 34m
PR unite (30g)
11

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di phpstan/tally

Tutte le issue di phpstan/tally

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.