Skip to content

Commit

Permalink
increment counter only when necessary to avoid fragmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed Dec 6, 2023
1 parent e59b448 commit f92e7a4
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions include/dlplan/utils/factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ReferenceCountedObjectFactory {
std::tuple<std::shared_ptr<PerTypeCache<Ts>>...> m_cache;

// Identifiers are shared since types can be polymorphic
int m_count = -1;
int m_count = 0;

public:
ReferenceCountedObjectFactory()
Expand All @@ -61,7 +61,7 @@ class ReferenceCountedObjectFactory {
template<typename T, typename... Args>
GetOrCreateResult<T> get_or_create(Args&&... args) {
auto& t_cache = std::get<std::shared_ptr<PerTypeCache<T>>>(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<T>(new T(identifier, std::forward<Args>(args)...));
/* we must declare sp before locking the mutex
Expand All @@ -71,8 +71,8 @@ class ReferenceCountedObjectFactory {
sp = cached.lock();
// std::lock_guard<std::mutex> 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<T>(
Expand Down

0 comments on commit f92e7a4

Please sign in to comment.