Skip to content

Commit 519fb14

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 519fb14

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/taskgraph/transforms/cached_tasks.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,35 @@
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 = [
22+
dep
23+
for dep in task.get("dependencies", {}).values()
24+
if dep.startswith(kind_prefix)
25+
]
26+
if not deps:
27+
yield task
28+
continue
29+
label = task["label"]
30+
pending[label] = task
31+
pending_deps[label] = deps
32+
33+
if not pending:
34+
return
35+
36+
# now sort the remaining tasks
1937
edges = {
2038
(label, dep, "")
2139
for label in pending
22-
for dep in pending[label].get("dependencies", {}).values()
23-
if dep.startswith(kind_prefix)
40+
for dep in pending_deps[label]
41+
if dep in pending
2442
}
25-
graph = Graph(nodes, edges)
43+
graph = Graph(pending.keys(), edges)
2644

2745
for label in graph.visit_postorder():
2846
yield pending.pop(label)

0 commit comments

Comments
 (0)