Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions parametric/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <size_t i>
Expand Down
22 changes: 18 additions & 4 deletions parametric/dag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 <typename Derived>
Expand Down
20 changes: 19 additions & 1 deletion tests/customclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,22 @@ TEST(CustomClass, void_function){

//reset
MyVoidFun::value = 0.;
}
}
// 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<CustomComputer>(new CustomComputer(2.0));
using Results = std::tuple<parametric::param<double>, parametric::param<double>>;
auto results = std::make_unique<Results>(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<double>(0));

auto extra = parametric::new_param("extra", 0.0);
ptr->computes(extra);

EXPECT_EQ(nullptr, ptr->res<double>(0));
EXPECT_NE(nullptr, ptr->res<double>(1));
EXPECT_EQ(div_result.node_pointer(), ptr->res<double>(1));
}
14 changes: 14 additions & 0 deletions tests/dag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Loading