git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 2/2] git-p4: speed up search for branch parent

From
Joachim Kuebart via GitGitGadget <gitgitgadget@gmail.com>
Date
May 5, 2021, 11:56 UTC
Message-ID
<41b3a23f682cddb3720de14723854c5956f25704.1620215786.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1013.v2.git.git.1620215786.gitgitgadget@gmail.com>
From: Joachim Kuebart <joachim.kuebart@gmail.com>

For every new branch that git-p4 imports, it needs to find the commit where it branched off its parent branch. While p4 doesn't record this information explicitly, the first changelist on a branch is usually an identical copy of the parent branch.

The method searchParent() tries to find a commit in the history of the given "parent" branch whose tree exactly matches the initial changelist of the new branch, "target". The code iterates through the parent commits and compares each of them to this initial changelist using diff-tree.

Since we already know the tree object name we are looking for, spawning diff-tree for each commit is wasteful.

Use the "--format" option of "rev-list" to find out the tree object name of each commit in the history, and find the tree whose name is exactly the same as the tree of the target commit to optimize this.

This results in a considerable speed-up, at least on Windows. On one Windows machine with a fairly large repository of about 16000 commits in the parent branch, the current code takes over 7 minutes, while the new code only takes just over 10 seconds for the same changelist:

Before:
    $ time git p4 sync
    Importing from/into multiple branches
    Depot paths: //depot
    Importing revision 31274 (100.0%)
    Updated branches: b1
    real    7m41.458s
    user    0m0.000s
    sys     0m0.077s
After:
    $ time git p4 sync
    Importing from/into multiple branches
    Depot paths: //depot
    Importing revision 31274 (100.0%)
    Updated branches: b1
    real    0m10.235s
    user    0m0.000s
    sys     0m0.062s
Signed-off-by: Joachim Kuebart <joachim.kuebart@gmail.com>
Helped-by: Junio C Hamano <gitster@pobox.com>
Helped-by: Luke Diamand <luke@diamand.org>
---
 git-p4.py | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)
diff --git a/git-p4.py b/git-p4.py
index 09c9e93ac401..d34a1946b754 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -3600,19 +3600,18 @@ def importNewBranch(self, branch, maxChange):
         return True
 
     def searchParent(self, parent, branch, target):
-        parentFound = False
-        for blob in read_pipe_lines(["git", "rev-list", "--reverse",
+        targetTree = read_pipe(["git", "rev-parse",
+                                "{}^{{tree}}".format(target)]).strip()
+        for line in read_pipe_lines(["git", "rev-list", "--format=%H %T",
                                      "--no-merges", parent]):
-            blob = blob.strip()
-            if len(read_pipe(["git", "diff-tree", blob, target])) == 0:
-                parentFound = True
+            if line.startswith("commit "):
+                continue
+            commit, tree = line.strip().split(" ")
+            if tree == targetTree:
                 if self.verbose:
-                    print("Found parent of %s in commit %s" % (branch, blob))
-                break
-        if parentFound:
-            return blob
-        else:
-            return None
+                    print("Found parent of %s in commit %s" % (branch, commit))
+                return commit
+        return None
 
     def importChanges(self, changes, origin_revision=0):
         cnt = 1
-- 
gitgitgadget
Previous: Joachim Kuebart via GitGitGadget
Message 10 of 10 in “git-p4: speed up search for branch parent”
  1. git-p4: speed up search for branch parentJoachim Kuebart via GitGitGadget, Apr 28, 2021
  2. Junio C HamanoApr 29, 2021
  3. Joachim KuebartApr 29, 2021
  4. Luke DiamandApr 29, 2021
  5. Junio C HamanoApr 29, 2021
  6. Joachim KuebartApr 29, 2021
  7. Joachim KuebartApr 29, 2021
  8. 0/2 git-p4: speed up search for branch parentJoachim Kuebart via GitGitGadget, May 5, 2021
  9. 1/2 git-p4: ensure complex branches are cloned correctlyJoachim Kuebart via GitGitGadget, May 5, 2021
  10. 2/2 git-p4: speed up search for branch parentJoachim Kuebart via GitGitGadget, May 5, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.