Skip to content

perf(engine): create the parser and the name resolver on the first parse - #238

Merged
lisachenko merged 1 commit into
masterfrom
perf/lazy-parser
Oct 6, 2026
Merged

lisachenko merged 1 commit into
masterfrom
perf/lazy-parser

Conversation

@lisachenko

Copy link
Copy Markdown
Member

What and why

Part of goaop/framework#730.

src/bootstrap.php is a composer files autoload entry, so it runs ReflectionEngine::init(new ComposerLocator()) on every request of every application that has this package installed. init() built the PHP-Parser parser, the emulative lexer and the name-resolving traverser right away. Most requests never parse anything; in goaop/framework, for example, parsing only happens when the transformed-code cache misses.

Change

  • init() only stores the locator and the optional grammar version. It also resets any previously created parser and traverser, so a re-init() with another PhpVersion still takes effect.
  • getParser() creates the parser on first use. A new private getTraverser() does the same for the traverser. parseFile() uses both.
  • ComposerLocator stays eager on purpose. It looks up composer's own ClassLoader in spl_autoload_functions(), and goaop/framework later wraps that loader, after which a lazy lookup would find no ClassLoader.
  • $parser and $traverser become nullable (?Parser = null, ?NodeTraverser = null) so that init() can reset them.

Measured. goaop/framework app, PHP 8.5, opcache with validate_timestamps=0, JIT off, built-in server, median of 200 warm requests. require vendor/autoload.php takes 229–242 µs → 87–91 µs, about 145 µs saved per request.

Backport. The 4.x branch, which serves PHP 8.4 installs, has the same eager init(). A cherry-pick of this commit applies to it.

Tests

  • New tests/ReflectionEngineTest.php:
    • init() creates no parser and no traverser;
    • the parser is created once, on first use;
    • the PhpVersion given to init() is honoured: an 8.4 grammar rejects the pipe operator;
    • parseFile() creates both and resolves names.
  • Full suite: 13827 tests OK, with the same 136 skipped and 2 incomplete as master. vendor/bin/phpstan analyse src: no errors.

🤖 Generated with Claude Code

https://claude.ai/code/session_019bYFRwMWKndKaj6SemwDn2


Generated by Claude Code

The composer "files" bootstrap of this package calls ReflectionEngine::init()
on every request of an application that installs it, and init() built the
PHP-Parser parser, lexer and name resolving traverser right away. Most
requests never parse anything: in goaop/framework, parsing only happens on
a cache miss.

init() now only stores the locator and the grammar version, getParser()
and parseFile() create the parser and the traverser on first use. The
ComposerLocator stays eager on purpose: it must find composer's own class
loader before other loaders wrap it.

Warm request with opcache (built-in server, median of 200): requiring
vendor/autoload.php of goaop/framework takes 87-91 instead of 229-242 µs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019bYFRwMWKndKaj6SemwDn2
@lisachenko
lisachenko marked this pull request as ready for review October 5, 2026 13:28
@lisachenko
lisachenko merged commit a2e42b7 into master Oct 6, 2026
7 checks passed
@lisachenko
lisachenko deleted the perf/lazy-parser branch October 6, 2026 21:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants