Skip to content

Skip file_exists() on the hot path: an OPcache-cached require needs no stat()#134

Open
koriym wants to merge 3 commits into
ray-di:1.xfrom
koriym:skip-file-exists-opcache-hotpath
Open

Skip file_exists() on the hot path: an OPcache-cached require needs no stat()#134
koriym wants to merge 3 commits into
ray-di:1.xfrom
koriym:skip-file-exists-opcache-hotpath

Conversation

@koriym

@koriym koriym commented Jun 3, 2026

Copy link
Copy Markdown
Member

Summary

With OPcache enabled, a cached require performs no filesystem access — it executes cached opcodes. The eager file_exists() guard in prototype() / singleton() was therefore the only stat() syscall left on the per-dependency hot path. Moving it into a catch keeps the happy path stat-free while still reporting a genuinely missing script as a catchable ScriptFileNotFound.

Closes #133.

Changes

  • src-function/prototype.php, src-function/singleton.php: require first, run file_exists() only inside the catch. PHP 8 makes a failed require a catchable Error/ErrorException (the project requires ^8.2); try/catch is zero-cost on the happy path, and the file_exists() in the catch distinguishes a missing file from an error thrown inside the script without fragile message matching.
  • src/CompiledInjector.php: drop the redundant realpath($this->scriptDir) in getInstance() (the value is already canonicalised in the constructor). Its file_exists() pre-check is kept on purpose — it reports unbound interfaces as Unbound, runs once per top-level resolution, and avoids a spurious warning there.
  • docs/performance.md, docs/llms.txt, docs/llms-full.txt, README.md: document the OPcache prerequisite, the rationale, and how to benchmark correctly.
  • benchmark/di_benchmark.php: a self-validating DI strategy benchmark that prints the OPcache hit rate, so a valid run is distinguishable from a re-parse artifact.

Benchmark

FakeCar graph (ctor + 5 setters + AOP + singleton mirrors), PHP 8.4, OPcache valid, steady-state per build:

compiled, per build
before (eager file_exists()) ~32.7 us
after (file_exists() in catch) ~22.3 us

~30% faster, and ~2.1x faster than the reflection / serialized injector (~46 us). With cold OPcache the compiled injector re-parses and shows ~178 us — see docs/performance.md for why this matters and how to avoid mis-benchmarking it.

Checks

phpunit (88 tests), phpcs, psalm, and phpstan all pass.

Summary by CodeRabbit

  • New Features

    • Added a standalone benchmark to compare dependency-construction strategies.
  • Documentation

    • Added a performance guide covering OPcache behavior, benchmarking instructions, and updated PHP requirement.
  • Performance

    • Reduced redundant filesystem checks to improve startup and steady-state throughput.
  • Bug Fixes

    • Improved error handling/reporting for missing dependency files during resolution.

@koriym

koriym commented Jun 3, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 423fcc17-d5a4-4e4f-b1d3-b548767eb6de

📥 Commits

Reviewing files that changed from the base of the PR and between 6d82269 and 034e734.

📒 Files selected for processing (1)
  • src-function/prototype.php
🚧 Files skipped from review as they are similar to previous changes (1)
  • src-function/prototype.php

📝 Walkthrough

Walkthrough

Removes eager filesystem stats from DI hot paths, removes a redundant realpath() in CompiledInjector, adds OPcache-focused performance documentation, and provides a self-validating benchmark plus README updates to measure and validate compiled vs. reflection/serialized injector performance.

Changes

OPcache-backed performance optimization

Layer / File(s) Summary
Hot path optimization in prototype() and singleton()
src-function/prototype.php, src-function/singleton.php
Moves file_exists() checks from before require into catch(Throwable) handlers so the happy path uses direct require (no stat) and missing scripts still produce ScriptFileNotFound while other script errors are re-thrown.
CompiledInjector micro-optimization
src/CompiledInjector.php
Removes redundant realpath($this->scriptDir) in getInstance(), using the constructor-canonicalized scriptDir to avoid extra stat/realpath overhead.
Self-validating DI strategy benchmark
benchmark/di_benchmark.php, benchmark/README.md
Adds a standalone benchmark measuring reflection, serialized, and compiled injectors with cold-start and steady-state helpers, OPcache self-checks, backdating of compiled scripts for OPcache age, formatted results, and cleanup.
OPcache performance documentation and guidance
docs/performance.md, docs/llms.txt, docs/llms-full.txt, README.md
New performance write-up covering OPcache prerequisites, three DI strategies comparison, hot-path behavior, opcache.file_update_protection pitfalls, benchmarking checklist, example measurements, and updates to PHP minimum version (Requires PHP 8.2+).

