Skip to content

Commit ecfd672

Browse files
committed
Benchmark parsing PHPDocs with Tally
Tally counts instructions, cycles, and memory under CodSpeed's CPU simulation, so the numbers barely move between CI runs and a regression of a few percent fails the build instead of disappearing in timing noise. The two benchmarks guard the recent performance fixes: - LexerBench lexes every tenth PHPDoc of the dev dependencies: 148ceff (with e59b0c8) took it from 47.39M to 33.42M instructions. - SymfonyConfigReferenceBench parses the config/reference.php that Symfony 7.4 generates, one large array shape full of generics, as PHPStan does with line attributes: 1427af4 took it from 977M to 482M instructions, 3fcdcf0 to 199M. Tally needs PHP 8.2, so it lives in tools/tally like phplrt in tools/phplrt, and tally.php loads the library's autoloader. The Benchmarks workflow runs it on a fixed image and PHP version, because the counts depend on both. The expected numbers come from its first run: it prints a patch that adds them. Amp-Thread-ID: https://ampcode.com/threads/T-01a1d9be-190b-7065-bc04-21b38804941c
1 parent eb688ae commit ecfd672

12 files changed

Lines changed: 3185 additions & 0 deletions

File tree

‎.gitattributes‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
.github export-ignore
55
apigen export-ignore
6+
benchmarks export-ignore
67
phpcs.xml export-ignore
78
doc export-ignore
89
tests export-ignore
@@ -17,3 +18,4 @@ Makefile export-ignore
1718
phpstan-baseline.neon export-ignore
1819
phpstan.neon export-ignore
1920
phpunit.xml export-ignore
21+
tally.php export-ignore

