git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #56918 from grahamc/closure-graph-memory

references-by-popularity: get a handle on memory usage

commit5d3fd3674a66c5b1ada63e2eace140519849c967 authorShea Levy <shea@shealevy.com> dateWed 6 Mar 2019, 08:31 (8 years ago) parent18b0cf709362bc
1 file changed+50−3
pkgs/build-support/references-by-popularity/closure-graph.py+50−3
pkgs/build-support/references-by-popularity/closure-graph.py+50−3
@@ -117,6 +117,17 @@ import unittest
117117from pprint import pprint
118118from collections import defaultdict
119119
120+
121+def debug(msg, *args, **kwargs):
122+ if False:
123+ print(
124+ "DEBUG: {}".format(
125+ msg.format(*args, **kwargs)
126+ ),
127+ file=sys.stderr
128+ )
129+
130+
120131# Find paths in the original dataset which are never referenced by
121132# any other paths
122133def find_roots(closures):
@@ -327,10 +338,23 @@ class TestMakeLookup(unittest.TestCase):
327338# /nix/store/tux: {}
328339# }
329340# }
341+subgraphs_cache = {}
330342def make_graph_segment_from_root(root, lookup):
343+ global subgraphs_cache
331344 children = {}
332345 for ref in lookup[root]:
333− children[ref] = make_graph_segment_from_root(ref, lookup)
346+ # make_graph_segment_from_root is a pure function, and will
347+ # always return the same result based on a given input. Thus,
348+ # cache computation.
349+ #
350+ # Python's assignment will use a pointer, preventing memory
351+ # bloat for large graphs.
352+ if ref not in subgraphs_cache:
353+ debug("Subgraph Cache miss on {}".format(ref))
354+ subgraphs_cache[ref] = make_graph_segment_from_root(ref, lookup)
355+ else:
356+ debug("Subgraph Cache hit on {}".format(ref))
357+ children[ref] = subgraphs_cache[ref]
334358 return children
335359
336360class TestMakeGraphSegmentFromRoot(unittest.TestCase):
@@ -381,12 +405,27 @@ class TestMakeGraphSegmentFromRoot(unittest.TestCase):
381405# /nix/store/baz: 4
382406# /nix/store/tux: 6
383407# ]
408+popularity_cache = {}
384409def graph_popularity_contest(full_graph):
410+ global popularity_cache
385411 popularity = defaultdict(int)
386412 for path, subgraph in full_graph.items():
387413 popularity[path] += 1
388− subcontest = graph_popularity_contest(subgraph)
414+ # graph_popularity_contest is a pure function, and will
415+ # always return the same result based on a given input. Thus,
416+ # cache computation.
417+ #
418+ # Python's assignment will use a pointer, preventing memory
419+ # bloat for large graphs.
420+ if path not in popularity_cache:
421+ debug("Popularity Cache miss on {}", path)
422+ popularity_cache[path] = graph_popularity_contest(subgraph)
423+ else:
424+ debug("Popularity Cache hit on {}", path)
425+
426+ subcontest = popularity_cache[path]
389427 for subpath, subpopularity in subcontest.items():
428+ debug("Calculating popularity for {}", subpath)
390429 popularity[subpath] += subpopularity + 1
391430
392431 return popularity
@@ -474,6 +513,7 @@ def main():
474513 filename = sys.argv[1]
475514 key = sys.argv[2]
476515
516+ debug("Loading from {}", filename)
477517 with open(filename) as f:
478518 data = json.load(f)
479519
@@ -497,14 +537,21 @@ def main():
497537 # ]
498538 graph = data[key]
499539
540+ debug("Finding roots from {}", key)
500541 roots = find_roots(graph);
542+ debug("Making lookup for {}", key)
501543 lookup = make_lookup(graph)
502544
503545 full_graph = {}
504546 for root in roots:
547+ debug("Making full graph for {}", root)
505548 full_graph[root] = make_graph_segment_from_root(root, lookup)
506549
507− ordered = order_by_popularity(graph_popularity_contest(full_graph))
550+ debug("Running contest")
551+ contest = graph_popularity_contest(full_graph)
552+ debug("Ordering by popularity")
553+ ordered = order_by_popularity(contest)
554+ debug("Checking for missing paths")
508555 missing = []
509556 for path in all_paths(graph):
510557 if path not in ordered: