Skip to content

Commit

Permalink
git.py: keep statistics on how much sha1 searching we had to do.
Browse files Browse the repository at this point in the history
And cmd/memtest prints out the results.  Unfortunately this slows down
memtest runs by 0.126/2.526 = 5% or so.  Yuck.  Well, we can take it out
later.

Signed-off-by: Avery Pennarun <apenwarr@gmail.com>
  • Loading branch information
apenwarr committed Aug 27, 2010
1 parent 94b76f8 commit 9fbcff9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/memtest-cmd.py
Expand Up @@ -80,4 +80,7 @@ def foreverit(mi):
assert(not m.exists(bin))
report((c+1)*opt.number)

print ('%d objects searched in %d steps: avg %.3f steps/object'
% (git._total_searches, git._total_steps,
git._total_steps*1.0/git._total_searches))
print 'Total time: %.3fs' % (time.time() - start)
14 changes: 14 additions & 0 deletions lib/bup/git.py
Expand Up @@ -14,6 +14,9 @@
_typemap = { 'blob':3, 'tree':2, 'commit':1, 'tag':4 }
_typermap = { 3:'blob', 2:'tree', 1:'commit', 4:'tag' }

_total_searches = 0
_total_steps = 0


class GitError(Exception):
pass
Expand Down Expand Up @@ -148,13 +151,17 @@ def _ofs_from_idx(self, idx):
return ofs

def _idx_from_hash(self, hash):
global _total_searches, _total_steps
_total_searches += 1
assert(len(hash) == 20)
b1 = ord(hash[0])
start = self.fanout[b1-1] # range -1..254
end = self.fanout[b1] # range 0..255
buf = buffer(self.map, 8 + 256*4, end*20)
want = str(hash)
_total_steps += 1 # lookup table is a step
while start < end:
_total_steps += 1
mid = start + (end-start)/2
v = str(buf[mid*20:(mid+1)*20])
if v < want:
Expand Down Expand Up @@ -226,14 +233,18 @@ def _fanget(self, i):

def exists(self, hash):
"""Return nonempty if the object exists in the index files."""
global _total_searches, _total_steps
_total_searches += 1
want = str(hash)
el = extract_bits(want, self.bits)
if el:
start = self._fanget(el-1)
else:
start = 0
end = self._fanget(el)
_total_steps += 1 # lookup table is a step
while start < end:
_total_steps += 1
mid = start + (end-start)/2
v = str(self.shalist[mid*20:(mid+1)*20])
if v < want:
Expand Down Expand Up @@ -276,10 +287,13 @@ def __len__(self):

def exists(self, hash):
"""Return nonempty if the object exists in the index files."""
global _total_searches
_total_searches += 1
if hash in self.also:
return True
for i in range(len(self.packs)):
p = self.packs[i]
_total_searches -= 1 # will be incremented by sub-pack
if p.exists(hash):
# reorder so most recently used packs are searched first
self.packs = [p] + self.packs[:i] + self.packs[i+1:]
Expand Down

0 comments on commit 9fbcff9

Please sign in to comment.