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

Abierto
#11 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
48/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
php

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
PHP
Estrellas
83
Forks
0
Merge medio
1 h 34 min
PR fusionados (30 d)
11

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de phpstan/tally

Todos los issues de phpstan/tally

Issues similares

Más issues de PHP

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.