PHP Phar creation slow on a powerful PC, how to speed up (loading/reading ~3000 files)? PHP Phar creation slow on a powerful PC, how to speed up (loading/reading ~3000 files)? symfony symfony

PHP Phar creation slow on a powerful PC, how to speed up (loading/reading ~3000 files)?


Try using Phar::addFile($file) instead of Phar::addFromString(file_get_contents($file))

i.e.

function addFile(Phar $phar, SplFileInfo $file){    $root = realpath(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR);    $path = strtr(str_replace($root, '', $file->getRealPath()), '\\', '/');    //$phar->addFromString($path, file_get_contents($file));    $phar->addFile($file,$path);}


Phar::addFromString() or Phar:addFromFile() is incredibly slow.Like @sectus said Phar::buildFromDirectory() is a lot faster. But as an easy alternative with little changes to your code you could use Phar::buildFromIterator().

Example:

$all = $app->append($vendor)->append($src)->append($web);$phar->buildFromIterator($all, dirname(__DIR__));

instead of:

$all = array_merge(    iterator_to_array($app),    iterator_to_array($src),    iterator_to_array($vendor),    iterator_to_array($web));$c = count($all);$i = 1;$strings = array();foreach ($all as $file) {    addFile($phar, $file);    echo "Done $i/$c\r\n";    $i++;}

$ time app/build

real    0m4.459suser    0m2.895ssys     0m1.108s

Takes < 5 seconds on my quite slow ubuntu machine.


I'd suggest to dig in php config.

First recomendation - is to disable open_basedir if it is enabled. As i understand php internals, when u try to access any file location with php, it is a must for php to check if file location matches allowed directory-tree. So if there are many files this operation will be preformed for each file, and it can slow the proccess down significantly. On the other hand if open_basedir is disabled, check is never done.

http://www.php.net/manual/en/ini.core.php#ini.open-basedir

Second - is to check realpath_cache_size and realpath_cache_ttl.

As written in php description

Determines the size of the realpath cache to be used by PHP. This value should be increased on systems where PHP opens many files, to reflect the quantity of the file operations performed.

http://www.php.net/manual/en/ini.core.php#ini.realpath-cache-size

I hope this will help u to speed up ur operations.