Skip to content

Do not keep in memory what result cache symbol tracking does not need - #7057

Merged
ondrejmirtes merged 3 commits into
2.3.xfrom
bench-memory-symbol-tracking
Nov 13, 2026
Merged

ondrejmirtes merged 3 commits into
2.3.xfrom
bench-memory-symbol-tracking

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

The Benchmarks workflow on 2.3.x started failing at 2418876: bug-13218 peakMemory went from 39.2 MB to 43.2 MB (+10.2 %, the limit is ±10 %), and every other benchmark grew by about 2.5 MB. Bisecting locally put the growth in three commits. Each is fixed here in its own commit, and the result cache tracks what it did before:

  • 2decb5f (~2.2 MB, bug-13218): ObjectType::isOffsetAccessible() asked ClassReflection::is() about each of EXTRA_OFFSET_CLASSES. Since lookups are tracked, each class found was expanded like a referenced class, which resolved its PHPDoc from the whole PhpStorm stub file (intl.php alone is ~835 KB). The answer depends only on the class's ancestors, which are already tracked, so it now finds the class among them, with the same result as is(). The turbo-ext mirror is changed the same way. This also drops four "missing" value dependencies per file (Dom\NamedNodeMap, Dom\NodeList, Dom\HTMLCollection, Dom\DtdNamedNodeMap).
  • 8435dd9 (~2.0 MB, every benchmark): OptimizedDirectorySourceLocatorGroup kept a merged copy of the symbol maps of all its locators. It now checks each locator's own maps, which are shared rather than copied. Locally, 20,000 names declared in none of 44 locators take ~21 ms, against ~4.6 ms with the merged map and ~190 ms when asking each locator in turn (before 8435dd9).
  • ccd495a (~0.35 MB): CachedPhpInternalSourceLocator::isStubFunction() kept a lowercased copy of all PhpStorm stub functions. It now looks up the stub map directly and keeps only the 335 names that aren't lowercase there.

Local peak memory, from tests/bench/measure.php (the second of two runs):

Benchmark cb5fc9e 2418876 this PR
bug-13218 84.26 MB 88.49 MB 82.04 MB
bug-1388 82.77 MB 85.37 MB 83.00 MB
bug-12159 103.77 MB 106.38 MB 104.01 MB
or-chain-falsey-blowup 83.56 MB 86.22 MB 83.85 MB

tests/bench/expected.neon is unchanged.

Verified locally: the full test suite with and without the turbo extension loaded, the turbo strict build with smoke.php and the side-by-side and signature parity checks, bin/phpstan, phpcs, and the result-cache e2e suites for symbol lookups, missing classes, autoload entries, vendor classes and dependency types.

…s not extend

ObjectType::isOffsetAccessible() asked ClassReflection::is() about each of
EXTRA_OFFSET_CLASSES - DOMNodeList, ResourceBundle, PDORow, FFI\CData and
others - for every object type that is not an ArrayAccess. That looked each
class up, and every lookup is a dependency of the analysed file now: a
class found was expanded like a referenced one, resolving the PHPDoc of the
built-in class from its PhpStorm stub file and keeping it in memory, and
a class missing, like Dom\NodeList, became a value dependency of nearly
every file.

The answer depends only on the ancestors of the class, which are tracked
already, so it is found among them instead, with the same result as is().

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d4f2-262c-75e2-9f82-27a5c05cebf6
OptimizedDirectorySourceLocatorGroup built a map of the symbols of all its
locators, a second copy of every class, function and constant name for the
rest of the process. It checks the maps of the locators themselves now,
shared instead of copied, and still asks only the locators declaring the
name.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d4f2-262c-75e2-9f82-27a5c05cebf6
Nearly all of them are lowercase in PhpStormStubsMap already and are looked
up there directly; only the others are kept lowercased.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d4f2-262c-75e2-9f82-27a5c05cebf6
@ondrejmirtes
ondrejmirtes merged commit e3814db into 2.3.x Nov 13, 2026
140 of 141 checks passed
@ondrejmirtes
ondrejmirtes deleted the bench-memory-symbol-tracking branch November 13, 2026 13:38
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