diff --git a/src/metric.cpp b/src/metric.cpp index 836360b..bb278b5 100644 --- a/src/metric.cpp +++ b/src/metric.cpp @@ -244,7 +244,16 @@ template <> struct Family::State { std::string_view help; struct PerThreadState { - std::unordered_map instances; + std::unordered_map< + LabelsKey, Counter::State *, std::hash, + std::equal_to, + ArenaStlAllocator>> + instances; + + explicit PerThreadState(ArenaAllocator &arena) + : instances( + ArenaStlAllocator>( + &arena)) {} }; std::unordered_map per_thread_state; @@ -296,7 +305,16 @@ template <> struct Family::State { ArenaVector buckets; struct PerThreadState { - std::unordered_map instances; + std::unordered_map< + LabelsKey, Histogram::State *, std::hash, + std::equal_to, + ArenaStlAllocator>> + instances; + + explicit PerThreadState(ArenaAllocator &arena) + : instances( + ArenaStlAllocator>( + &arena)) {} }; std::unordered_map per_thread_state; @@ -588,9 +606,16 @@ struct Metric { // Ensure thread state exists auto thread_id = std::this_thread::get_id(); - // Thread state is automatically created by operator[] + auto per_thread_it = family->p->per_thread_state.find(thread_id); + if (per_thread_it == family->p->per_thread_state.end()) { + // Create new PerThreadState with thread-local arena + auto result = family->p->per_thread_state.emplace( + thread_id, + Family::State::PerThreadState(get_thread_local_arena())); + per_thread_it = result.first; + } - auto &ptr = family->p->per_thread_state[thread_id].instances[key]; + auto &ptr = per_thread_it->second.instances[key]; if (!ptr) { ptr = get_thread_local_arena().construct(); ptr->value = 0.0; @@ -642,9 +667,16 @@ struct Metric { // Ensure thread state exists auto thread_id = std::this_thread::get_id(); - // Thread state is automatically created by operator[] + auto per_thread_it = family->p->per_thread_state.find(thread_id); + if (per_thread_it == family->p->per_thread_state.end()) { + // Create new PerThreadState with thread-local arena + auto result = family->p->per_thread_state.emplace( + thread_id, + Family::State::PerThreadState(get_thread_local_arena())); + per_thread_it = result.first; + } - auto &ptr = family->p->per_thread_state[thread_id].instances[key]; + auto &ptr = per_thread_it->second.instances[key]; if (!ptr) { ptr = get_thread_local_arena().construct();