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

Đang mở
#11 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
php
Lĩnh vực
performance, testing

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
PHP
Star
83
Fork
0
Merge trung bình
1 giờ 34 phút
Pull request đã merge (30 ngày)
11

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của phpstan/tally

Tất cả issue của phpstan/tally

Issue tương tự

Thêm issue về PHP

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.