1
0
Fork 0
mirror of https://github.com/composer/composer synced 2025-05-09 08:32:56 +00:00

Improve memory usage resolving dependencies

It is known that composer update takes a lot of memory: #5915, #5902,

I am playing with a profiler (@blackfireio) to make a demo in my local
PHP meetup (@phpvigo) and I found out a way to use less memory. These
are my first tests:

* Private project using PHP 5.6:
  * Memory: from 1.31GB to 1.07GB
  * Wall Time: from 2min 8s to 1min 33s

* symfony-demo using PHP 7.1 in my old mac book:
  * Memory: from 667MB to 523MB
  * Wall Time: from  5min 29s to 5min 28s

Not use an array inside conflict rules is this improvement main idea:

```php
<?php
//Memory 38MB
gc_collect_cycles();
gc_disable();

class Rule
{
    public $literals;

    public function __construct(array $literals)
    {
        $this->literals = $literals;
    }
}

$rules = array();

$i = 0;
while ($i<80000){ //
    $i++;

    $array = array(-$i, $i);
    $rule = new Rule($array);
    $rules[] = $rule;
}
```

```php
<?php
//Memory 11.1MB
gc_collect_cycles();
gc_disable();

class Rule2Literals
{
    public $literal1;
    public $literal2;

    public function __construct($literal1, $literal2)
    {
        $this->literal1 = $literal1;
        $this->literal2 = $literal2;
    }
}

$rules = array();

$i = 0;
while ($i<80000){ //
    $i++;

    $rule = new ConflictRule(-$i, $i);
    $rules[] = $rule;
}
```

More info https://github.com/composer/composer/pull/6168
This commit is contained in:
rubenrua 2017-02-16 22:00:57 +00:00
parent 363bab90fa
commit 4e1887a721
10 changed files with 263 additions and 126 deletions

View file

@ -0,0 +1,102 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\DependencyResolver;
use Composer\Package\PackageInterface;
use Composer\Package\Link;
/**
* @author Nils Adermann <naderman@naderman.de>
*/
class Rule2Literals extends Rule
{
protected $literal1;
protected $literal2;
/**
* @param int $literal1
* @param int $literal2
* @param int $reason A RULE_* constant describing the reason for generating this rule
* @param Link|PackageInterface $reasonData
* @param array $job The job this rule was created from
*/
public function __construct($literal1, $literal2, $reason, $reasonData, $job = null)
{
parent::__construct($reason, $reasonData, $job);
if ($literal1 < $literal2) {
$this->literal1 = $literal1;
$this->literal2 = $literal2;
} else {
$this->literal1 = $literal2;
$this->literal2 = $literal1;
}
}
public function getLiterals()
{
return array($this->literal1, $this->literal2);
}
public function getHash()
{
$data = unpack('ihash', md5($this->literal1.','.$this->literal2, true));
return $data['hash'];
}
/**
* Checks if this rule is equal to another one
*
* Ignores whether either of the rules is disabled.
*
* @param Rule $rule The rule to check against
* @return bool Whether the rules are equal
*/
public function equals(Rule $rule)
{
$literals = $rule->getLiterals();
if (2 != count($literals)) {
return false;
}
if ($this->literal1 !== $literals[0]) {
return false;
}
if ($this->literal2 !== $literals[1]) {
return false;
}
return true;
}
public function isAssertion()
{
return false;
}
/**
* Formats a rule as a string of the format (Literal1|Literal2|...)
*
* @return string
*/
public function __toString()
{
$result = ($this->isDisabled()) ? 'disabled(' : '(';
$result .= $this->literal1 . '|' . $this->literal2 . ')';
return $result;
}
}