Skip to content

Skip env resolution in Config::isEnabled() when no override is set - #131

Merged
rcalicdan merged 1 commit into
typephp-php:mainfrom
naxvog:perf/config-is-enabled-fast-path
Nov 11, 2026
Merged

rcalicdan merged 1 commit into
typephp-php:mainfrom
naxvog:perf/config-is-enabled-fast-path

Conversation

@naxvog

@naxvog naxvog commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Every runtime check calls Config::isEnabled(). On each call it resolved TYPEPHP_DISABLE and TYPEPHP_ENABLED through getEnvValue() and then trimmed and filtered the results, even when neither override is set, which is the normal case.

Change

Probe for the overrides first ($_ENV, $_SERVER, getenv(), defined()), and run the existing resolution only when one is present.

The probes still run on every call. Values that Dotenv or putenv() set after boot keep taking effect, which the existing "Laravel Dotenv compatibility" tests cover. Caching the result was not an option for that reason.

Measurements

PHP 8.4, no opcache, 2M iterations, best of 5, three interleaved runs:

before after
Config::isEnabled() 192 ns 139 ns
function with @param positive-int and @return positive-int 3155 ns 2986 ns
function with @param list<int> (20 items) 2455 ns 2376 ns

That is about 5% and 3% per instrumented call. The remaining cost is the two getenv() calls, which cannot be dropped without changing behaviour.

Verification

  • New test for the putenv() path of both variables in tests/Internal/Util/ConfigTest.php. The existing $_ENV and $_SERVER tests pass unchanged.
  • Full suite on PHP 8.4: 2582 passed. PHPStan: no errors. Pint: no new findings.

@codecov

codecov Bot commented Nov 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.11765% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Internal/Util/Config.php 94.11% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Every runtime check calls Config::isEnabled(), which resolved
TYPEPHP_DISABLE and TYPEPHP_ENABLED through getEnvValue() and trimmed and
filtered them on each call, even when neither is set.

Probe for the overrides first and only resolve them when one is present.
The probes still run on every call, so values that Dotenv or putenv()
set after boot keep taking effect.

PHP 8.4, 2M iterations, best of 5:
  Config::isEnabled()                 192 ns -> 139 ns
  call with @param/@return contract  3155 ns -> 2986 ns
@naxvog
naxvog force-pushed the perf/config-is-enabled-fast-path branch from 65cd450 to 296a002 Compare November 11, 2026 08:35
@rcalicdan
rcalicdan merged commit bdeb54d into typephp-php:main Nov 11, 2026
15 checks passed
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.

2 participants