FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

removed objectpool · medengineer/cpp-taskflow@808956a · GitHub

Repository navigation

Commit 808956a

Browse files
removed objectpool
1 parent c65b0c5 commit 808956a

3 files changed

Lines changed: 132 additions & 76 deletions

File tree

‎example/simple.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <taskflow/taskflow.hpp> // the only include you need
77

88
int main(){
9-
9+
1010
tf::Executor executor;
1111
tf::Taskflow taskflow;
1212

‎taskflow/core/executor.hpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -616,9 +616,9 @@ inline void Executor::_invoke(unsigned me, Node* node) {
616616
// access caused by topology clear.
617617
const auto num_successors = node->num_successors();
618618

619-
// regular node type
619+
// static task
620620
// The default node work type. We only need to execute the callback if any.
621-
if(auto index=node->_work.index(); index == 0) {
621+
if(auto index=node->_work.index(); index == 1) {
622622
if(node->_module != nullptr) {
623623
bool first_time = !node->is_spawned();
624624
_invoke_static_work(me, node);
@@ -632,8 +632,8 @@ inline void Executor::_invoke(unsigned me, Node* node) {
632632
}
633633
}
634634
}
635-
// subflow node type
636-
else {
635+
// dynamic task
636+
else if (index == 2){
637637

638638
// Clear the subgraph before the task execution
639639
if(!node->is_spawned()) {
@@ -687,7 +687,7 @@ inline void Executor::_invoke(unsigned me, Node* node) {
687687
if(!node->is_subtask()) {
688688
// Only dynamic tasking needs to restore _dependents
689689
// TODO:
690-
if(node->_work.index() == 1 && !node->_subgraph->empty()) {
690+
if(node->_work.index() == 2 && !node->_subgraph->empty()) {
691691
while(!node->_dependents.empty() && node->_dependents.back()->is_subtask()) {
692692
node->_dependents.pop_back();
693693
}

‎taskflow/core/graph.hpp‎

Lines changed: 126 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

33
#include "../error/error.hpp"
4-
#include "../utility/object_pool.hpp"
54
#include "../utility/traits.hpp"
65
#include "../utility/passive_vector.hpp"
76

@@ -15,19 +14,16 @@ class FlowBuilder;
1514
class Subflow;
1615
class Taskflow;
1716

18-
// TLS node pool
19-
inline thread_local ObjectPool<Node> NodePool;
20-
2117
// ----------------------------------------------------------------------------
2218

2319
// Class: Graph
2420
class Graph {
21+
22+
friend class Node;
2523

2624
public:
2725

2826
Graph() = default;
29-
~Graph();
30-
3127
Graph(const Graph&) = delete;
3228
Graph(Graph&&);
3329

@@ -40,15 +36,17 @@ class Graph {
4036

4137
size_t size() const;
4238

43-
template <typename... ArgsT>
44-
Node& emplace_back(ArgsT&&...);
39+
template <typename C>
40+
Node& emplace_back(C&&);
41+
42+
Node& emplace_back();
4543

4644
std::vector<std::unique_ptr<Node>>& nodes();
4745

4846
const std::vector<std::unique_ptr<Node>>& nodes() const;
4947

5048
private:
51-
49+
5250
std::vector<std::unique_ptr<Node>> _nodes;
5351
};
5452

@@ -78,15 +76,9 @@ class Node {
7876

7977
~Node();
8078

81-
template <typename C>
82-
void animate(C&&);
83-
84-
void animate();
85-
void recycle();
8679
void precede(Node&);
8780
void dump(std::ostream&) const;
88-
void clear_subgraph();
89-
81+
9082
size_t num_successors() const;
9183
size_t num_dependents() const;
9284

@@ -107,11 +99,11 @@ class Node {
10799
private:
108100

109101
std::string _name;
110-
std::variant<StaticWork, DynamicWork> _work;
102+
std::variant<std::monostate, StaticWork, DynamicWork> _work;
111103

112104
tf::PassiveVector<Node*> _successors;
113105
tf::PassiveVector<Node*> _dependents;
114-
106+
115107
std::optional<Graph> _subgraph;
116108

117109
Topology* _topology {nullptr};
@@ -129,50 +121,25 @@ Node::Node(C&& c) : _work {std::forward<C>(c)} {
129121

130122
// Destructor
131123
inline Node::~Node() {
132-
clear_subgraph();
133-
}
134-
135-
// Constructor-like call
136-
inline void Node::animate() {
137-
}
138-
139-
// Constructor-like call
140-
template <typename C>
141-
void Node::animate(C&& c) {
142-
_work = std::forward<C>(c);
143-
}
144-
145-
// Destructor-like call
146-
inline void Node::recycle() {
147-
_name.clear();
148-
_work.emplace<StaticWork>();
149-
_successors.clear();
150-
_dependents.clear();
151-
_topology = nullptr;
152-
_module = nullptr;
153-
_status = 0;
154-
_num_dependents.store(0, std::memory_order_relaxed);
155-
clear_subgraph();
156-
}
157-
158-
inline void Node::clear_subgraph() {
159124
// this is to avoid stack overflow
160125
if(_subgraph.has_value()) {
161-
std::list<Graph> gs;
162-
gs.push_back(std::move(*_subgraph));
126+
std::vector<std::unique_ptr<Node>> nodes;
127+
std::move(
128+
_subgraph->_nodes.begin(), _subgraph->_nodes.end(), std::back_inserter(nodes)
129+
);
130+
_subgraph->_nodes.clear();
163131
_subgraph.reset();
164-
auto i=gs.begin();
165-
while(i!=gs.end()) {
166-
auto n = i->nodes().begin();
167-
while(n != i->nodes().end()) {
168-
if((*n)->_subgraph.has_value()) {
169-
gs.push_back(std::move(*((*n)->_subgraph)));
170-
(*n)->_subgraph.reset();
171-
}
172-
++n;
173-
}
174-
++i;
175-
}
132+
size_t i = 0;
133+
while(i < nodes.size()) {
134+
if(auto& sbg = nodes[i]->_subgraph; sbg) {
135+
std::move(
136+
sbg->_nodes.begin(), sbg->_nodes.end(), std::back_inserter(nodes)
137+
);
138+
sbg->_nodes.clear();
139+
sbg.reset();
140+
}
141+
++i;
142+
}
176143
}
177144
}
178145

@@ -239,16 +206,101 @@ inline void Node::dump(std::ostream& os) const {
239206

240207
// ----------------------------------------------------------------------------
241208

209+
/*// Class: NodePool
210+
class NodePool {
211+
212+
public:
213+
214+
template <typename C>
215+
std::unique_ptr<Node> acquire(C&&);
216+
217+
std::unique_ptr<Node> acquire();
218+
219+
void release(std::unique_ptr<Node>);
220+
221+
private:
222+
223+
//std::mutex _mutex;
224+
225+
std::vector<std::unique_ptr<Node>> _nodes;
226+
227+
void _recycle(Node&);
228+
};
229+
230+
// Function: acquire
231+
template <typename C>
232+
inline std::unique_ptr<Node> NodePool::acquire(C&& c) {
233+
if(_nodes.empty()) {
234+
return std::make_unique<Node>(std::forward<C>(c));
235+
}
236+
else {
237+
auto node = std::move(_nodes.back());
238+
node->_work = std::forward<C>(c);
239+
_nodes.pop_back();
240+
return node;
241+
}
242+
}
243+
244+
// Function: acquire
245+
inline std::unique_ptr<Node> NodePool::acquire() {
246+
if(_nodes.empty()) {
247+
return std::make_unique<Node>();
248+
}
249+
else {
250+
auto node = std::move(_nodes.back());
251+
_nodes.pop_back();
252+
return node;
253+
}
254+
}
255+
256+
// Procedure: release
257+
inline void NodePool::release(std::unique_ptr<Node> node) {
258+
259+
return;
260+
261+
//assert(node);
262+
if(_nodes.size() >= 65536) {
263+
return;
264+
}
265+
266+
auto children = node->_extract_children();
267+
268+
for(auto& child : children) {
269+
_recycle(*child);
270+
}
271+
_recycle(*node);
272+
273+
std::move(children.begin(), children.end(), std::back_inserter(_nodes));
274+
_nodes.push_back(std::move(node));
275+
}
276+
277+
// Procedure: _recycle
278+
inline void NodePool::_recycle(Node& node) {
279+
node._name.clear();
280+
node._work = {};
281+
node._successors.clear();
282+
node._dependents.clear();
283+
node._topology = nullptr;
284+
node._module = nullptr;
285+
node._status = 0;
286+
node._num_dependents.store(0, std::memory_order_relaxed);
287+
//assert(!node._subgraph);
288+
}
289+
290+
// ----------------------------------------------------------------------------
291+
292+
namespace this_thread {
293+
inline thread_local NodePool nodepool;
294+
}
295+
*/
296+
297+
// ----------------------------------------------------------------------------
298+
242299
// Move constructor
243300
inline Graph::Graph(Graph&& other) :
244301
_nodes {std::move(other._nodes)} {
245302
}
246303

247-
// Destructor
248-
inline Graph::~Graph() {
249-
clear();
250-
}
251-
252304
// Move assignment
253305
inline Graph& Graph::operator = (Graph&& other) {
254306
_nodes = std::move(other._nodes);
@@ -258,9 +310,6 @@ inline Graph& Graph::operator = (Graph&& other) {
258310
// Procedure: clear
259311
// clear and recycle the nodes
260312
inline void Graph::clear() {
261-
for(auto& node : _nodes) {
262-
NodePool.release(std::move(node));
263-
}
264313
_nodes.clear();
265314
}
266315

@@ -292,9 +341,16 @@ inline const std::vector<std::unique_ptr<Node>>& Graph::nodes() const {
292341

293342
// Function: emplace_back
294343
// create a node from a give argument; constructor is called if necessary
295-
template <typename... ArgsT>
296-
Node& Graph::emplace_back(ArgsT&&... args) {
297-
_nodes.push_back(NodePool.acquire(std::forward<ArgsT>(args)...));
344+
template <typename C>
345+
Node& Graph::emplace_back(C&& c) {
346+
_nodes.push_back(std::make_unique<Node>(std::forward<C>(c)));
347+
return *(_nodes.back());
348+
}
349+
350+
// Function: emplace_back
351+
// create a node from a give argument; constructor is called if necessary
352+
inline Node& Graph::emplace_back() {
353+
_nodes.push_back(std::make_unique<Node>());
298354
return *(_nodes.back());
299355
}
300356

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL