Compare commits

...

2 Commits

Author SHA1 Message Date
J. Nick Koston
0b02476e8a [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].
2026-02-10 21:02:24 -06:00
J. Nick Koston
e3fbbb2e99 [light] Eliminate redundant clamp in LightCall::validate_()
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. Saves ~204 bytes of flash on ESP32.
2026-02-10 20:45:05 -06:00
2 changed files with 25 additions and 19 deletions

View File

@@ -270,22 +270,23 @@ LightColorValues LightCall::validate_() {
if (this->has_state())
v.set_state(this->state_);
#define VALIDATE_AND_APPLY(field, setter, name_str, ...) \
// clamp_and_log_if_invalid already clamps in-place, so assign directly
// to avoid redundant clamp code from the setter being inlined.
#define VALIDATE_AND_APPLY(field, name_str, ...) \
if (this->has_##field()) { \
clamp_and_log_if_invalid(name, this->field##_, LOG_STR(name_str), ##__VA_ARGS__); \
v.setter(this->field##_); \
v.field##_ = this->field##_; \
}
VALIDATE_AND_APPLY(brightness, set_brightness, "Brightness")
VALIDATE_AND_APPLY(color_brightness, set_color_brightness, "Color brightness")
VALIDATE_AND_APPLY(red, set_red, "Red")
VALIDATE_AND_APPLY(green, set_green, "Green")
VALIDATE_AND_APPLY(blue, set_blue, "Blue")
VALIDATE_AND_APPLY(white, set_white, "White")
VALIDATE_AND_APPLY(cold_white, set_cold_white, "Cold white")
VALIDATE_AND_APPLY(warm_white, set_warm_white, "Warm white")
VALIDATE_AND_APPLY(color_temperature, set_color_temperature, "Color temperature", traits.get_min_mireds(),
traits.get_max_mireds())
VALIDATE_AND_APPLY(brightness, "Brightness")
VALIDATE_AND_APPLY(color_brightness, "Color brightness")
VALIDATE_AND_APPLY(red, "Red")
VALIDATE_AND_APPLY(green, "Green")
VALIDATE_AND_APPLY(blue, "Blue")
VALIDATE_AND_APPLY(white, "White")
VALIDATE_AND_APPLY(cold_white, "Cold white")
VALIDATE_AND_APPLY(warm_white, "Warm white")
VALIDATE_AND_APPLY(color_temperature, "Color temperature", traits.get_min_mireds(), traits.get_max_mireds())
#undef VALIDATE_AND_APPLY

View File

@@ -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;
}
}
}
@@ -276,6 +279,8 @@ class LightColorValues {
/// Set the warm white property of these light color values. In range 0.0 to 1.0.
void set_warm_white(float warm_white) { this->warm_white_ = clamp(warm_white, 0.0f, 1.0f); }
friend class LightCall;
protected:
float state_; ///< ON / OFF, float for transition
float brightness_;