diff --git a/Changelog.md b/Changelog.md index d7d579f..246a459 100644 --- a/Changelog.md +++ b/Changelog.md @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0 # Unreleased - Fix undefined behaviour in `DAGNode::~DAGNode()`, which erased from the parent list while iterating over it (#86) +- Fix dead child entries accumulating in long-lived nodes, which made attaching N consumers cost O(N^2) (#85) # v0.3.5 diff --git a/parametric/core.hpp b/parametric/core.hpp index 1f229c3..f48dce8 100644 --- a/parametric/core.hpp +++ b/parametric/core.hpp @@ -559,6 +559,10 @@ class ComputeNode add_parent(output.node_pointer(), ptr); } +protected: + /** @brief The i-th child is the i-th result, so expired results must keep their slot. */ + bool has_positional_children() const override { return true; } + private: template diff --git a/parametric/dag.hpp b/parametric/dag.hpp index fe60e7f..77d376c 100644 --- a/parametric/dag.hpp +++ b/parametric/dag.hpp @@ -96,11 +96,19 @@ class DAGNode throw std::runtime_error("Cannot attach node: cycles are not allowed."); } + // Drop entries of consumers that have been destroyed in the meantime, so that a long-lived + // node does not accumulate one dead weak_ptr per consumer ever attached to it. This is done + // here and not in ~DAGNode, because a node may be destroyed while its parent is traversing + // its child list. Nodes with positional children (outputs of compute nodes) must keep their slots. + if (!parent->has_positional_children()) { + parent->childs.erase( + std::remove_if(parent->childs.begin(), parent->childs.end(), + [](auto const& c) { return c.expired(); }), + parent->childs.end()); + } + auto predicate = [&child](auto const& c){ - if (!c.expired()) { - return c.lock() == child; - } - return false; + return c.lock() == child; }; if (std::find_if(std::begin(parent->childs), std::end(parent->childs), predicate ) == parent->childs.end()) { parent->childs.push_back(child); @@ -365,6 +373,12 @@ class DAGNode * specific invalidation logic of the node */ virtual void invalidateSelf() {} + + /** + * @brief Override this function to return true if the position of a child in the child list is + * meaningful, e.g. for the outputs of a compute node. Dead entries of such nodes are never pruned. + */ + virtual bool has_positional_children() const { return false; } }; template diff --git a/tests/customclass.cpp b/tests/customclass.cpp index f7fb2ca..0b6a1a7 100644 --- a/tests/customclass.cpp +++ b/tests/customclass.cpp @@ -208,4 +208,22 @@ TEST(CustomClass, void_function){ //reset MyVoidFun::value = 0.; -} \ No newline at end of file +} +// Attaching to a compute node must not shift its output slots when an earlier output is dead (#85) +TEST(CustomClass, deadOutputKeepsSlot) +{ + auto b = parametric::new_param("b", 4.0); + auto ptr = std::shared_ptr(new CustomComputer(2.0)); + using Results = std::tuple, parametric::param>; + auto results = std::make_unique(parametric::compute(ptr, b)); + auto div_result = std::get<1>(*results); + results.reset(); // drops the first output only, div_result keeps the second alive + ASSERT_EQ(nullptr, ptr->res(0)); + + auto extra = parametric::new_param("extra", 0.0); + ptr->computes(extra); + + EXPECT_EQ(nullptr, ptr->res(0)); + EXPECT_NE(nullptr, ptr->res(1)); + EXPECT_EQ(div_result.node_pointer(), ptr->res(1)); +} diff --git a/tests/dag.cpp b/tests/dag.cpp index 7a4955a..e29759e 100644 --- a/tests/dag.cpp +++ b/tests/dag.cpp @@ -110,3 +110,17 @@ TEST(DAG, destroyNodeWithDuplicateParents) add_parent(c, a); EXPECT_TRUE(a->precedes(*c)); } + +// Dead consumers must not pile up in a long-lived parent. See https://github.com/grunk-dev/parametric/issues/85 +TEST(DAG, deadChildrenArePruned) +{ + NodeRef x(new DAGNode("x")); + for (int i = 0; i < 1000; ++i) { + NodeRef c(new DAGNode("c")); + add_parent(c, x); + } + NodeRef alive(new DAGNode("alive")); + add_parent(alive, x); + EXPECT_EQ(1, x->num_children()); + EXPECT_TRUE(x->precedes(*alive)); +}