fastannotate: skip flushing revmap if nothing changed

Summary:
Writing the revmap back to disk can be a bit expensive if the revmap is not
small. This diff optimizes `revmap.flush` so it does not write anything if
nothing changed.

Test Plan: Profiling data shows no more substantial `write` syscalls.

Reviewers: #sourcecontrol, zamsden

Reviewed By: zamsden

Subscribers: zamsden, mjpieters

Differential Revision: https://phabricator.intern.facebook.com/D3837023

Signature: t1:3837023:1473954024:6af83074096517ab1d3d05cb90d091af6e0c5d02
This commit is contained in:
Jun Wu 2016-09-08 21:01:47 +01:00
parent 23ab46339b
commit 12805254e6

View File

@ -48,6 +48,7 @@ class revmap(object):
self._rev2hsh = [None]
self._rev2flag = [None]
self._hsh2rev = {}
self._lastmaxrev = -1
if path:
if os.path.exists(path):
self._load()
@ -74,6 +75,7 @@ class revmap(object):
with open(self.path, 'a') as f:
f.write(hsh)
f.write(struct.pack('B', flag))
self._lastmaxrev = self.maxrev
return idx
def rev2hsh(self, rev):
@ -105,7 +107,7 @@ class revmap(object):
def flush(self):
"""write the state down to the file"""
if not self.path:
if not self.path or self.maxrev == self._lastmaxrev: # nothing changed
return
with open(self.path, 'wb') as f:
f.write(self.HEADER)
@ -114,6 +116,7 @@ class revmap(object):
continue
f.write(hsh)
f.write(struct.pack('B', self._rev2flag[i]))
self._lastmaxrev = self.maxrev
def _load(self):
"""load state from file"""
@ -135,6 +138,7 @@ class revmap(object):
self._rev2flag.append(flag)
else:
raise error.CorruptedFileError()
self._lastmaxrev = self.maxrev
def __contains__(self, f):
"""(fctx or node) -> bool.