Skip to content

Commit 194acd4

Browse files
committed
optimize taskgraph.transforms.cached_tasks.order_tasks
We're doing a topological sort of a graph that's typically very sparse, so start by dealing with the common case of tasks with no dependencies.
1 parent 1e1ed20 commit 194acd4

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/taskgraph/transforms/cached_tasks.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,31 @@
1212

1313

1414
def order_tasks(config, tasks):
15-
"""Iterate image tasks in an order where parent tasks come first."""
15+
"""Iterate tasks within a kind in an order where parent tasks come first."""
1616
kind_prefix = config.kind + "-"
17-
pending = {task["label"]: task for task in tasks}
18-
nodes = set(pending)
17+
pending = {}
18+
pending_deps = {}
19+
# first, yield tasks with no dependencies
20+
for task in tasks:
21+
deps = [dep for dep in task.get("dependencies", {}).values() if dep.startswith(kind_prefix)]
22+
if not deps:
23+
yield task
24+
continue
25+
label = task["label"]
26+
pending[label] = task
27+
pending_deps[label] = deps
28+
29+
if not pending:
30+
return
31+
32+
# now sort the remaining tasks
1933
edges = {
2034
(label, dep, "")
2135
for label in pending
22-
for dep in pending[label].get("dependencies", {}).values()
23-
if dep.startswith(kind_prefix)
36+
for dep in pending_deps[label]
37+
if dep in pending
2438
}
25-
graph = Graph(nodes, edges)
39+
graph = Graph(pending.keys(), edges)
2640

2741
for label in graph.visit_postorder():
2842
yield pending.pop(label)

0 commit comments

Comments
 (0)