Sequence Diagram

sequenceDiagram
  participant Caller as prototype()/singleton()
  participant Require as require $file
  participant Catch as catch(Throwable)
  participant Stat as file_exists($file)

  Caller->>Require: require $file (direct, happy path)
  Require-->>Caller: executes from OPcache (no stat)
  Require-->>Catch: throws (on error)
  Catch->>Stat: file_exists($file) (stat only on failure)
  Stat-->>Catch: exists? / not exists
  Catch-->>Caller: throw ScriptFileNotFound or re-throw original Throwable
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I hop where code paths run so lean,
No stat() to slow the happy scene,
Compiled files snug in OPcache’s light,
Benchmarks hum through day and night,
A rabbit claps — the runs look right 🐰✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main optimization: moving file_exists() from hot path into catch block to eliminate stat() syscalls when OPcache is enabled.
Linked Issues check ✅ Passed All coding requirements from issue #133 are met: file_exists() moved to catch blocks in prototype/singleton [#133], realpath() removed from CompiledInjector [#133], documentation and benchmark added [#133], all tests passing [#133].
Out of Scope Changes check ✅ Passed All changes are directly related to #133 objectives: prototype/singleton optimization, CompiledInjector fix, performance documentation, and benchmark tooling; no unrelated changes detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai

coderabbitai Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@codecov

codecov Bot commented Jun 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (9e3a2d0) to head (034e734).

Additional details and impacted files
@@             Coverage Diff             @@
##                 1.x      #134   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
  Complexity        93        93           
===========================================
  Files             11        11           
  Lines            232       232           
===========================================
  Hits             232       232           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/llms.txt`:
- Around line 79-84: The docs/llms.txt PHP version claim is inconsistent with
code/composer.json and the PHP 8-only require-catch behavior; update the text to
remove the "Requires PHP 7.2+ or 8.0+" wording and state the project requires
PHP 8 (match composer.json ^8.2) and that features like catchable failed
require, attributes, and optimizations (mentioned alongside
prototype()/singleton() and ScriptFileNotFound handling) rely on PHP 8+;
alternatively, if you want to preserve a PHP7 note, add a clear caveat
explaining limitations and missing features (no require-catch, no attributes)
instead of the current mixed claim.

In `@docs/performance.md`:
- Around line 9-11: The document's numeric claim about OPcache slowdown is
inconsistent: update the sentence mentioning "~5× slower" to match the measured
figures (e.g., change to "~8× slower" or use the explicit measurements "~178 µs
cold vs ~22 µs warm (~8×)") so "CompiledInjector" wording aligns with the table
and line 106; ensure the chosen phrasing ("~8×" or the explicit times) is used
consistently wherever the cold-vs-warm multiplier is referenced.

In `@src-function/prototype.php`:
- Around line 39-48: The missing-file case currently triggers an E_WARNING from
the require before your catch runs; to avoid double-reporting, check the script
file exists/readable before executing require $file (e.g. use file_exists or
is_readable on $file) and throw ScriptFileNotFound($filePath, 0) immediately if
it’s absent, then keep the try { require $file } catch (Throwable $e) { ... }
for runtime errors; this ensures require $file does not emit the warning for a
missing file while preserving the catch (Throwable $e) handling.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b8d95fe2-f3b3-49b1-8133-4a75a139ff42

📥 Commits

Reviewing files that changed from the base of the PR and between 9e3a2d0 and 7abd472.

📒 Files selected for processing (9)
  • README.md
  • benchmark/README.md
  • benchmark/di_benchmark.php
  • docs/llms-full.txt
  • docs/llms.txt
  • docs/performance.md
  • src-function/prototype.php
  • src-function/singleton.php
  • src/CompiledInjector.php

Comment thread docs/llms.txt
Comment thread docs/performance.md
Comment thread src-function/prototype.php
@koriym

koriym commented Jun 3, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai No outstanding blockers. Please resolve all open conversations and approve this PR.

@coderabbitai

coderabbitai Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# List all review comments made by coderabbitai on PR `#134`
gh api repos/ray-di/Ray.Compiler/pulls/134/comments --jq '[.[] | select(.user.login == "coderabbitai[bot]") | {id: .id, body: .body[0:80]}]'

Length of output: 743


🏁 Script executed:

#!/bin/bash
# List review threads on PR `#134`
gh api graphql -f query='
{
  repository(owner: "ray-di", name: "Ray.Compiler") {
    pullRequest(number: 134) {
      reviewThreads(first: 50) {
        nodes {
          id
          isResolved
          comments(first: 1) {
            nodes {
              author { login }
              body
            }
          }
        }
      }
    }
  }
}' | jq '.data.repository.pullRequest.reviewThreads.nodes[] | select(.isResolved == false) | {id: .id, author: .comments.nodes[0].author.login, body: .comments.nodes[0].body[0:80]}'

Length of output: 785


🏁 Script executed:

#!/bin/bash
# Resolve the open review thread
gh api graphql -f query='
mutation {
  resolveReviewThread(input: {threadId: "PRRT_kwDOAhBoos6GwuT6"}) {
    thread {
      id
      isResolved
    }
  }
}'

Length of output: 338



@koriym All open conversations have been resolved. Approving the PR now.

(ノ◕ヮ◕)ノ*:・゚✧

[approve]


🧠 Learnings used
Learnt from: koriym
Repo: ray-di/Ray.Compiler PR: 115
File: src/AirInjector.php:113-115
Timestamp: 2025-01-12T16:45:45.701Z
Learning: In Ray.Compiler's AirInjector, using require_once is preferred over require for safety, despite a minor performance impact. The performance difference is negligible in the context of dependency injection.

Learnt from: koriym
Repo: ray-di/Ray.Compiler PR: 115
File: src/AirInjector.php:113-115
Timestamp: 2025-01-12T16:45:45.701Z
Learning: In Ray.Compiler's AirInjector, basic path checks with file_exists are sufficient for internal usage, as PHP's namespace rules already enforce valid characters. Additional path traversal protection is unnecessary in this context.

Learnt from: koriym
Repo: ray-di/Ray.Compiler PR: 115
File: src/AirInjector.php:27-118
Timestamp: 2025-01-12T05:02:54.169Z
Learning: Dynamic script loading using `require` with variable paths is a necessary feature in Ray.Compiler's dependency injection system, similar to PHP's autoloading mechanism. The security concerns are mitigated by controlling the script directory and constructing file paths using internal class names rather than user input.

