Skip to content

Commit d24b490

Browse files
committed
progress
1 parent 88090f7 commit d24b490

4 files changed

Lines changed: 93 additions & 44 deletions

File tree

‎include/react/Event.h‎

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -263,17 +263,41 @@ class EventSlot : public Event<E>
263263
private:
264264
void AddInput(const Event<E>& input)
265265
{
266-
SameGroupOrLink(input)
266+
using REACT_IMPL::NodeId;
267+
using SlotNodeType = REACT_IMPL::EventSlotNode<E>;
268+
269+
SlotNodeType* castedPtr = static_cast<SlotNodeType*>(this->GetNodePtr().get());
270+
271+
NodeId nodeId = castedPtr->GetInputNodeId();
272+
auto& graphPtr = GetInternals(this->GetGroup()).GetGraphPtr();
267273

274+
graphPtr->AddInput(nodeId, [castedPtr, &input] { castedPtr->AddInput(input); });
275+
}
276+
277+
void RemoveInput(const Event<E>& input)
278+
{
268279
using REACT_IMPL::NodeId;
269-
using REACT_IMPL::ReactiveGraph;
270280
using SlotNodeType = REACT_IMPL::EventSlotNode<E>;
271281

272282
SlotNodeType* castedPtr = static_cast<SlotNodeType*>(this->GetNodePtr().get());
273283

274284
NodeId nodeId = castedPtr->GetInputNodeId();
275-
auto& graphPtr = NodePtr()->GraphPtr();
276-
graphPtr->AddInput(nodeId, [castedPtr, &newInput] { castedPtr->SetInput(PrivateNodeInterface::NodePtr(newInput)); });
285+
auto& graphPtr = GetInternals(this->GetGroup()).GetGraphPtr();
286+
287+
graphPtr->AddInput(nodeId, [castedPtr, &input] { castedPtr->RemoveInput(input); });
288+
}
289+
290+
void RemoveAllInputs()
291+
{
292+
using REACT_IMPL::NodeId;
293+
using SlotNodeType = REACT_IMPL::EventSlotNode<E>;
294+
295+
SlotNodeType* castedPtr = static_cast<SlotNodeType*>(this->GetNodePtr().get());
296+
297+
NodeId nodeId = castedPtr->GetInputNodeId();
298+
auto& graphPtr = GetInternals(this->GetGroup()).GetGraphPtr();
299+
300+
graphPtr->AddInput(nodeId, [castedPtr] { castedPtr->RemoveAllInputs(); });
277301
}
278302
};
279303

@@ -295,22 +319,33 @@ class EventLink : public Event<E>
295319
EventLink::Event( REACT_IMPL::CtorTag{ }, GetOrCreateLinkNode(group, input) )
296320
{ }
297321

322+
~EventLink()
323+
{
324+
auto nodePtr = GetNodePtr();
325+
}
326+
298327
protected:
299328
static auto GetOrCreateLinkNode(const Group& group, const Event<E>& input) -> decltype(auto)
300329
{
301330
using REACT_IMPL::EventLinkNode;
302331

303-
const void* inputPtr = GetInternals(input.GetGroup()).GetGraphPtr().get();
304-
const void* targetPtr = GetInternals(group).GetGraphPtr().get();
332+
auto targetGraphPtr = GetInternals(group).GetGraphPtr();
333+
334+
void* k1 = GetInternals(input.GetGroup()).GetGraphPtr().get();
335+
void* k2 = GetInternals(input).GetNodePtr().get();
305336

306-
{
337+
auto& linkCache = targetGraphPtr->GetLinkCache();
307338

308-
}
339+
auto nodePtr = linkCache.LookupOrCreate<EventLinkNode<E>>(
340+
{ k1, k2 },
341+
[&]
342+
{
343+
auto nodePtr = std::make_shared<EventLinkNode<E>>(group, input);
344+
nodePtr->SetWeakSelfPtr(std::weak_ptr<EventLinkNode<E>>{ nodePtr });
345+
return nodePtr;
346+
});
309347

310-
auto nodePtr = std::make_shared<EventLinkNode<E>>(group, input);
311-
auto weakPtr =
312-
nodePtr->SetWeakSelfPtr(std::weak_ptr<EventLinkNode<E>>{ nodePtr });
313-
return node;
348+
return nodePtr;
314349
}
315350
};
316351

‎include/react/detail/graph/EventNodes.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,13 @@ class EventSlotNode : public EventStreamNode<E>
261261

262262
virtual UpdateResult Update(TurnId turnId, size_t successorCount) override
263263
{
264-
for (const auto& e : deps_)
264+
for (auto& e : inputs_)
265265
{
266266
const auto& events = GetInternals(e).Events();
267267
this->Events().insert(this->Events().end(), events.begin(), events.end());
268-
}
269268

270-
slotInput_.dep->DecrementPendingSuccessorCount();
269+
GetInternals(e).DecrementPendingSuccessorCount();
270+
}
271271

