Repository navigation
Description
Summary
A node that lives long (a parameter of an interactive model) keeps one dead std::weak_ptr in childs for every consumer that was ever attached to it, and each new attachment scans the whole list. After consumers are created and destroyed repeatedly (e.g. an editor that re-creates steps while the user edits), memory grows without bound and creating N consumers costs O(N²).
Reproducer (uses grunk static mode, links nothing but headers)
// parametric/grunk: a long-lived node keeps one (dead) weak_ptr per consumer that was ever attached to it, and attaching the next consumer
// scans the whole list -> memory grows without bound and creating N consumers costs O(N^2).
#include <chrono>
#include <cstdio>
#include <grunk/grunk.hpp>
int main() {
auto x = grunk::feature(1.0).with_id("x"); // long-lived parameter
using clock = std::chrono::steady_clock;
for (int round = 1; round <= 6; ++round) {
auto t0 = clock::now();
for (int i = 0; i < 20000; ++i) { // consumers created and dropped again (an editor re-creating a step)
auto y = grunk::action([](double v) { return v * 2.0; }, x);
(void)y.value();
} // y is destroyed here: no consumer is alive
const double ms = std::chrono::duration<double, std::milli>(clock::now() - t0).count();
std::printf("consumers created so far: %6d live consumers: 0 x.num_children() = %6zu last 20000 took %8.1f ms\n",
round * 20000, x.node_pointer()->num_children(), ms);
}
}Output
consumers created so far: 20000 live consumers: 0 x.num_children() = 20000 last 20000 took 216.3 ms
consumers created so far: 40000 live consumers: 0 x.num_children() = 40000 last 20000 took 741.6 ms
consumers created so far: 60000 live consumers: 0 x.num_children() = 60000 last 20000 took 1806.3 ms
consumers created so far: 80000 live consumers: 0 x.num_children() = 80000 last 20000 took 3416.7 ms
consumers created so far: 100000 live consumers: 0 x.num_children() = 100000 last 20000 took 4648.7 ms
consumers created so far: 120000 live consumers: 0 x.num_children() = 120000 last 20000 took 5856.1 ms
No consumer is alive at the end of each round, but num_children() equals the number of consumers ever created, and the time per 20 000 consumers keeps growing (quadratic overall).
Cause
add_parent(dag.hpp) only searchesparent->childsfor an existing entry (std::find_if, O(n)) and thenpush_backs; expired entries are skipped by the predicate but never erased.~DAGNode()callsremove_parent(*parent)which tries to erase the child withchild.lock().get() == this. During destruction the strong count is already 0, solock()returns an emptyshared_ptr, the entry is never found and stays in the parent forever.
Suggestion
Prune lazily: in add_parent/remove_parent use std::erase_if(childs, [](auto& w){ return w.expired(); }) (amortised), and/or compare with w.owner_before/a raw DAGNode* stored next to the weak_ptr so that the destructor can find and remove its own entry. invalidate()/accept() also keep visiting the dead entries.
Related: #22 (replacing a parameter), which would make this pattern more common. Another bug in the same destructor is reported separately.
Environment: grunk main (8af34a7, 0.5.0), parametric 0.3.5, sol2 3.5.0, Lua 5.4.8, GCC 15.3 (conda-forge), Linux x86-64; WebAssembly: Emscripten 3.1.58.
(Found while building a prototype GUI/viewer on top of grunk.)
Activity
fix #85
Merge pull request #88 from grunk-dev/85-dagnode-dead-child-entries-a…