Move PerThreadState to per-thread arenas

This commit is contained in:
2025-09-03 13:16:35 -04:00
parent f067f4e85b
commit 54d06c654f

View File

@@ -244,7 +244,16 @@ template <> struct Family<Counter>::State {
std::string_view help;
struct PerThreadState {
std::unordered_map<LabelsKey, Counter::State *> instances;
std::unordered_map<
LabelsKey, Counter::State *, std::hash<LabelsKey>,
std::equal_to<LabelsKey>,
ArenaStlAllocator<std::pair<const LabelsKey, Counter::State *>>>
instances;
explicit PerThreadState(ArenaAllocator &arena)
: instances(
ArenaStlAllocator<std::pair<const LabelsKey, Counter::State *>>(
&arena)) {}
};
std::unordered_map<std::thread::id, PerThreadState> per_thread_state;
@@ -296,7 +305,16 @@ template <> struct Family<Histogram>::State {
ArenaVector<double> buckets;
struct PerThreadState {
std::unordered_map<LabelsKey, Histogram::State *> instances;
std::unordered_map<
LabelsKey, Histogram::State *, std::hash<LabelsKey>,
std::equal_to<LabelsKey>,
ArenaStlAllocator<std::pair<const LabelsKey, Histogram::State *>>>
instances;
explicit PerThreadState(ArenaAllocator &arena)
: instances(
ArenaStlAllocator<std::pair<const LabelsKey, Histogram::State *>>(
&arena)) {}
};
std::unordered_map<std::thread::id, PerThreadState> 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<Counter>::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<Counter::State>();
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<Histogram>::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<Histogram::State>();