composer / semver

Semantic versioning utilities with the addition of version constraints parsing and checking.
MIT License
3.15k stars 76 forks source link

Add a compile method #86

Closed jderusse closed 4 years ago

jderusse commented 4 years ago

This PR adds a Compile method to the constraints to optimize constraints computation.

By caching the compiled code, we will be able to perform much more complex optimization on MultiConstraint https://github.com/composer/semver/pull/84#issuecomment-620502945

By using this new feature in the most used called locations in Composer (Pool, PoolBuilder and ComposerRepository) I get 15% CPU https://blackfire.io/profiles/compare/cfa5e43e-0510-4b24-bd44-722dc0c3c7ce/graph

Not covered by the current PR:

Here is a sample of code generated by the compile method

(version_compare($v, '1.1.0.0', '==')) || (!$b && version_compare($v, '2.0.0.0', '>='))
jderusse commented 4 years ago

Reviewing the compile function is very hard. I've add a "matrix" test that take all case (combining operators and type of version) and compare the result of matches with compile to ensure both implementations are identical

Which triggers 4 errors that sounds legit to me

==3.0-b2 matches >3.0-beta2 => current = true, mine = false
==3.0-b2 matches <3.0-beta2 => current = true, mine = false
==3.0-beta2 matches >3.0-b2 => current = true, mine = false
==3.0-beta2 matches <3.0-b2 => current = true, mine = false
naderman commented 4 years ago

This looks great to me!

codecov-io commented 4 years ago

Codecov Report

Merging #86 into master will decrease coverage by 0.94%. The diff coverage is 88.88%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master      #86      +/-   ##
============================================
- Coverage     96.04%   95.10%   -0.95%     
- Complexity      251      287      +36     
============================================
  Files             7        8       +1     
  Lines           481      552      +71     
============================================
+ Hits            462      525      +63     
- Misses           19       27       +8     
Impacted Files Coverage Δ Complexity Δ
src/Constraint/EmptyConstraint.php 62.50% <0.00%> (-8.93%) 8.00 <1.00> (+1.00) :arrow_down:
src/Constraint/MultiConstraint.php 91.66% <66.66%> (-4.63%) 55.00 <8.00> (+8.00) :arrow_down:
src/CompiledMatcher.php 94.11% <94.11%> (ø) 8.00 <8.00> (?)
src/Constraint/Constraint.php 100.00% <100.00%> (ø) 57.00 <19.00> (+19.00)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update bd21c31...8f687aa. Read the comment docs.

Seldaek commented 4 years ago

Tried this on a few projects and seeing about 20% CPU time reduction on pure dependency solving for ~0.3% memory increase, so totally worth it. Thanks @jderusse and all reviewers for the great work here!