Skip to content

Memoize Intervals::compactConstraint() results - #194

Merged
Seldaek merged 1 commit into
composer:mainfrom
Seldaek:memoize-compact-constraint
Oct 29, 2026
Merged

Seldaek merged 1 commit into
composer:mainfrom
Seldaek:memoize-compact-constraint

Conversation

@Seldaek

@Seldaek Seldaek commented Oct 29, 2026

Copy link
Copy Markdown
Member

Composer's `PoolOptimizer` calls `compactConstraint()` for every require and conflict link in the pool. On laravel/laravel that is about 58k calls for a few hundred distinct constraints (see composer/composer#13174). This caches the compacted result per constraint string, the same key `Intervals::get()` already uses. `clear()` frees it too.

Time spent in `PoolOptimizer::optimize()` on laravel/laravel, best of 15 in-process runs, median of 4 rounds:

optimize()
semver 3.5.2 0.449 s
this PR 0.133 s
composer/composer#13174 (cache in PoolOptimizer) 0.123 s

The resulting lock files are identical. Doing it here also helps the other `compactConstraint()` callers, such as `PoolBuilder` and `ValidatingArrayLoader`.

Long-running processes that compact constraints without calling `Intervals::clear()` will now hold on to the cache, as the class docblock already warns.

Callers like Composer's PoolOptimizer compact the same few hundred constraints
tens of thousands of times. The cache is keyed like the existing intervals
cache and is freed by Intervals::clear().
@Seldaek
Seldaek merged commit 3e9f796 into composer:main Oct 29, 2026
20 checks passed
@Seldaek
Seldaek deleted the memoize-compact-constraint branch October 29, 2026 09:11
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