rev-branch-cache: schedule a write of the "v2" format if we read from "v1"
authorPierre-Yves David <pierre-yves.david@octobus.net>
Wed, 25 Sep 2024 12:49:32 +0200
changeset 51908 16efed18ae4e
parent 51907 bd7359c18d69
child 51909 e51161b12c7e
rev-branch-cache: schedule a write of the "v2" format if we read from "v1" The new file can be memorymapped, while the old one cannot. So there is value in having the v2 format around as soon a possible.
mercurial/branching/rev_cache.py
tests/test-branches.t
tests/test-rebase-conflicts.t
tests/test-strip.t
--- a/mercurial/branching/rev_cache.py	Tue Sep 24 15:44:10 2024 +0200
+++ b/mercurial/branching/rev_cache.py	Wed Sep 25 12:49:32 2024 +0200
@@ -146,6 +146,7 @@
         self._names = []  # branch names in local encoding with static index
         self._rbcrevs = rbcrevs(bytearray())
         self._rbcsnameslen = 0  # length of names read at _rbcsnameslen
+        self._force_overwrite = False
         v1_fallback = False
         try:
             try:
@@ -157,6 +158,7 @@
                 # consider stop doing this many version after hg-6.9 release
                 bndata = repo.cachevfs.read(_rbc_legacy_names)
                 v1_fallback = True
+                self._force_overwrite = True
             self._rbcsnameslen = len(bndata)  # for verification before writing
             if bndata:
                 self._names = [
@@ -198,7 +200,6 @@
             self._names = []
         self._rbcnamescount = len(self._names)  # number of names read at
         # _rbcsnameslen
-        self._force_overwrite = False
 
     def _clear(self):
         self._rbcsnameslen = 0
@@ -320,7 +321,7 @@
         step = b''
         try:
             # write the new names
-            if self._rbcnamescount < len(self._names):
+            if self._force_overwrite or self._rbcnamescount < len(self._names):
                 wlock = repo.wlock(wait=False)
                 step = b' names'
                 self._writenames(repo)
@@ -345,15 +346,24 @@
     def _writenames(self, repo):
         """write the new branch names to revbranchcache"""
         f = None
+        if self._force_overwrite:
+            self._rbcsnameslen = 0
+            self._rbcnamescount = 0
         try:
-            if self._rbcnamescount != 0:
+            if self._force_overwrite or self._rbcnamescount != 0:
                 f = repo.cachevfs.open(_rbcnames, b'ab')
-                if f.tell() == self._rbcsnameslen:
+                current_size = f.tell()
+                if current_size == self._rbcsnameslen:
                     f.write(b'\0')
                 else:
                     f.close()
-                    f = None
-                    repo.ui.debug(b"%s changed - rewriting it\n" % _rbcnames)
+                    if self._force_overwrite:
+                        dbg = b"resetting content of %s\n"
+                    elif current_size > 0:
+                        dbg = b"%s changed - rewriting it\n"
+                    else:
+                        dbg = b"%s is missing - rewriting it\n"
+                    repo.ui.debug(dbg % _rbcnames)
                     self._rbcnamescount = 0
                     self._rbcrevslen = 0
             if self._rbcnamescount == 0:
--- a/tests/test-branches.t	Tue Sep 24 15:44:10 2024 +0200
+++ b/tests/test-branches.t	Wed Sep 25 12:49:32 2024 +0200
@@ -880,8 +880,8 @@
 Smoothly reuse "v1" format if no v2 exists
 ------------------------------------------
 
-read only operation with valid data(
-(does not need to rewrite anything, maybe we should force it?)
+read only operation with valid data
+(actively rewrite data)
 
   $ rm .hg/cache/rbc-names-v2
   $ rm .hg/cache/rbc-revs-v2
@@ -895,17 +895,23 @@
   5
   $ f --size .hg/cache/rbc-*-*
   .hg/cache/rbc-names-v1: size=92
+  .hg/cache/rbc-names-v2: size=92
   .hg/cache/rbc-revs-v1: size=160
+  .hg/cache/rbc-revs-v2: size=160
 
 
 Write operation write a full v2 files
 
+  $ mv .hg/cache/rbc-names-v2 .hg/cache/rbc-names-v1
+  $ mv .hg/cache/rbc-revs-v2 .hg/cache/rbc-revs-v1
+  $ f --size .hg/cache/rbc-*
+  .hg/cache/rbc-names-v1: size=92
+  .hg/cache/rbc-revs-v1: size=160
   $ hg branch not-here-for-long
   marked working directory as branch not-here-for-long
   $ hg ci -m not-long --debug
   reusing manifest from p1 (no file change)
   committing changelog
-  rbc-names-v2 changed - rewriting it
   updating the branch cache
   committed changeset * (glob)
   $ f --size .hg/cache/rbc-*
@@ -927,6 +933,7 @@
   5
   $ f --size .hg/cache/rbc-*-*
   .hg/cache/rbc-names-v1: size=110
+  .hg/cache/rbc-names-v2: size=110
   .hg/cache/rbc-revs-v1: size=110
   .hg/cache/rbc-revs-v2: size=168
 
@@ -968,7 +975,7 @@
   $ echo > .hg/cache/rbc-names-v2
   $ hg log -r '5:&branch(.)' -T '{rev} ' --debug
   referenced branch names not found - rebuilding revision branch cache from scratch
-  8 9 10 11 12 13  (no-eol)
+  8 9 10 11 12 13 resetting content of rbc-names-v2
   $ f --size .hg/cache/rbc-names-*
   .hg/cache/rbc-names-v2: size=84
   $ grep "i-will-regret-this" .hg/cache/rbc-names-* > /dev/null
--- a/tests/test-rebase-conflicts.t	Tue Sep 24 15:44:10 2024 +0200
+++ b/tests/test-rebase-conflicts.t	Wed Sep 25 12:49:32 2024 +0200
@@ -319,7 +319,7 @@
   bundle2-input-part: "phase-heads" supported
   bundle2-input-part: total payload size 24
   bundle2-input-bundle: 3 parts total
-  resetting content of cache/rbc-revs-v2
+  resetting content of rbc-names-v2
   added 2 changesets with 2 changes to 1 files
   updating the branch cache
   invalid branch cache (served): tip differs
--- a/tests/test-strip.t	Tue Sep 24 15:44:10 2024 +0200
+++ b/tests/test-strip.t	Wed Sep 25 12:49:32 2024 +0200
@@ -913,7 +913,7 @@
   saved backup bundle to $TESTTMP/issue4736/.hg/strip-backup/6625a5168474-345bb43d-backup.hg
   updating the branch cache
   invalid branch cache (served): tip differs
-  resetting content of cache/rbc-revs-v2
+  resetting content of rbc-names-v2
   $ hg log -G
   o  changeset:   2:5c51d8d6557d
   |  tag:         tip