From 7fe4352786ba744c2d7212b20fa1489438215694 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 20 Oct 2026 15:33:38 -1000 Subject: [PATCH] [graph] Stop copying the trace name (#21211) --- esphome/components/graph/graph.cpp | 16 ++++++++-------- esphome/components/graph/graph.h | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/esphome/components/graph/graph.cpp b/esphome/components/graph/graph.cpp index e09eb385e32..147890fa29a 100644 --- a/esphome/components/graph/graph.cpp +++ b/esphome/components/graph/graph.cpp @@ -47,7 +47,7 @@ void HistoryData::take_sample(float data) { } void GraphTrace::init(Graph *g) { - ESP_LOGI(TAG, "Init trace for sensor %s", this->get_name().c_str()); + ESP_LOGI(TAG, "Init trace for sensor %s", this->name_); this->data_.init(g->get_width()); sensor_->add_on_state_callback([this](float state) { this->data_.take_sample(state); }); this->data_.set_update_time_ms(g->get_duration() * 1000 / g->get_width()); @@ -220,16 +220,16 @@ void GraphLegend::init(Graph *g) { int valw = 0, valh = 0; int lt = 0; for (auto *trace : g->traces_) { - std::string txtstr = trace->get_name(); + const char *txtstr = trace->name_; int fw, fos, fbl, fh; - this->font_label_->measure(txtstr.c_str(), &fw, &fos, &fbl, &fh); + this->font_label_->measure(txtstr, &fw, &fos, &fbl, &fh); if (fw > txtw) txtw = fw; if (fh > txth) txth = fh; if (trace->get_line_thickness() > lt) lt = trace->get_line_thickness(); - ESP_LOGI(TAGL, " %s %d %d", txtstr.c_str(), fw, fh); + ESP_LOGI(TAGL, " %s %d %d", txtstr, fw, fh); if (this->values_ != VALUE_POSITION_TYPE_NONE) { char valstr[VALUE_ACCURACY_MAX_LEN]; @@ -351,10 +351,10 @@ void Graph::draw_legend(display::Display *buff, uint16_t x_offset, uint16_t y_of int x = x_offset + legend_->x0_; int y = y_offset; for (auto *trace : traces_) { - std::string txtstr = trace->get_name(); - ESP_LOGV(TAG, " %s", txtstr.c_str()); + const char *txtstr = trace->name_; + ESP_LOGV(TAG, " %s", txtstr); - buff->printf(x, y, legend_->font_label_, trace->get_line_color(), TextAlign::TOP_CENTER, "%s", txtstr.c_str()); + buff->printf(x, y, legend_->font_label_, trace->get_line_color(), TextAlign::TOP_CENTER, "%s", txtstr); if (legend_->lines_) { uint16_t thick = trace->get_line_thickness(); @@ -393,7 +393,7 @@ void Graph::setup() { void Graph::dump_config() { for (auto *trace : traces_) { - ESP_LOGCONFIG(TAG, "Graph for sensor %s", trace->get_name().c_str()); + ESP_LOGCONFIG(TAG, "Graph for sensor %s", trace->name_); } } diff --git a/esphome/components/graph/graph.h b/esphome/components/graph/graph.h index dbedab6085d..0196c44e3bf 100644 --- a/esphome/components/graph/graph.h +++ b/esphome/components/graph/graph.h @@ -1,9 +1,9 @@ #pragma once #include -#include #include #include "esphome/components/sensor/sensor.h" #include "esphome/core/color.h" +#include "esphome/core/string_ref.h" #include "esphome/core/component.h" namespace esphome { @@ -108,7 +108,7 @@ class HistoryData { class GraphTrace final { public: void init(Graph *g); - void set_name(std::string name) { name_ = std::move(name); } + void set_name(const char *name) { this->name_ = name; } void set_sensor(sensor::Sensor *sensor) { sensor_ = sensor; } uint8_t get_line_thickness() { return this->line_thickness_; } void set_line_thickness(uint8_t val) { this->line_thickness_ = val; } @@ -118,12 +118,12 @@ class GraphTrace final { void set_line_color(Color val) { this->line_color_ = val; } bool get_continuous() { return this->continuous_; } void set_continuous(bool continuous) { this->continuous_ = continuous; } - std::string get_name() { return name_; } + StringRef get_name() const { return StringRef(this->name_); } const HistoryData *get_tracedata() { return &data_; } protected: sensor::Sensor *sensor_{nullptr}; - std::string name_; + const char *name_{""}; uint8_t line_thickness_{3}; enum LineType line_type_ { LINE_TYPE_SOLID }; Color line_color_{COLOR_ON};