Skip to content

perf: release syntax trees once objects are described - #42

Open
artygrand wants to merge 1 commit into
pestphp:5.xfrom
artygrand:release-syntax-trees
Open

artygrand wants to merge 1 commit into
pestphp:5.xfrom
artygrand:release-syntax-trees

Conversation

@artygrand

Copy link
Copy Markdown

What:

  • Bug Fix
  • New Feature

Description:

Every object description keeps the full php-parser syntax tree of its file (stmts), and ObjectsRepository keeps the descriptions until the process ends. Once the description is built, only one place reads the tree: Blueprint::getUsagePathAndLines(), which uses it to find the line of a violation. The tree takes up most of a description's memory. On a Laravel 13 app with 1,609 files under app/ and database/, the descriptions take 198 MB, and 185 MB of that is the trees.

With this change, ObjectDescriptionFactory empties stmts once the object is described, as ObjectsStorage in ta-tikoma/phpunit-architecture-test already does. When a test fails, getUsagePathAndLines() parses that one file again. Passing runs don't parse anything extra.

Numbers come from that app on PHP 8.5.11 with opcache off in the CLI. The single expectation was measured with memory_get_peak_usage(true). The suite figure is the highest memory PHPUnit's event log reports after a test.

expect('App\Providers')->not->toBeUsed() alone 19 arch tests in 3 files, including the php and security presets
Before 290 MB 403 MB
After 100 MB 108 MB

I added a test that checks a described object no longer holds the tree. The existing toThrowArchitectureViolation tests, such as the native functions in ToBeUsedInNothing, check that a violation still reports the right file and line. The 5 tests that already fail on 5.x and Rector's change to src/Support/UserDefinedFunctions.php fail with or without this change.

This works separately from #41, which stops the same file from being described once per namespace. This change makes each description small instead. They touch different files and apply cleanly together. With both applied, the suite above peaks at 123 MB.

Related:

pestphp/pest#1947, #41

🤖 Generated with Claude Code

Every object description kept the full syntax tree of its file for the
rest of the run, though after describing the object only
Blueprint::getUsagePathAndLines() reads it, to locate a violation. The
factory now empties stmts, and the violation lookup parses that one file
again when a test fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@artygrand

Copy link
Copy Markdown
Author

The red checks here aren't from this change: every job stops at composer update, before any test runs:

pestphp/pest ^5.0.0 requires pestphp/pest-plugin-arch ^5.0.0
but pestphp/pest-plugin-arch dev-<sha> is the root package and cannot be modified

pestphp/pest and this plugin require each other. In CI the checkout is the root package with version dev-<sha>, which doesn't satisfy ^5.0.0. Pushes to 5.x and the 5.0.0 tag fail the same way, and so do #41 and the dependabot PRs.

Either of these should fix it:

  • set COMPOSER_ROOT_VERSION: 5.x-dev in the env of the composer update step in .github/workflows/tests.yml;
  • or add "extra": { "branch-alias": { "dev-5.x": "5.x-dev" } } to composer.json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant