From 71ff2d85de5ff1f9aabc32191f1bd04514191587 Mon Sep 17 00:00:00 2001 From: epriestley Date: Tue, 25 Jan 2011 18:05:15 -0800 Subject: [PATCH] Use caching to provide a huge perf boost to phutil_mapper. --- .gitignore | 1 + scripts/phutil_mapper.php | 94 ++++++++++++++++++++++++++++++++------- 2 files changed, 80 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index eda77f49..acdf7452 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store ._* +/src/.phutil_module_cache diff --git a/scripts/phutil_mapper.php b/scripts/phutil_mapper.php index 20d9235e..8dc99436 100755 --- a/scripts/phutil_mapper.php +++ b/scripts/phutil_mapper.php @@ -26,6 +26,7 @@ if ($argc != 2) { } phutil_require_module('phutil', 'filesystem'); +phutil_require_module('phutil', 'filesystem/filefinder'); phutil_require_module('phutil', 'future/exec'); $root = Filesystem::resolvePath($argv[1]); @@ -35,31 +36,88 @@ if (!@file_exists($root.'/__phutil_library_init__.php')) { } echo "Finding phutil modules...\n"; +$files = id(new FileFinder($root)) + ->withType('f') + ->withSuffix('php') + ->excludePath('*/.*') + ->setGenerateChecksums(true) + ->find(); + +// NOTE: Sorting by filename ensures that hash computation is stable; it is +// important we sort by name instead of by hash because sorting by hash could +// create a bad cache hit if the user swaps the contents of two files. +ksort($files); + +$modules = array(); +foreach ($files as $file => $hash) { + if (dirname($file) == $root) { + continue; + } + $modules[Filesystem::readablePath(dirname($file), $root)][] = $hash; +} + +echo "Found ".count($files)." in ".count($modules)." modules.\n"; + +$signatures = array(); +foreach ($modules as $module => $hashes) { + $hashes = implode(' ', $hashes); + $signature = md5($hashes); + $signatures[$module] = $signature; +} + +try { + $cache = Filesystem::readFile($root.'/.phutil_module_cache'); +} catch (Exception $ex) { + $cache = null; +} -list($stdout) = execx( - "(cd %s && find . -type d -path '*/.*' -prune -o -type d -print0)", - $root); +$signature_cache = array(); +if ($cache) { + $signature_cache = json_decode($cache, true); + if (!is_array($signature_cache)) { + $signature_cache = array(); + } +} + +$specs = array(); $futures = array(); -foreach (array_filter(explode("\0", $stdout)) as $dir) { - if ($dir == '.') { - continue; +foreach ($signatures as $module => $signature) { + if (isset($signature_cache[$module]) && + $signature_cache[$module]['signature'] == $signature) { + $specs[$module] = $signature_cache[$module]; + } else { + $futures[$module] = new ExecFuture( + '%s %s', + dirname(__FILE__).'/phutil_analyzer.php', + $root.'/'.$module); } - $module = preg_replace('@^\\./@', '', $dir); - $futures[$module] = new ExecFuture( - '%s %s', - dirname(__FILE__).'/phutil_analyzer.php', - $root.'/'.$module); } -echo "Analyzing ".number_format(count($futures))." modules"; +if ($futures) { + echo "Found ".count($specs)." modules in cache; ". + "analyzing ".count($futures)." modified modules"; +} else { + echo "All modules were found in cache.\n"; +} + +if ($futures) { + foreach (Futures($futures)->limit(8) as $module => $future) { + echo "."; + $specs[$module] = array( + 'signature' => $signatures[$module], + 'spec' => $future->resolveJSON(), + ); + } + echo "\n"; +} + $class_map = array(); $requires_class_map = array(); $requires_interface_map = array(); $function_map = array(); -foreach (Futures($futures)->limit(16) as $module => $future) { - echo "."; - $spec = $future->resolveJSON(); +foreach ($specs as $module => $info) { + $spec = $info['spec']; foreach (array('class', 'interface') as $type) { foreach ($spec['declares'][$type] as $class => $where) { if (!empty($class_map[$class])) { @@ -125,4 +183,10 @@ echo "Writing library map file...\n"; Filesystem::writeFile($root.'/__phutil_library_map__.php', $map_file); +echo "Writing module cache...\n"; + +Filesystem::writeFile( + $root.'/.phutil_module_cache', + json_encode($specs)); + echo "Done.\n"; -- 2.43.0