@@ -90,9 +90,9 @@ graph_mem_pool::tryReuseExistingAllocation(
9090 const std::vector<std::shared_ptr<node_impl>> &DepNodes) {
9191 // If we have no dependencies this is a no-op because allocations must connect
9292 // 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+ }
9696
9797 std::vector<alloc_info> CompatibleAllocs;
9898 // Compatible allocs can only be as big as MFreeAllocations
@@ -127,26 +127,22 @@ graph_mem_pool::tryReuseExistingAllocation(
127127 NodesToCheck.push (Dep);
128128 }
129129
130- std::optional<alloc_info> AllocInfo = {};
131-
132130 // Called when traversing over nodes to check if the current node is a free
133131 // node for one of the available allocations. If it is we populate AllocInfo
134132 // with the allocation to be reused.
135133 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> {
138136 for (auto &Alloc : CompatibleAllocs) {
139137 const auto &AllocFreeNode = Alloc.LastFreeNode ;
140138 // Compare control blocks without having to lock AllocFreeNode to check
141139 // for node equality
142140 if (!CurrentNode.owner_before (AllocFreeNode) &&
143141 !AllocFreeNode.owner_before (CurrentNode)) {
144- Alloc.LastFreeNode .reset ();
145- AllocInfo = Alloc;
146- return true ;
142+ return Alloc;
147143 }
148144 }
149- return false ;
145+ return std:: nullopt ;
150146 };
151147
152148 while (!NodesToCheck.empty ()) {
@@ -161,11 +157,19 @@ graph_mem_pool::tryReuseExistingAllocation(
161157 // for any of the allocations which are free for reuse. We should not bother
162158 // checking nodes that are not free nodes, so we continue and check their
163159 // 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+ }
169173 }
170174
171175 // Add CurrentNode predecessors to queue
@@ -176,16 +180,8 @@ graph_mem_pool::tryReuseExistingAllocation(
176180 // Mark node as visited
177181 CurrentNode->MTotalVisitedEdges = 1 ;
178182 }
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- }
187183
188- return AllocInfo ;
184+ return std:: nullopt ;
189185}
190186
191187void graph_mem_pool::markAllocationAsAvailable (
0 commit comments