debugdeltachain: stop summing the same chain over and over stable
authorPierre-Yves David <pierre-yves.david@octobus.net>
Tue, 21 Mar 2023 15:44:38 +0000
branchstable
changeset 50317 af776c3d5c3e
parent 50316 87f0155d68aa
child 50318 3bb7c56e8fe6
debugdeltachain: stop summing the same chain over and over Before this patch, delta chain size was computed from scratch for each chain, disregarding the fact very likely already computed the same of length-1 prefix for another revisions. We not cache delta chain size and shortcut the computation when we see them. Just for my mercurial-devel clone, this move the computation from about 17.5 second to about 4.8 seconds.
mercurial/debugcommands.py
--- a/mercurial/debugcommands.py	Mon Mar 20 11:52:17 2023 +0100
+++ b/mercurial/debugcommands.py	Tue Mar 21 15:44:38 2023 +0000
@@ -803,11 +803,12 @@
     # security to avoid crash on corrupted revlogs
     total_revs = len(index)
 
+    chain_size_cache = {}
+
     def revinfo(rev):
         e = index[rev]
         compsize = e[revlog_constants.ENTRY_DATA_COMPRESSED_LENGTH]
         uncompsize = e[revlog_constants.ENTRY_DATA_UNCOMPRESSED_LENGTH]
-        chainsize = 0
 
         base = e[revlog_constants.ENTRY_DELTA_BASE]
         p1 = e[revlog_constants.ENTRY_PARENT_1]
@@ -870,11 +871,17 @@
                 deltatype = b'prev'
 
         chain = r._deltachain(rev)[0]
-        for iterrev in chain:
-            e = index[iterrev]
-            chainsize += e[revlog_constants.ENTRY_DATA_COMPRESSED_LENGTH]
-
-        return p1, p2, compsize, uncompsize, deltatype, chain, chainsize
+        chain_size = 0
+        for iter_rev in reversed(chain):
+            cached = chain_size_cache.get(iter_rev)
+            if cached is not None:
+                chain_size += cached
+                break
+            e = index[iter_rev]
+            chain_size += e[revlog_constants.ENTRY_DATA_COMPRESSED_LENGTH]
+        chain_size_cache[rev] = chain_size
+
+        return p1, p2, compsize, uncompsize, deltatype, chain, chain_size
 
     fm = ui.formatter(b'debugdeltachain', opts)