This commit is contained in:
J. Nick Koston 2025-07-08 09:40:12 -06:00
parent 7d2726ab21
commit 07a4f6f53c
No known key found for this signature in database
2 changed files with 13 additions and 9 deletions

View File

@ -58,10 +58,10 @@ void RuntimeStatsCollector::log_stats_() {
// Log top components by period runtime
for (const auto &it : stats_to_display) {
const std::string &source = it.name;
const char *source = it.name;
const ComponentRuntimeStats *stats = it.stats;
ESP_LOGI(TAG, " %s: count=%" PRIu32 ", avg=%.2fms, max=%" PRIu32 "ms, total=%" PRIu32 "ms", source.c_str(),
ESP_LOGI(TAG, " %s: count=%" PRIu32 ", avg=%.2fms, max=%" PRIu32 "ms, total=%" PRIu32 "ms", source,
stats->get_period_count(), stats->get_period_avg_time_ms(), stats->get_period_max_time_ms(),
stats->get_period_time_ms());
}
@ -76,10 +76,10 @@ void RuntimeStatsCollector::log_stats_() {
});
for (const auto &it : stats_to_display) {
const std::string &source = it.name;
const char *source = it.name;
const ComponentRuntimeStats *stats = it.stats;
ESP_LOGI(TAG, " %s: count=%" PRIu32 ", avg=%.2fms, max=%" PRIu32 "ms, total=%" PRIu32 "ms", source.c_str(),
ESP_LOGI(TAG, " %s: count=%" PRIu32 ", avg=%.2fms, max=%" PRIu32 "ms, total=%" PRIu32 "ms", source,
stats->get_total_count(), stats->get_total_avg_time_ms(), stats->get_total_max_time_ms(),
stats->get_total_time_ms());
}

View File

@ -5,9 +5,9 @@
#ifdef USE_RUNTIME_STATS
#include <map>
#include <string>
#include <vector>
#include <cstdint>
#include <cstring>
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
@ -79,7 +79,7 @@ class ComponentRuntimeStats {
// For sorting components by run time
struct ComponentStatPair {
std::string name;
const char *name;
const ComponentRuntimeStats *stats;
bool operator>(const ComponentStatPair &other) const {
@ -109,9 +109,13 @@ class RuntimeStatsCollector {
}
}
// Back to string keys, but we'll cache the source name per component
std::map<std::string, ComponentRuntimeStats> component_stats_;
std::map<Component *, std::string> component_names_cache_;
// Use const char* keys for efficiency
// Custom comparator for const char* keys in map
struct CStrCompare {
bool operator()(const char *a, const char *b) const { return std::strcmp(a, b) < 0; }
};
std::map<const char *, ComponentRuntimeStats, CStrCompare> component_stats_;
std::map<Component *, const char *> component_names_cache_;
uint32_t log_interval_;
uint32_t next_log_time_;
};