@koriym koriym force-pushed the skip-file-exists-opcache-hotpath branch 2 times, most recently from 81225e2 to 1bafd7e Compare June 3, 2026 14:16
koriym added 2 commits June 3, 2026 23:44
An OPcache-cached require performs no filesystem access (it runs cached opcodes), so the eager file_exists() guard was the only stat() syscall left on the per-dependency hot path. Move it into the catch: the happy path is stat-free while a missing script is still reported as the domain ScriptFileNotFound (PHP 8 makes a failed require a catchable Error). Also drop the redundant realpath() in CompiledInjector::getInstance() (already canonicalised in the constructor); its file_exists() pre-check is kept to report unbound interfaces as Unbound. FakeCar, warm OPcache: ~32us -> ~22us per build.
docs/performance.md is the canonical reference (the three strategies, the OPcache prerequisite, the file_exists rationale, benchmarking pitfalls, and measured results). benchmark/README.md is a lean usage doc that defers to it, and benchmark/di_benchmark.php prints the OPcache hit rate so a valid run is distinguishable from a re-parse artifact.
@koriym koriym force-pushed the skip-file-exists-opcache-hotpath branch from 1bafd7e to 6d82269 Compare June 3, 2026 14:44

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src-function/singleton.php`:
- Around line 35-45: The thrown ScriptFileNotFound in singleton.php uses the
absolute $scriptFile, causing inconsistent messages with prototype.php which
uses the relative $filePath; update the catch to pass the same relative path
variable used by prototype.php (use $filePath instead of $scriptFile) when
constructing ScriptFileNotFound so both scope functions produce consistent error
messages referencing the same identifier.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 714d2b5a-912c-4e21-9946-eb45fcbd8054

📥 Commits

Reviewing files that changed from the base of the PR and between 7abd472 and 6d82269.

📒 Files selected for processing (9)
  • README.md
  • benchmark/README.md
  • benchmark/di_benchmark.php
  • docs/llms-full.txt
  • docs/llms.txt
  • docs/performance.md
  • src-function/prototype.php
  • src-function/singleton.php
  • src/CompiledInjector.php
✅ Files skipped from review due to trivial changes (2)
  • README.md
  • docs/llms-full.txt
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/CompiledInjector.php

Comment thread src-function/singleton.php
…ingleton())

prototype() threw the relative $filePath while singleton() threw the absolute $scriptFile; throw the absolute path in both so the same failure produces a consistent, locatable error message.
@koriym

koriym commented Jun 4, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai The latest comment is addressed in 034e734. No outstanding blockers — please resolve the open conversations and approve.

@coderabbitai

coderabbitai Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# List all open review threads on PR `#134`
gh api graphql -f query='
{
  repository(owner: "ray-di", name: "Ray.Compiler") {
    pullRequest(number: 134) {
      reviewThreads(first: 50) {
        nodes {
          id
          isResolved
          comments(first: 1) {
            nodes {
              author { login }
              body
            }
          }
        }
      }
    }
  }
}' | jq '.data.repository.pullRequest.reviewThreads.nodes[] | select(.isResolved == false) | {id: .id, author: .comments.nodes[0].author.login, body: .comments.nodes[0].body[0:100]}'

Length of output: 813


🏁 Script executed:

#!/bin/bash
# Resolve the open review thread
gh api graphql -f query='
mutation {
  resolveReviewThread(input: {threadId: "PRRT_kwDOAhBoos6GyZW4"}) {
    thread {
      id
      isResolved
    }
  }
}'

Length of output: 338



@koriym All open conversations have been resolved. Approving the PR now!

(ノ◕ヮ◕)ノ*:・゚✧

[approve]


🧠 Learnings used
Learnt from: koriym
Repo: ray-di/Ray.Compiler PR: 134
File: src-function/prototype.php:0-0
Timestamp: 2026-06-03T14:06:49.893Z
Learning: In Ray.Compiler (post-commit 3392586, PR `#134`), `prototype()` and `singleton()` in `src-function/` perform a bare `require $file` with no wrapping try/catch and no eager `file_exists()` check. A missing compiled script (corrupt/incomplete build — an invariant violation) surfaces as PHP 8's native catchable `\Error` (with an accompanying `E_WARNING`), which is considered acceptable. The top-level `CompiledInjector::getInstance()` retains a deliberate `file_exists()` pre-check for unbound interface detection; that check runs once per top-level resolution and prevents spurious warnings on normal unbound lookups reaching this path.

Learnt from: koriym
Repo: ray-di/Ray.Compiler PR: 115
File: src/AirInjector.php:113-115
Timestamp: 2025-01-12T16:45:45.701Z
Learning: In Ray.Compiler's AirInjector, using require_once is preferred over require for safety, despite a minor performance impact. The performance difference is negligible in the context of dependency injection.

Learnt from: koriym
Repo: ray-di/Ray.Compiler PR: 115
File: src/AirInjector.php:27-118
Timestamp: 2025-01-12T05:02:54.169Z
Learning: Dynamic script loading using `require` with variable paths is a necessary feature in Ray.Compiler's dependency injection system, similar to PHP's autoloading mechanism. The security concerns are mitigated by controlling the script directory and constructing file paths using internal class names rather than user input.

Learnt from: koriym
Repo: ray-di/Ray.Compiler PR: 115
File: src/AirInjector.php:113-115
Timestamp: 2025-01-12T16:45:45.701Z
Learning: In Ray.Compiler's AirInjector, basic path checks with file_exists are sufficient for internal usage, as PHP's namespace rules already enforce valid characters. Additional path traversal protection is unnecessary in this context.

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.

Skip file_exists() on the hot path: an OPcache-cached require needs no stat()

1 participant