comparison mercurial/ancestor.py @ 7882:8d78fc991b71

ancestor: caching the parent list to improve performance When computing the DAG depth, we walk through all ancestors: this commit adds memoization during that first step. Then, the memorized parents are fetched from a dict instead of calling parents() on each vertex. This tweak, according to Kcachegrind, improves ancestor() performance by 16% when cloning a big repository.
author Nicolas Dumazet <nicdumz.commits@gmail.com>
date Mon, 23 Mar 2009 15:36:30 +0100
parents 532ca442b903
children 46293a0c7e9f
comparison
equal deleted inserted replaced
7881:3ac7114c2555 7882:8d78fc991b71
17 17
18 if a == b: 18 if a == b:
19 return a 19 return a
20 20
21 # find depth from root of all ancestors 21 # find depth from root of all ancestors
22 parentcache = {}
22 visit = [a, b] 23 visit = [a, b]
23 depth = {} 24 depth = {}
24 while visit: 25 while visit:
25 vertex = visit[-1] 26 vertex = visit[-1]
26 pl = pfunc(vertex) 27 pl = pfunc(vertex)
28 parentcache[vertex] = pl
27 if not pl: 29 if not pl:
28 depth[vertex] = 0 30 depth[vertex] = 0
29 visit.pop() 31 visit.pop()
30 else: 32 else:
31 for p in pl: 33 for p in pl:
44 while h: 46 while h:
45 d, n = heapq.heappop(h) 47 d, n = heapq.heappop(h)
46 if n not in seen: 48 if n not in seen:
47 seen[n] = 1 49 seen[n] = 1
48 yield (d, n) 50 yield (d, n)
49 for p in pfunc(n): 51 for p in parentcache[n]:
50 heapq.heappush(h, (depth[p], p)) 52 heapq.heappush(h, (depth[p], p))
51 53
52 def generations(vertex): 54 def generations(vertex):
53 sg, s = None, {} 55 sg, s = None, {}
54 for g, v in ancestors(vertex): 56 for g, v in ancestors(vertex):