diff --git a/mercurial/branchmap.py b/mercurial/branchmap.py
index b181a650a886b68a13d8364a5ed4ce1dd1032427_bWVyY3VyaWFsL2JyYW5jaG1hcC5weQ==..db0095c83344a03e1357da5486c054ad51d47afd_bWVyY3VyaWFsL2JyYW5jaG1hcC5weQ== 100644
--- a/mercurial/branchmap.py
+++ b/mercurial/branchmap.py
@@ -402,5 +402,5 @@
         if rev == nullrev:
             return changelog.branchinfo(rev)
 
-        # if requested rev is missing, add and populate all missing revs
+        # if requested rev isn't allocated, grow and cache the rev info
         if len(self._rbcrevs) < rbcrevidx + _rbcrecsize:
@@ -406,6 +406,5 @@
         if len(self._rbcrevs) < rbcrevidx + _rbcrecsize:
-            self._rbcrevs.extend('\0' * (len(changelog) * _rbcrecsize -
-                                         len(self._rbcrevs)))
+            return self._branchinfo(rev)
 
         # fast path: extract data from cache, use it if node is matching
         reponode = changelog.node(rev)[:_rbcnodelen]
@@ -452,6 +451,10 @@
         rbcrevidx = rev * _rbcrecsize
         rec = array('c')
         rec.fromstring(pack(_rbcrecfmt, node, branchidx))
+        if len(self._rbcrevs) < rbcrevidx + _rbcrecsize:
+            self._rbcrevs.extend('\0' *
+                                 (len(self._repo.changelog) * _rbcrecsize -
+                                  len(self._rbcrevs)))
         self._rbcrevs[rbcrevidx:rbcrevidx + _rbcrecsize] = rec
         self._rbcrevslen = min(self._rbcrevslen, rev)
 
diff --git a/tests/test-branches.t b/tests/test-branches.t
index b181a650a886b68a13d8364a5ed4ce1dd1032427_dGVzdHMvdGVzdC1icmFuY2hlcy50..db0095c83344a03e1357da5486c054ad51d47afd_dGVzdHMvdGVzdC1icmFuY2hlcy50 100644
--- a/tests/test-branches.t
+++ b/tests/test-branches.t
@@ -655,8 +655,6 @@
   $ rm .hg/cache/rbc*
   $ hg log -r "5 & branch(5)" -T "{rev}\n"
   5
-BUG: rbc-revs should have an entry as 5th record but has it misplaced as the
-first:
   $ f --size --hexdump .hg/cache/rbc-*
   .hg/cache/rbc-names-v1: size=1
   0000: 61                                              |a|
@@ -660,7 +658,13 @@
   $ f --size --hexdump .hg/cache/rbc-*
   .hg/cache/rbc-names-v1: size=1
   0000: 61                                              |a|
-  .hg/cache/rbc-revs-v1: size=8
-  0000: d8 cb c6 1d 00 00 00 00                         |........|
+  .hg/cache/rbc-revs-v1: size=112
+  0000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
+  0010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
+  0020: 00 00 00 00 00 00 00 00 d8 cb c6 1d 00 00 00 00 |................|
+  0030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
+  0040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
+  0050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
+  0060: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
 
   $ cd ..