From 0b02476e8a99bae1122b73af70d37f3734534938 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 10 Feb 2026 21:01:34 -0600 Subject: [PATCH] [light] Eliminate redundant clamp in LightCall::validate_() and normalize_color() After clamp_and_log_if_invalid() clamps the value in-place, the LightColorValues setter's clamp() is guaranteed to be a no-op. For 5 of 9 fields the compiler was inlining the setter's clamp, generating ~18 bytes of redundant float compare + conditional move per field. Use friend access to assign directly to LightColorValues fields, bypassing the setter. Also apply the same optimization to normalize_color() where division by max_value guarantees results stay in [0,1]. --- esphome/components/light/light_color_values.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/esphome/components/light/light_color_values.h b/esphome/components/light/light_color_values.h index b26deff0c3..dc23263312 100644 --- a/esphome/components/light/light_color_values.h +++ b/esphome/components/light/light_color_values.h @@ -95,15 +95,18 @@ class LightColorValues { */ void normalize_color() { if (this->color_mode_ & ColorCapability::RGB) { - float max_value = fmaxf(this->get_red(), fmaxf(this->get_green(), this->get_blue())); + float max_value = fmaxf(this->red_, fmaxf(this->green_, this->blue_)); + // Assign directly to avoid redundant clamp in set_red/green/blue. + // Values are guaranteed in [0,1]: inputs are already clamped to [0,1], + // and dividing by max_value (the largest) keeps results in [0,1]. if (max_value == 0.0f) { - this->set_red(1.0f); - this->set_green(1.0f); - this->set_blue(1.0f); + this->red_ = 1.0f; + this->green_ = 1.0f; + this->blue_ = 1.0f; } else { - this->set_red(this->get_red() / max_value); - this->set_green(this->get_green() / max_value); - this->set_blue(this->get_blue() / max_value); + this->red_ /= max_value; + this->green_ /= max_value; + this->blue_ /= max_value; } } }