]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Improve hg arc diff perf when image files are uploaded
authordurham <durham@fb.com>
Thu, 28 Feb 2013 17:46:42 +0000 (09:46 -0800)
committerdurham <durham@fb.com>
Thu, 28 Feb 2013 17:46:42 +0000 (09:46 -0800)
Summary:
arc diff called 'hg cat' twice for every image binary in the diff.
This turns out to take 1 second per call on a large repo because mercurial
has to parse the manifest every time.

Now arc diff batches up all the files and does only two 'hg cat'
commands.  This makes the cost constant relative to the number of
images being uploaded.

Test Plan:
Ran arc diff on a diff with 30 images on both git and hg.
Verified that it was fast and that the images showed up in the web ui.

Reviewers: epriestley

Reviewed By: epriestley

CC: sid0, dschleimer, bos, aran, Korvin
Differential Revision: https://secure.phabricator.com/D5144

src/parser/ArcanistDiffParser.php
src/repository/api/ArcanistMercurialAPI.php
src/repository/api/ArcanistRepositoryAPI.php

index 180ce7008519b72f5d3a1531e7ffd8a36a085f9f..146606ee72027127c0e7e27fc77d0cd6a7cdf91b 100644 (file)
@@ -1142,6 +1142,8 @@ final class ArcanistDiffParser {
       return;
     }
 
+    $imagechanges = array();
+
     $changes = $this->changes;
     foreach ($changes as $change) {
       $path = $change->getCurrentPath();
@@ -1183,8 +1185,22 @@ final class ArcanistDiffParser {
         continue;
       }
 
-      $change->setOriginalFileData($repository_api->getOriginalFileData($path));
-      $change->setCurrentFileData($repository_api->getCurrentFileData($path));
+      $imagechanges[$path] = $change;
+    }
+
+    // Fetch the actual file contents in batches so repositories
+    // that have slow random file accesses (i.e. mercurial) can
+    // optimize the retrieval.
+    $paths = array_keys($imagechanges);
+
+    $filedata = $repository_api->getBulkOriginalFileData($paths);
+    foreach ($filedata as $path => $data) {
+      $imagechanges[$path]->setOriginalFileData($data);
+    }
+
+    $filedata = $repository_api->getBulkCurrentFileData($paths);
+    foreach ($filedata as $path => $data) {
+      $imagechanges[$path]->setCurrentFileData($data);
     }
 
     $this->changes = $changes;
index 10de875f879ca2715044cb3876ddd9b8d676ff13..359d61b2c1494e4795629fef2702944a906e914c 100644 (file)
@@ -408,6 +408,50 @@ final class ArcanistMercurialAPI extends ArcanistRepositoryAPI {
       $this->getWorkingCopyRevision());
   }
 
+  public function getBulkOriginalFileData($paths) {
+    return $this->getBulkFileDataAtRevision($paths, $this->getBaseCommit());
+  }
+
+  public function getBulkCurrentFileData($paths) {
+    return $this->getBulkFileDataAtRevision(
+      $paths,
+      $this->getWorkingCopyRevision());
+  }
+
+  private function getBulkFileDataAtRevision($paths, $revision) {
+    // Calling 'hg cat' on each file individually is slow (1 second per file
+    // on a large repo) because mercurial has to decompress and parse the
+    // entire manifest every time.  Do it in one large batch instead.
+
+    // hg cat will write the file data to files in a temp directory
+    $tmpdir = Filesystem::createTemporaryDirectory();
+
+    // Mercurial doesn't create the directories for us :(
+    foreach ($paths as $path) {
+      $tmppath = $tmpdir.'/'.$path;
+      Filesystem::createDirectory(dirname($tmppath), 0755, true);
+    }
+
+    list($err, $stdout) = $this->execManualLocal(
+      'cat --rev %s --output %s -- %C',
+      $revision,
+      // %p is the formatter for the repo-relative filepath
+      $tmpdir.'/%p',
+      implode(' ', $paths));
+
+    $filedata = array();
+    foreach ($paths as $path) {
+      $tmppath = $tmpdir.'/'.$path;
+      if (Filesystem::pathExists($tmppath)) {
+        $filedata[$path] = Filesystem::readFile($tmppath);
+      }
+    }
+
+    Filesystem::remove($tmpdir);
+
+    return $filedata;
+  }
+
   private function getFileDataAtRevision($path, $revision) {
     list($err, $stdout) = $this->execManualLocal(
       'cat --rev %s -- %s',
index 0a170fa780c13b63e71a2ffb27f2ada3d6129899..1d41c67774fb94c2229dd89a7d5e0ee4c08c9504 100644 (file)
@@ -296,6 +296,33 @@ abstract class ArcanistRepositoryAPI {
     }
   }
 
+  /**
+   * Fetches the original file data for each path provided.
+   *
+   * @return map<string, string> Map from path to file data.
+   */
+  public function getBulkOriginalFileData($paths) {
+    $filedata = array();
+    foreach ($paths as $path) {
+      $filedata[$path] = $this->getOriginalFileData($path);
+    }
+
+    return $filedata;
+  }
+
+  /**
+   * Fetches the current file data for each path provided.
+   *
+   * @return map<string, string> Map from path to file data.
+   */
+  public function getBulkCurrentFileData($paths) {
+    $filedata = array();
+    foreach ($paths as $path) {
+      $filedata[$path] = $this->getCurrentFileData($path);
+    }
+
+    return $filedata;
+  }
 
   /**
    * @return Traversable