# HG changeset patch # User Nicolas Dumazet # Date 2009-03-23 14:32:29 # Node ID c63c30ae9e39229ff3cbb2f452ef11a009a363f2 # Parent 8d78fc991b71e627327debc413b4a12c5a7f7f16 revlog: faster hash computation when one of the parent node is null Because we often compute sha1(nullid), it's interesting to copy a precomputed hash of nullid instead of computing everytime the same hash. Similarly, when one of the parents is null, we can avoid a < comparison (sort). Overall, this change adds a string equality comparison on each hash() call, but when p2 is null, we drop one string < comparison, and copy a hash instead of computing it. Since it is common to have revisions with only one parent, this change makes hash() 25% faster when cloning a big repository. diff --git a/mercurial/revlog.py b/mercurial/revlog.py --- a/mercurial/revlog.py +++ b/mercurial/revlog.py @@ -42,6 +42,8 @@ def gettype(q): def offset_type(offset, type): return long(long(offset) << 16 | type) +nullhash = _sha(nullid) + def hash(text, p1, p2): """generate a hash from the given text and its parent hashes @@ -49,10 +51,17 @@ def hash(text, p1, p2): in a manner that makes it easy to distinguish nodes with the same content in the revision graph. """ - l = [p1, p2] - l.sort() - s = _sha(l[0]) - s.update(l[1]) + # As of now, if one of the parent node is null, p2 is null + if p2 == nullid: + # deep copy of a hash is faster than creating one + s = nullhash.copy() + s.update(p1) + else: + # none of the parent nodes are nullid + l = [p1, p2] + l.sort() + s = _sha(l[0]) + s.update(l[1]) s.update(text) return s.digest()