Skip to content

Commit 64b5925

Browse files
Andy-Jostclaude
andcommitted
cuda.core: factor retire_graph out of the graph invalidation sweeps
invalidate_child_graph_state and invalidate_root_graph_state repeated the per-box retirement: detach node handles, drop the registry entry and attachments, move the box to the graveyard. Both now call retire_graph and differ only in which boxes they select. No behavior change. Review feedback on #2838. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent b0290c4 commit 64b5925

1 file changed

Lines changed: 23 additions & 29 deletions

File tree

‎cuda_core/cuda/core/_cpp/rt/graph.cpp‎

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,26 @@ static const GraphNodeBox* get_box(const GraphNodeHandle& h) {
679679
);
680680
}
681681

682+
// Retire one box of a graph that CUDA has destroyed: detach its node handles,
683+
// drop its registry entry and attachments, and move it to the graveyard.
684+
// `graph` must be an iterator into hierarchy.graphs; the splice invalidates it.
685+
static void retire_graph(
686+
GraphHierarchy& hierarchy,
687+
std::list<GraphBox>::iterator graph) noexcept {
688+
for (auto& entry : graph->node_handles.drain()) {
689+
if (GraphNodeHandle h_node = entry.second.lock()) {
690+
get_box(h_node)->resource = nullptr;
691+
}
692+
}
693+
if (graph->resource) {
694+
graph_registry.unregister_handle(graph->resource);
695+
graph->resource = nullptr;
696+
}
697+
graph->attachments.clear();
698+
hierarchy.graveyard.splice(
699+
hierarchy.graveyard.end(), hierarchy.graphs, graph);
700+
}
701+
682702
// graphs is ordered parent-before-child. Nulling a selected box marks its
683703
// later descendants, whose parent pointers remain valid after list splicing.
684704
// This permits one allocation-free sweep of the hierarchy.
@@ -701,21 +721,9 @@ void invalidate_child_graph_state(
701721
graph->owner_node == owner_node;
702722
bool is_descendant = graph->parent &&
703723
!graph->parent->resource;
704-
if (!is_owned_root && !is_descendant) {
705-
continue;
706-
}
707-
708-
// Empty node_handles and invalidate each one.
709-
for (auto& entry : graph->node_handles.drain()) {
710-
if (GraphNodeHandle h_node = entry.second.lock()) {
711-
get_box(h_node)->resource = nullptr;
712-
}
724+
if (is_owned_root || is_descendant) {
725+
retire_graph(hierarchy, graph);
713726
}
714-
graph_registry.unregister_handle(graph->resource);
715-
graph->resource = nullptr;
716-
graph->attachments.clear();
717-
hierarchy.graveyard.splice(
718-
hierarchy.graveyard.end(), hierarchy.graphs, graph);
719727
}
720728
}
721729

@@ -734,21 +742,7 @@ void invalidate_root_graph_state(const GraphHandle& h_root) noexcept {
734742
GraphHierarchy& hierarchy = *root->hierarchy;
735743
for (auto it = hierarchy.graphs.begin();
736744
it != hierarchy.graphs.end();) {
737-
auto graph = it++;
738-
739-
// Empty node_handles and invalidate each one.
740-
for (auto& entry : graph->node_handles.drain()) {
741-
if (GraphNodeHandle h_node = entry.second.lock()) {
742-
get_box(h_node)->resource = nullptr;
743-
}
744-
}
745-
if (graph->resource) {
746-
graph_registry.unregister_handle(graph->resource);
747-
graph->resource = nullptr;
748-
}
749-
graph->attachments.clear();
750-
hierarchy.graveyard.splice(
751-
hierarchy.graveyard.end(), hierarchy.graphs, graph);
745+
retire_graph(hierarchy, it++);
752746
}
753747
}
754748

0 commit comments

Comments
 (0)