Skip to content

Commit 487fbf4

Browse files
perf(env): read each process environment value once per reader (#43)
* perf(env): read each process environment value once per reader Every read() and define() called getenv() through externalValue(), so defining the roughly 160 catalog constants scanned the process environment several hundred times per request. Memoize the lookup per name; the reader's own putenv() in compatibility profiles invalidates the entry. * docs(env): state the once-per-request sampling of process values The invalidation in write() was unreachable: compatibility profiles never consult the sampled values. Document the contract and pin it with a test.
1 parent 944d0e2 commit 487fbf4

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎docs/environment.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ when changing that mode; older formats without the field are treated as
105105
compatibility-enabled.
106106

107107
Native payloads persist file-owned values only; process-only values and process
108-
overrides are read live and never serialized. A file default remains available
108+
overrides are read live and never serialized. Within one request the reader
109+
samples each process variable once, on its first read; a `putenv()` made later
110+
in the same request is not observed until the next request. A file default remains available
109111
when a process override is removed. Native runtime caches store interpolation
110112
templates and salted dependency fingerprints, excluding derived process bytes.
111113
An unchanged dependency resolves the template without loading the dotenv parser.

‎src/Env/EnvReader.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ final class EnvReader
4949
private array $external = [];
5050
/** @var array<string, string> */
5151
private array $ownedProcess = [];
52+
/** @var array<string, string|false> */
53+
private array $processValues = [];
5254
/** @var array<string, array{string, bool|int|float|string|null}> */
5355
private array $cache = [];
5456
/** @var array<string, string> */
@@ -640,7 +642,10 @@ private function sentinel(): bool
640642
private function externalValue(string $name): ?string
641643
{
642644
if ($this->profile === 'native' && !str_starts_with($name, 'HTTP_')) {
643-
$process = getenv($name);
645+
// Each process variable is sampled once per reader (one request); every
646+
// read() and define() used to scan the environment again. Compatibility
647+
// profiles never reach this branch, so their putenv() needs no invalidation.
648+
$process = $this->processValues[$name] ??= getenv($name);
644649
if ($process !== false && ($this->ownedProcess[$name] ?? null) !== $process) {
645650
return $process;
646651
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SymPress\Runtime\Tests\Unit;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use SymPress\Runtime\Env\EnvReader;
9+
10+
// phpcs:disable SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable -- The reader owns the superglobal boundary under test.
11+
final class EnvReaderProcessValueTest extends TestCase
12+
{
13+
protected function tearDown(): void
14+
{
15+
putenv('RTV_SAMPLED');
16+
unset($_ENV['RTV_SAMPLED'], $_SERVER['RTV_SAMPLED']);
17+
}
18+
19+
public function testProcessValuesAreSampledOncePerReader(): void
20+
{
21+
unset($_ENV['RTV_SAMPLED'], $_SERVER['RTV_SAMPLED']);
22+
putenv('RTV_SAMPLED=first');
23+
$reader = new EnvReader();
24+
25+
self::assertSame('first', $reader->read('RTV_SAMPLED'));
26+
27+
putenv('RTV_SAMPLED=second');
28+
29+
self::assertSame('first', $reader->read('RTV_SAMPLED'), 'A reader keeps the value sampled on first read.');
30+
self::assertSame('second', (new EnvReader())->read('RTV_SAMPLED'), 'The next reader (request) observes the change.');
31+
}
32+
}

0 commit comments

Comments
 (0)