diff --git a/libsrc/core/paje_trace.cpp b/libsrc/core/paje_trace.cpp index 9b4ac053..1a1c9303 100644 --- a/libsrc/core/paje_trace.cpp +++ b/libsrc/core/paje_trace.cpp @@ -942,7 +942,7 @@ namespace ngcore size_t imax_mem_allocated = 0; const auto & names = MemoryTracer::GetNames(); - const auto & tree = MemoryTracer::GetTree(); + const auto & parents = MemoryTracer::GetParents(); size_t N = names.size(); Array mem_allocated_id; @@ -1006,39 +1006,27 @@ namespace ngcore Array nodes; nodes.SetSize(N); nodes = nullptr; + Array> children(N); + Array sorting; // topological sorting (parents before children) sorting.SetAllocSize(N); - ArrayMem stack; - - // find root nodes in memory tracer tree, i.e. they have no parents - Array parents; - parents.SetSize(N); - parents = -1; - for( const auto & [iparent, children] : tree ) - for (auto child_id : children) - { - if(parents[child_id] != -1) - std::cerr << "Error in memory tracer: multiple parents found for " << names[child_id] << std::endl; - parents[child_id] = iparent; - } for(auto i : IntRange(1, N)) - if(parents[i]==-1) - { - sorting.Append(i); - if(tree.count(i)) - stack.Append(i); - } + children[parents[i]].Append(i); + + ArrayMem stack; + sorting.Append(0); + stack.Append(0); while(stack.Size()) { auto current = stack.Last(); stack.DeleteLast(); - for(const auto child : tree.at(current)) + for(const auto child : children[current]) { sorting.Append(child); - if(tree.count(child)) + if(children[child].Size()) stack.Append(child); } } diff --git a/libsrc/core/profiler.cpp b/libsrc/core/profiler.cpp index 66365321..33ef98f4 100644 --- a/libsrc/core/profiler.cpp +++ b/libsrc/core/profiler.cpp @@ -114,8 +114,8 @@ namespace ngcore NgProfiler prof; // NOLINT #ifdef NETGEN_TRACE_MEMORY - std::vector MemoryTracer::names{"root"}; - std::map< int, std::vector > MemoryTracer::tree; + std::vector MemoryTracer::names{"all"}; + std::vector MemoryTracer::parents{-1}; #endif // NETGEN_TRACE_MEMORY } // namespace ngcore diff --git a/libsrc/core/profiler.hpp b/libsrc/core/profiler.hpp index cdd945bd..208b7a4e 100644 --- a/libsrc/core/profiler.hpp +++ b/libsrc/core/profiler.hpp @@ -324,12 +324,13 @@ namespace ngcore { #ifdef NETGEN_TRACE_MEMORY NGCORE_API static std::vector names; - NGCORE_API static std::map< int, std::vector > tree; + NGCORE_API static std::vector parents; static int CreateId(const std::string& name) { int id = names.size(); names.push_back(name); + parents.push_back(0); if(id==10*NgProfiler::SIZE) std::cerr << "Allocated " << id << " MemoryTracer objects" << std::endl; return id; @@ -400,7 +401,7 @@ namespace ngcore void Track( T & obj, const std::string& name ) const { obj.GetMemoryTracer().Activate(obj, name); - tree[id].push_back(obj.GetMemoryTracer().GetId()); + parents[obj.GetMemoryTracer().GetId()] = id; } static std::string GetName(int id) @@ -433,7 +434,7 @@ namespace ngcore static const std::vector & GetNames() { return names; } - static const std::map> & GetTree() { return tree; } + static const std::vector & GetParents() { return parents; } #else // NETGEN_TRACE_MEMORY public: MemoryTracer() {}