@@ -90,9 +90,9 @@ graph_mem_pool::tryReuseExistingAllocation(
90
90
const std::vector<std::shared_ptr<node_impl>> &DepNodes) {
91
91
// If we have no dependencies this is a no-op because allocations must connect
92
92
// to a free node for reuse to be possible.
93
- // if (DepNodes.empty()) {
94
- // return std::nullopt;
95
- // }
93
+ if (DepNodes.empty ()) {
94
+ return std::nullopt;
95
+ }
96
96
97
97
std::vector<alloc_info> CompatibleAllocs;
98
98
// Compatible allocs can only be as big as MFreeAllocations
@@ -127,26 +127,22 @@ graph_mem_pool::tryReuseExistingAllocation(
127
127
NodesToCheck.push (Dep);
128
128
}
129
129
130
- std::optional<alloc_info> AllocInfo = {};
131
-
132
130
// Called when traversing over nodes to check if the current node is a free
133
131
// node for one of the available allocations. If it is we populate AllocInfo
134
132
// with the allocation to be reused.
135
133
auto CheckNodeEqual =
136
- [&CompatibleAllocs,
137
- &AllocInfo]( const std::shared_ptr<node_impl> &CurrentNode) -> bool {
134
+ [&CompatibleAllocs]( const std::shared_ptr<node_impl> &CurrentNode)
135
+ -> std::optional<alloc_info> {
138
136
for (auto &Alloc : CompatibleAllocs) {
139
137
const auto &AllocFreeNode = Alloc.LastFreeNode ;
140
138
// Compare control blocks without having to lock AllocFreeNode to check
141
139
// for node equality
142
140
if (!CurrentNode.owner_before (AllocFreeNode) &&
143
141
!AllocFreeNode.owner_before (CurrentNode)) {
144
- Alloc.LastFreeNode .reset ();
145
- AllocInfo = Alloc;
146
- return true ;
142
+ return Alloc;
147
143
}
148
144
}
149
- return false ;
145
+ return std::nullopt ;
150
146
};
151
147
152
148
while (!NodesToCheck.empty ()) {
@@ -161,11 +157,19 @@ graph_mem_pool::tryReuseExistingAllocation(
161
157
// for any of the allocations which are free for reuse. We should not bother
162
158
// checking nodes that are not free nodes, so we continue and check their
163
159
// predecessors.
164
- if (CurrentNode->MNodeType == node_type::async_free &&
165
- CheckNodeEqual (CurrentNode)) {
166
- // If we found an allocation AllocInfo has already been populated in
167
- // CheckNodeEqual(), so we simply break out of the loop
168
- break ;
160
+ if (CurrentNode->MNodeType == node_type::async_free) {
161
+ std::optional<alloc_info> AllocFound = CheckNodeEqual (CurrentNode);
162
+ if (AllocFound) {
163
+ // Reset visited nodes tracking
164
+ MGraph.resetNodeVisitedEdges ();
165
+ // Reset last free node for allocation
166
+ MAllocations.at (AllocFound.value ().Ptr ).LastFreeNode .reset ();
167
+ // Remove found allocation from the free list
168
+ MFreeAllocations.erase (std::find (MFreeAllocations.begin (),
169
+ MFreeAllocations.end (),
170
+ AllocFound.value ().Ptr ));
171
+ return AllocFound;
172
+ }
169
173
}
170
174
171
175
// Add CurrentNode predecessors to queue
@@ -176,16 +180,8 @@ graph_mem_pool::tryReuseExistingAllocation(
176
180
// Mark node as visited
177
181
CurrentNode->MTotalVisitedEdges = 1 ;
178
182
}
179
- // Reset visited nodes tracking
180
- MGraph.resetNodeVisitedEdges ();
181
- // If we found an allocation, remove it from the free list.
182
- if (AllocInfo) {
183
- MFreeAllocations.erase (std::find (MFreeAllocations.begin (),
184
- MFreeAllocations.end (),
185
- AllocInfo.value ().Ptr ));
186
- }
187
183
188
- return AllocInfo ;
184
+ return std::nullopt ;
189
185
}
190
186
191
187
void graph_mem_pool::markAllocationAsAvailable (
0 commit comments