Skip to content

Avoid concatenated cache keys in CompilingMatcher - #186

Open
Toflar wants to merge 1 commit into
composer:mainfrom
Toflar:feature/optimize-compiling-matcher-cache
Open

Toflar wants to merge 1 commit into
composer:mainfrom
Toflar:feature/optimize-compiling-matcher-cache

Conversation

@Toflar

@Toflar Toflar commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

CompilingMatcher caches match results but currently builds a combined string key on every call, even when the result is already cached.

For example, checking whether 2.1.0 satisfies >=2.0.0:

  1. convert the constraint to a string.
  2. concatenate the operator, constraint and version into one cache key.
  3. look up the result using that key.

Checking the same constraint against 2.2.0 etc. repeats this work, including concatenating the unchanged operator and constraint.

This PR stores results in nested arrays instead:

$cache[$operator]['>= 2.0.0']['2.1.0'] = true;
$cache[$operator]['>= 2.0.0']['2.2.0'] = true;

Each call accesses the operator/constraint bucket by reference, then looks up the version within it. This avoids constructing the combined string key over and over again.
The reference bit might look strange at first, that's why I've added a comment explaining it. It basically lets the lookup and any subsequent write reuse the same bucket and it has a measurable positive impact on performance.

On a real project's composer update, pool optimization took 678 ms instead of 723 ms (~6% faster) so really this complexity is all about optimizing the hot path 😊

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