[sprinkler] Eliminate std::string heap allocations (#13379)

This commit is contained in:
J. Nick Koston
2026-01-19 17:35:58 -10:00
committed by GitHub
parent 8998ef0bc3
commit 5d787e2512
2 changed files with 13 additions and 15 deletions
+9 -11
View File
@@ -327,14 +327,13 @@ SprinklerValveOperator *SprinklerValveRunRequest::valve_operator() { return this
SprinklerValveRunRequestOrigin SprinklerValveRunRequest::request_is_from() { return this->origin_; } SprinklerValveRunRequestOrigin SprinklerValveRunRequest::request_is_from() { return this->origin_; }
Sprinkler::Sprinkler() {} Sprinkler::Sprinkler() : Sprinkler("") {}
Sprinkler::Sprinkler(const std::string &name) { Sprinkler::Sprinkler(const char *name) : name_(name) {
// The `name` is needed to set timers up, hence non-default constructor // The `name` is stored for dump_config logging
// replaces `set_name()` method previously existed
this->name_ = name;
this->timer_.init(2); this->timer_.init(2);
this->timer_.push_back({this->name_ + "sm", false, 0, 0, std::bind(&Sprinkler::sm_timer_callback_, this)}); // Timer names only need to be unique within this component instance
this->timer_.push_back({this->name_ + "vs", false, 0, 0, std::bind(&Sprinkler::valve_selection_callback_, this)}); this->timer_.push_back({"sm", false, 0, 0, std::bind(&Sprinkler::sm_timer_callback_, this)});
this->timer_.push_back({"vs", false, 0, 0, std::bind(&Sprinkler::valve_selection_callback_, this)});
} }
void Sprinkler::setup() { this->all_valves_off_(true); } void Sprinkler::setup() { this->all_valves_off_(true); }
@@ -1575,8 +1574,7 @@ const LogString *Sprinkler::state_as_str_(SprinklerState state) {
void Sprinkler::start_timer_(const SprinklerTimerIndex timer_index) { void Sprinkler::start_timer_(const SprinklerTimerIndex timer_index) {
if (this->timer_duration_(timer_index) > 0) { if (this->timer_duration_(timer_index) > 0) {
// FixedVector ensures timer_ can't be resized, so .c_str() pointers remain valid this->set_timeout(this->timer_[timer_index].name, this->timer_duration_(timer_index),
this->set_timeout(this->timer_[timer_index].name.c_str(), this->timer_duration_(timer_index),
this->timer_cbf_(timer_index)); this->timer_cbf_(timer_index));
this->timer_[timer_index].start_time = millis(); this->timer_[timer_index].start_time = millis();
this->timer_[timer_index].active = true; this->timer_[timer_index].active = true;
@@ -1587,7 +1585,7 @@ void Sprinkler::start_timer_(const SprinklerTimerIndex timer_index) {
bool Sprinkler::cancel_timer_(const SprinklerTimerIndex timer_index) { bool Sprinkler::cancel_timer_(const SprinklerTimerIndex timer_index) {
this->timer_[timer_index].active = false; this->timer_[timer_index].active = false;
return this->cancel_timeout(this->timer_[timer_index].name.c_str()); return this->cancel_timeout(this->timer_[timer_index].name);
} }
bool Sprinkler::timer_active_(const SprinklerTimerIndex timer_index) { return this->timer_[timer_index].active; } bool Sprinkler::timer_active_(const SprinklerTimerIndex timer_index) { return this->timer_[timer_index].active; }
@@ -1618,7 +1616,7 @@ void Sprinkler::sm_timer_callback_() {
} }
void Sprinkler::dump_config() { void Sprinkler::dump_config() {
ESP_LOGCONFIG(TAG, "Sprinkler Controller -- %s", this->name_.c_str()); ESP_LOGCONFIG(TAG, "Sprinkler Controller -- %s", this->name_);
if (this->manual_selection_delay_.has_value()) { if (this->manual_selection_delay_.has_value()) {
ESP_LOGCONFIG(TAG, " Manual Selection Delay: %" PRIu32 " seconds", this->manual_selection_delay_.value_or(0)); ESP_LOGCONFIG(TAG, " Manual Selection Delay: %" PRIu32 " seconds", this->manual_selection_delay_.value_or(0));
} }
+4 -4
View File
@@ -11,7 +11,7 @@
namespace esphome::sprinkler { namespace esphome::sprinkler {
const std::string MIN_STR = "min"; inline constexpr const char *MIN_STR = "min";
enum SprinklerState : uint8_t { enum SprinklerState : uint8_t {
// NOTE: these states are used by both SprinklerValveOperator and Sprinkler (the controller)! // NOTE: these states are used by both SprinklerValveOperator and Sprinkler (the controller)!
@@ -49,7 +49,7 @@ struct SprinklerQueueItem {
}; };
struct SprinklerTimer { struct SprinklerTimer {
const std::string name; const char *name;
bool active; bool active;
uint32_t time; uint32_t time;
uint32_t start_time; uint32_t start_time;
@@ -176,7 +176,7 @@ class SprinklerValveRunRequest {
class Sprinkler : public Component { class Sprinkler : public Component {
public: public:
Sprinkler(); Sprinkler();
Sprinkler(const std::string &name); Sprinkler(const char *name);
void setup() override; void setup() override;
void loop() override; void loop() override;
void dump_config() override; void dump_config() override;
@@ -504,7 +504,7 @@ class Sprinkler : public Component {
uint32_t start_delay_{0}; uint32_t start_delay_{0};
uint32_t stop_delay_{0}; uint32_t stop_delay_{0};
std::string name_; const char *name_{""};
/// Sprinkler controller state /// Sprinkler controller state
SprinklerState state_{IDLE}; SprinklerState state_{IDLE};