comparison mercurial/revlog.py @ 7883:c63c30ae9e39

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.
author Nicolas Dumazet <nicdumz.commits@gmail.com>
date Mon, 23 Mar 2009 15:32:29 +0100
parents d812029cda85
children 685ce2f7ee35
comparison
equal deleted inserted replaced
7882:8d78fc991b71 7883:c63c30ae9e39
40 return int(q & 0xFFFF) 40 return int(q & 0xFFFF)
41 41
42 def offset_type(offset, type): 42 def offset_type(offset, type):
43 return long(long(offset) << 16 | type) 43 return long(long(offset) << 16 | type)
44 44
45 nullhash = _sha(nullid)
46
45 def hash(text, p1, p2): 47 def hash(text, p1, p2):
46 """generate a hash from the given text and its parent hashes 48 """generate a hash from the given text and its parent hashes
47 49
48 This hash combines both the current file contents and its history 50 This hash combines both the current file contents and its history
49 in a manner that makes it easy to distinguish nodes with the same 51 in a manner that makes it easy to distinguish nodes with the same
50 content in the revision graph. 52 content in the revision graph.
51 """ 53 """
52 l = [p1, p2] 54 # As of now, if one of the parent node is null, p2 is null
53 l.sort() 55 if p2 == nullid:
54 s = _sha(l[0]) 56 # deep copy of a hash is faster than creating one
55 s.update(l[1]) 57 s = nullhash.copy()
58 s.update(p1)
59 else:
60 # none of the parent nodes are nullid
61 l = [p1, p2]
62 l.sort()
63 s = _sha(l[0])
64 s.update(l[1])
56 s.update(text) 65 s.update(text)
57 return s.digest() 66 return s.digest()
58 67
59 def compress(text): 68 def compress(text):
60 """ generate a possibly-compressed representation of text """ 69 """ generate a possibly-compressed representation of text """