From f92e7a4e8005f2a9fda68b4788d8b48a479b59aa Mon Sep 17 00:00:00 2001 From: Dominik Drexler Date: Wed, 6 Dec 2023 20:33:45 +0100 Subject: [PATCH] increment counter only when necessary to avoid fragmentation --- include/dlplan/utils/factory.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/dlplan/utils/factory.h b/include/dlplan/utils/factory.h index c918ff46..b58147f5 100644 --- a/include/dlplan/utils/factory.h +++ b/include/dlplan/utils/factory.h @@ -47,7 +47,7 @@ class ReferenceCountedObjectFactory { std::tuple>...> m_cache; // Identifiers are shared since types can be polymorphic - int m_count = -1; + int m_count = 0; public: ReferenceCountedObjectFactory() @@ -61,7 +61,7 @@ class ReferenceCountedObjectFactory { template GetOrCreateResult get_or_create(Args&&... args) { auto& t_cache = std::get>>(m_cache); - int identifier = ++m_count; + int identifier = m_count; /* Must explicitly call the constructor of T to give exclusive access to the factory. */ auto key = std::shared_ptr(new T(identifier, std::forward(args)...)); /* we must declare sp before locking the mutex @@ -71,8 +71,8 @@ class ReferenceCountedObjectFactory { sp = cached.lock(); // std::lock_guard hold(t_cache->mutex); bool new_insertion = false; - if (!sp) { + ++m_count; new_insertion = true; t_cache->identifier_to_key.emplace(identifier, key); cached = sp = std::shared_ptr(