summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-09-24 00:52:38 +0800
committerruki <[email protected]>2025-09-24 00:52:38 +0800
commitb73b791399b272c7a3d811df71cf6af3df50a4e0 (patch)
tree9e447feec43d372abd9bf823a3e24e6615534630
parentbf906566d8a9f69f09a6c0f20a9da30e3555d338 (diff)
improve jobgraph
-rw-r--r--xmake/core/base/graph.lua37
-rw-r--r--xmake/modules/async/jobgraph.lua20
2 files changed, 21 insertions, 36 deletions
diff --git a/xmake/core/base/graph.lua b/xmake/core/base/graph.lua
index 5fef5a5b3..29109a6bc 100644
--- a/xmake/core/base/graph.lua
+++ b/xmake/core/base/graph.lua
@@ -92,7 +92,7 @@ end
-- has the given vertex?
function graph:has_vertex(v)
- return table.contains(self:vertices(), v)
+ return self._adjacent_edges[v] ~= nil
end
-- add an isolated without edges
@@ -375,25 +375,22 @@ end
-- add edge
function graph:add_edge(from, to)
- local e = edge.new(from, to)
- if not self:has_vertex(from) then
- table.insert(self._vertices, from)
- self._adjacent_edges[from] = {}
- end
- if not self:has_vertex(to) then
- table.insert(self._vertices, to)
- self._adjacent_edges[to] = {}
+ if self:has_edge(from, to) then
+ return
end
+ self:add_vertex(from)
+ self:add_vertex(to)
+ local e = edge.new(from, to)
local edges_map = self._edges_map
edges_map[from] = edges_map[from] or {}
edges_map[from][to] = true
if self:is_directed() then
table.insert(self._adjacent_edges[from], e)
else
- table.insert(self._adjacent_edges[from], e)
- table.insert(self._adjacent_edges[to], e)
edges_map[to] = edges_map[to] or {}
edges_map[to][from] = true
+ table.insert(self._adjacent_edges[from], e)
+ table.insert(self._adjacent_edges[to], e)
end
table.insert(self._edges, e)
@@ -403,18 +400,16 @@ end
-- has the given edge?
function graph:has_edge(from, to)
- local edges = self:adjacent_edges(from)
- if edges then
- local edges_map = self._edges_map
- local from_map = edges_map[from]
+ local edges_map = self._edges_map
+ local from_map = edges_map[from]
+ if self:is_directed() then
if from_map and from_map[to] then
return true
- else
- for _, e in ipairs(edges) do
- if e:to() == to then
- return true
- end
- end
+ end
+ else
+ local to_map = edges_map[to]
+ if from_map and to_map and from_map[to] and to_map[from] then
+ return true
end
end
return false
diff --git a/xmake/modules/async/jobgraph.lua b/xmake/modules/async/jobgraph.lua
index 19d9f5e5a..5ad5d3bac 100644
--- a/xmake/modules/async/jobgraph.lua
+++ b/xmake/modules/async/jobgraph.lua
@@ -167,31 +167,21 @@ function jobgraph:add_orders(...)
-- we use a bridge job as a node to bridge the two groups.
local bridge = {from_group = prev_name, to_group = name}
for _, job in ipairs(prev) do
- if not dag:has_edge(job, bridge) then
- dag:add_edge(job, bridge)
- end
+ dag:add_edge(job, bridge)
end
for _, job in ipairs(curr) do
- if not dag:has_edge(bridge, job) then
- dag:add_edge(bridge, job)
- end
+ dag:add_edge(bridge, job)
end
elseif curr_is_group then
for _, job in ipairs(curr) do
- if not dag:has_edge(prev, job) then
- dag:add_edge(prev, job)
- end
+ dag:add_edge(prev, job)
end
elseif prev_is_group then
for _, job in ipairs(prev) do
- if not dag:has_edge(job, curr) then
- dag:add_edge(job, curr)
- end
+ dag:add_edge(job, curr)
end
else
- if not dag:has_edge(prev, curr) then
- dag:add_edge(prev, curr)
- end
+ dag:add_edge(prev, curr)
end
end
prev = curr