mirror of
https://github.com/esphome/esphome.git
synced 2026-05-26 03:07:04 +08:00
[light] Force-inline ColorModeBitPolicy::to_bit
Although to_bit is constexpr and header-defined, the compiler leaves it as an out-of-line function when called with a runtime ColorMode, costing a call0/call8 for what is a short linear search over a 10-entry compile-time table. Marking it ESPHOME_ALWAYS_INLINE lets the compiler fold the lookup into each caller (validate_, color_mode_to_human, get_suitable_color_modes_mask_, light_json_schema). The to_bit symbol disappears from the link output entirely. Flash delta is small (+16 B ESP8266, +44 B ESP32-IDF in isolated light build); the intent is the runtime win — CodSpeed will quantify it.
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include "esphome/core/finite_set_mask.h"
|
#include "esphome/core/finite_set_mask.h"
|
||||||
|
#include "esphome/core/helpers.h" // ESPHOME_ALWAYS_INLINE
|
||||||
|
|
||||||
namespace esphome::light {
|
namespace esphome::light {
|
||||||
|
|
||||||
@@ -128,7 +129,7 @@ struct ColorModeBitPolicy {
|
|||||||
using mask_t = uint16_t; // 10 bits requires uint16_t
|
using mask_t = uint16_t; // 10 bits requires uint16_t
|
||||||
static constexpr int MAX_BITS = sizeof(COLOR_MODE_LOOKUP) / sizeof(COLOR_MODE_LOOKUP[0]);
|
static constexpr int MAX_BITS = sizeof(COLOR_MODE_LOOKUP) / sizeof(COLOR_MODE_LOOKUP[0]);
|
||||||
|
|
||||||
static constexpr unsigned to_bit(ColorMode mode) {
|
static constexpr unsigned ESPHOME_ALWAYS_INLINE to_bit(ColorMode mode) {
|
||||||
// Linear search through lookup table
|
// Linear search through lookup table
|
||||||
// Compiler optimizes this to efficient code since array is constexpr
|
// Compiler optimizes this to efficient code since array is constexpr
|
||||||
for (int i = 0; i < MAX_BITS; ++i) {
|
for (int i = 0; i < MAX_BITS; ++i) {
|
||||||
|
|||||||
Reference in New Issue
Block a user