272272
if (! this->Events().empty())
273273
{

‎include/react/detail/graph/GraphBase.h‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ class NodeBase : public IReactiveNode
8383
void DetachFromMe(NodeId otherNodeId)
8484
{ GetGraphPtr()->OnNodeDetach(nodeId_, otherNodeId); }
8585

86-
void DynamicAttachToMe(NodeId otherNodeId, TurnId turnId)
87-
{ GetGraphPtr()->OnDynamicNodeAttach(nodeId_, otherNodeId, turnId); }
88-
89-
void DynamicDetachFromMe(NodeId otherNodeId, TurnId turnId)
90-
{ GetGraphPtr()->OnDynamicNodeDetach(nodeId_, otherNodeId, turnId); }
91-
9286
private:
9387
NodeId nodeId_;
9488

‎include/react/detail/graph/PropagationST.h‎

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <type_traits>
1717
#include <utility>
1818
#include <vector>
19+
#include <map>
20+
#include <mutex>
1921

2022
#include <tbb/concurrent_queue.h>
2123
#include <tbb/task.h>
@@ -30,27 +32,49 @@ template <typename K>
3032
class PtrCache
3133
{
3234
public:
33-
void Add(const K& key, const std::shared_ptr<void>& ptr)
35+
template <typename V, typename F>
36+
std::shared_ptr<V> LookupOrCreate(const K& key, F&& createFunc)
3437
{
35-
auto res = map1_.insert(key, ptr);
36-
if (res.first == true)
38+
std::lock_guard<std::mutex> scopedLock(mutex_);
39+
40+
auto it = map1_.find(key);
41+
42+
if (it != map1_.end())
3743
{
38-
map2_.insert(ptr , res.second);
39-
return true;
44+
if (auto ptr = it->second.lock())
45+
{
46+
return std::static_pointer_cast<V>(ptr);
47+
}
4048
}
41-
49+
50+
std::shared_ptr<V> v = createFunc();
51+
auto res = map1_.insert({ key, std::weak_ptr<void>{ v } });
52+
map2_[v.get()] = res.first;
53+
return v;
4254
}
4355

44-
void Remove(const std::shared_ptr<void>& ptr)
56+
template <typename V>
57+
void Erase(const std::shared_ptr<V>& ptr)
4558
{
46-
map2_.remove();
59+
std::lock_guard<std::mutex> scopedLock(mutex_);
60+
61+
auto it = map2_.find((void*)ptr.get());
62+
63+
if (it != map2_.end())
64+
{
65+
map1_.erase(it->second);
66+
map2_.erase(it);
67+
}
4768
}
4869

4970
private:
50-
std::mutex lock_;
71+
std::mutex mutex_;
72+
73+
using Map1Type = std::map<K, std::weak_ptr<void>>;
74+
using Map2Type = std::unordered_map<void*, typename Map1Type::iterator>;
5175

52-
std::map<K, std::weak_ptr<void>> map1_;
53-
std::unordered_map<void*, std::map::iterator> map2_;
76+
Map1Type map1_;
77+
Map2Type map2_;
5478
};
5579

5680
class ReactiveGraph;
@@ -115,6 +139,8 @@ class TransactionQueue
115139
class ReactiveGraph
116140
{
117141
public:
142+
using LinkCacheType = PtrCache<std::pair<void*, void*>>;
143+
118144
NodeId RegisterNode(IReactiveNode* nodePtr, NodeCategory category);
119145
void UnregisterNode(NodeId nodeId);
120146

@@ -129,6 +155,9 @@ class ReactiveGraph
129155

130156
template <typename F>
131157
void EnqueueTransaction(TransactionFlags flags, F&& transactionCallback);
158+
159+
LinkCacheType& GetLinkCache()
160+
{ return linkCache_; }
132161

133162
private:
134163
struct NodeData
@@ -189,7 +218,9 @@ class ReactiveGraph
189218
TopoQueue scheduledNodes_;
190219
IndexMap<NodeData> nodeData_;
191220
std::vector<NodeId> changedInputs_;
192-
LinkOutputMap scheduledLinkOutputs_;
221+
LinkOutputMap scheduledLinkOutputs_;
222+
223+
LinkCacheType linkCache_;
193224

194225
bool isTransactionActive_ = false;
195226
};
@@ -202,7 +233,6 @@ NodeId ReactiveGraph::RegisterNode(IReactiveNode* nodePtr, NodeCategory category
202233
void ReactiveGraph::UnregisterNode(NodeId nodeId)
203234
{
204235
nodeData_.Remove(nodeId);
205-
206236
}
207237

208238
void ReactiveGraph::OnNodeAttach(NodeId nodeId, NodeId parentId)
@@ -224,16 +254,6 @@ void ReactiveGraph::OnNodeDetach(NodeId nodeId, NodeId parentId)
224254
successors.erase(std::find(successors.begin(), successors.end(), nodeId));
225255
}
226256

227-
void ReactiveGraph::OnDynamicNodeAttach(NodeId nodeId, NodeId parentId, TurnId turnId)
228-
{
229-
OnNodeAttach(nodeId, parentId);
230-
}
231-
232-
void ReactiveGraph::OnDynamicNodeDetach(NodeId nodeId, NodeId parentId, TurnId turnId)
233-
{
234-
OnNodeDetach(nodeId, parentId);
235-
}
236-
237257
template <typename F>
238258
void ReactiveGraph::AddInput(NodeId nodeId, F&& inputCallback)
239259
{

0 commit comments

Comments
 (0)