mirror of
https://github.com/esphome/esphome.git
synced 2026-09-17 10:08:40 +00:00
Merge remote-tracking branch 'origin/controller-registry-direct-dispatch' into integration
This commit is contained in:
@@ -324,6 +324,9 @@ bool IDFUARTComponent::peek_byte(uint8_t *data) {
|
||||
}
|
||||
|
||||
bool IDFUARTComponent::read_array(uint8_t *data, size_t len) {
|
||||
if (len == 0) {
|
||||
return false;
|
||||
}
|
||||
size_t length_to_read = len;
|
||||
int32_t read_len = 0;
|
||||
if (!this->check_read_timeout_(len))
|
||||
@@ -331,11 +334,10 @@ bool IDFUARTComponent::read_array(uint8_t *data, size_t len) {
|
||||
if (this->has_peek_) {
|
||||
length_to_read--;
|
||||
*data = this->peek_byte_;
|
||||
data++;
|
||||
this->has_peek_ = false;
|
||||
}
|
||||
if (length_to_read > 0)
|
||||
read_len = uart_read_bytes(this->uart_num_, data, length_to_read, 20 / portTICK_PERIOD_MS);
|
||||
read_len = uart_read_bytes(this->uart_num_, data + (len - length_to_read), length_to_read, 20 / portTICK_PERIOD_MS);
|
||||
#ifdef USE_UART_DEBUGGER
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
this->debug_callback_.call(UART_DIRECTION_RX, data[i]);
|
||||
|
||||
@@ -235,16 +235,14 @@ bool HostUartComponent::read_array(uint8_t *data, size_t len) {
|
||||
}
|
||||
if (!this->check_read_timeout_(len))
|
||||
return false;
|
||||
uint8_t *data_ptr = data;
|
||||
size_t length_to_read = len;
|
||||
if (this->has_peek_) {
|
||||
length_to_read--;
|
||||
*data_ptr = this->peek_byte_;
|
||||
data_ptr++;
|
||||
*data = this->peek_byte_;
|
||||
this->has_peek_ = false;
|
||||
}
|
||||
if (length_to_read > 0) {
|
||||
int sz = ::read(this->file_descriptor_, data_ptr, length_to_read);
|
||||
int sz = ::read(this->file_descriptor_, data + (len - length_to_read), length_to_read);
|
||||
if (sz == -1) {
|
||||
this->update_error_(strerror(errno));
|
||||
return false;
|
||||
|
||||
@@ -10,24 +10,24 @@ StaticVector<Controller *, CONTROLLER_REGISTRY_MAX> ControllerRegistry::controll
|
||||
|
||||
void ControllerRegistry::register_controller(Controller *controller) { controllers.push_back(controller); }
|
||||
|
||||
void ControllerRegistry::notify(void *obj, DispatchFunc dispatch) {
|
||||
for (auto *controller : controllers) {
|
||||
dispatch(controller, obj);
|
||||
}
|
||||
}
|
||||
|
||||
// Macro for standard registry notification dispatch - calls on_<entity_name>_update()
|
||||
// Each wrapper passes a small trampoline lambda that calls the correct virtual method.
|
||||
// Each notify method directly iterates controllers and calls the virtual method.
|
||||
// This avoids the overhead of a shared noinline dispatch loop with function pointer
|
||||
// indirection. The loop is tiny (~20 bytes per entity type) so the flash cost of
|
||||
// duplicating it is negligible compared to eliminating two levels of indirection
|
||||
// (noinline call + function pointer) from every state publish.
|
||||
// NOLINTBEGIN(bugprone-macro-parentheses)
|
||||
#define CONTROLLER_REGISTRY_NOTIFY(entity_type, entity_name) \
|
||||
void ControllerRegistry::notify_##entity_name##_update(entity_type *obj) { \
|
||||
notify(obj, [](Controller *c, void *o) { c->on_##entity_name##_update(static_cast<entity_type *>(o)); }); \
|
||||
for (auto *controller : controllers) { \
|
||||
controller->on_##entity_name##_update(obj); \
|
||||
} \
|
||||
}
|
||||
|
||||
// Macro for entities where controller method has no "_update" suffix (Event, Update)
|
||||
#define CONTROLLER_REGISTRY_NOTIFY_NO_UPDATE_SUFFIX(entity_type, entity_name) \
|
||||
void ControllerRegistry::notify_##entity_name(entity_type *obj) { \
|
||||
notify(obj, [](Controller *c, void *o) { c->on_##entity_name(static_cast<entity_type *>(o)); }); \
|
||||
for (auto *controller : controllers) { \
|
||||
controller->on_##entity_name(obj); \
|
||||
} \
|
||||
}
|
||||
// NOLINTEND(bugprone-macro-parentheses)
|
||||
|
||||
|
||||
@@ -146,8 +146,8 @@ class UpdateEntity;
|
||||
* entities call ControllerRegistry::notify_*_update() which iterates the small list
|
||||
* of registered controllers (typically 2: API and WebServer).
|
||||
*
|
||||
* Controllers read state directly from entities using existing accessors (obj->state, etc.)
|
||||
* rather than receiving it as callback parameters that were being ignored anyway.
|
||||
* Each notify method directly iterates controllers and calls the virtual method,
|
||||
* avoiding function pointer indirection for minimal dispatch overhead.
|
||||
*
|
||||
* Memory savings: 32 bytes per entity (2 controllers × 16 bytes std::function overhead)
|
||||
* Typical config (25 entities): ~780 bytes saved
|
||||
@@ -247,21 +247,6 @@ class ControllerRegistry {
|
||||
#endif
|
||||
|
||||
protected:
|
||||
/** Type-erased dispatch function pointer.
|
||||
*
|
||||
* Each notify method passes a small trampoline that calls the
|
||||
* correct virtual method on Controller. The shared notify() loop
|
||||
* iterates controllers once, calling the trampoline for each.
|
||||
*/
|
||||
using DispatchFunc = void (*)(Controller *, void *);
|
||||
|
||||
/** Shared dispatch loop - iterates controllers and calls dispatch for each.
|
||||
*
|
||||
* Marked noinline to ensure only one copy of the loop exists in flash,
|
||||
* rather than being duplicated into each notify_*_update wrapper.
|
||||
*/
|
||||
static void __attribute__((noinline)) notify(void *obj, DispatchFunc dispatch);
|
||||
|
||||
static StaticVector<Controller *, CONTROLLER_REGISTRY_MAX> controllers;
|
||||
};
|
||||
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@ freetype-py==2.5.1
|
||||
jinja2==3.1.6
|
||||
bleak==2.1.1
|
||||
smpclient==6.0.0
|
||||
requests==2.32.5
|
||||
requests==2.33.0
|
||||
|
||||
# esp-idf >= 5.0 requires this
|
||||
pyparsing >= 3.0
|
||||
|
||||
Reference in New Issue
Block a user