‎.github/workflows/benchmarks.yml‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# https://help.github.com/en/categories/automating-your-workflow-with-github-actions
2+
3+
name: "Benchmarks"
4+
5+
on:
6+
pull_request:
7+
push:
8+
branches:
9+
- "2.4.x"
10+
11+
permissions:
12+
contents: read
13+
14+
concurrency:
15+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
16+
cancel-in-progress: true
17+
18+
jobs:
19+
benchmarks:
20+
name: "Benchmarks"
21+
# Tally's counts depend on the system libraries and the PHP build, so both stay fixed:
22+
# a new image or PHP release means accepting new numbers
23+
runs-on: "ubuntu-24.04"
24+
25+
steps:
26+
- name: Harden the runner (Audit all outbound calls)
27+
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
28+
with:
29+
egress-policy: audit
30+
31+
- name: "Checkout"
32+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
33+
with:
34+
persist-credentials: false
35+
36+
- name: "Install PHP"
37+
uses: "shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962" # v2
38+
with:
39+
coverage: "none"
40+
php-version: "8.5"
41+
extensions: "posix"
42+
43+
- name: "Install dependencies"
44+
run: "composer install --no-interaction --no-progress"
45+
46+
- name: "Install Tally"
47+
run: "make bench-install"
48+
49+
- name: "Run the benchmarks"
50+
run: "php tools/tally/vendor/bin/tally run --output=tmp/tally-results"
51+
52+
- name: "Upload the results"
53+
if: always()
54+
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
55+
with:
56+
name: "tally-results"
57+
path: "tmp/tally-results"

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
/composer.lock
88
.phpunit.result.cache
99
/.amp/portals/*
10+
/tools/tally/vendor

‎CLAUDE.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ make check
198198
- `make lint` - Check PHP syntax only
199199
- `make cs` - Check code style only
200200
- `make phpstan-generate-baseline` - Generate PHPStan baseline (use sparingly)
201+
- `make bench-install`, then `make bench` - Count the benchmarks in `benchmarks/` with [Tally](https://github.com/phpstan/tally) (needs PHP 8.2 on Linux). They cover parsing large and typical PHPDocs. Take their expected numbers from the Benchmarks workflow, not from a local run: a benchmark without numbers gets a patch in the job's output
201202

202203
## Workflow Summary
203204

‎Makefile‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,16 @@ phpstan-generate-baseline:
4545
.PHONY: grammars-install
4646
grammars-install:
4747
composer install --no-interaction --working-dir tools/phplrt
48+
49+
# ---------------------------------------------------------------------------
50+
# The benchmarks
51+
# ---------------------------------------------------------------------------
52+
53+
# Tally is a development dependency of its own too, because it needs PHP 8.2.
54+
.PHONY: bench-install
55+
bench-install:
56+
composer install --no-interaction --working-dir tools/tally
57+
58+
.PHONY: bench
59+
bench:
60+
php tools/tally/vendor/bin/tally run

‎benchmarks/LexerBench.php‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\PhpDocParser\Benchmarks;
4+
5+
use PHPStan\PhpDocParser\Lexer\Lexer;
6+
use PHPStan\PhpDocParser\ParserConfig;
7+
use Tally\Attribute\Before;
8+
use Tally\Attribute\Warmup;
9+
10+
/**
11+
* Lexing typical PHPDocs, which 148ceff made about 40 % quicker by extracting the matches
12+
* with PREG_PATTERN_ORDER after e59b0c8 made the numeric-separator groups non-capturing.
13+
*/
14+
final class LexerBench
15+
{
16+
17+
private Lexer $lexer;
18+
19+
/** @var list<string> */
20+
private array $phpDocs;
21+
22+
#[Before]
23+
public function prepare(): void
24+
{
25+
$this->lexer = new Lexer(new ParserConfig(['lines' => true]));
26+
$this->phpDocs = require __DIR__ . '/data/vendor-docblocks.php';
27+
}
28+
29+
#[Warmup(1)]
30+
public function benchTokenizeTypicalPhpDocs(): void
31+
{
32+
foreach ($this->phpDocs as $phpDoc) {
33+
$this->lexer->tokenize($phpDoc);
34+
}
35+
}
36+
37+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\PhpDocParser\Benchmarks;
4+
5+
use PHPStan\PhpDocParser\Lexer\Lexer;
6+
use PHPStan\PhpDocParser\Parser\ConstExprParser;
7+
use PHPStan\PhpDocParser\Parser\PhpDocParser;
8+
use PHPStan\PhpDocParser\Parser\TokenIterator;
9+
use PHPStan\PhpDocParser\Parser\TypeParser;
10+
use PHPStan\PhpDocParser\ParserConfig;
11+
use Tally\Attribute\Before;
12+
use Tally\Attribute\Warmup;
13+
14+
/**
15+
* Parsing the PHPDocs of a config/reference.php that Symfony 7.4 generates, as PHPStan does:
16+
* one large array shape with many generics and constant types. 1427af4 made it about twice as
17+
* quick by computing the token offsets once, 3fcdcf0 twice as quick again by not walking to
18+
* the end of the PHPDoc for every generic.
19+
*/
20+
final class SymfonyConfigReferenceBench
21+
{
22+
23+
private Lexer $lexer;
24+
25+
private PhpDocParser $phpDocParser;
26+
27+
/** @var list<string> */
28+
private array $phpDocs;
29+
30+
#[Before]
31+
public function prepare(): void
32+
{
33+
$config = new ParserConfig(['lines' => true]);
34+
$constExprParser = new ConstExprParser($config);
35+
$this->lexer = new Lexer($config);
36+
$this->phpDocParser = new PhpDocParser($config, new TypeParser($config, $constExprParser), $constExprParser);
37+
$this->phpDocs = require __DIR__ . '/data/symfony-config-reference.php';
38+
}
39+
40+
#[Warmup(1)]
41+
public function benchParse(): void
42+
{
43+
foreach ($this->phpDocs as $phpDoc) {
44+
$this->phpDocParser->parse(new TokenIterator($this->lexer->tokenize($phpDoc)));
45+
}
46+
}
47+
48+
}

0 commit comments

Comments
 (0)