mirror of
https://github.com/esphome/esphome.git
synced 2026-02-08 16:51:52 +00:00
[e131] Remove unnecessary heap allocation from packet receive loop
The received data was already in stack buffer buf[1460], but was being copied into a std::vector just to satisfy the packet_() signature. E1.31 runs at up to 44 Hz per universe, so this caused ~44 alloc/free cycles per second per universe. Change packet_() to accept const uint8_t* + size_t and pass the stack buffer directly. This is an internal method so there is no API breakage.
This commit is contained in:
@@ -55,7 +55,6 @@ void E131Component::setup() {
|
||||
}
|
||||
|
||||
void E131Component::loop() {
|
||||
std::vector<uint8_t> payload;
|
||||
E131Packet packet;
|
||||
int universe = 0;
|
||||
uint8_t buf[1460];
|
||||
@@ -64,11 +63,9 @@ void E131Component::loop() {
|
||||
if (len == -1) {
|
||||
return;
|
||||
}
|
||||
payload.resize(len);
|
||||
memmove(&payload[0], buf, len);
|
||||
|
||||
if (!this->packet_(payload, universe, packet)) {
|
||||
ESP_LOGV(TAG, "Invalid packet received of size %zu.", payload.size());
|
||||
if (!this->packet_(buf, (size_t) len, universe, packet)) {
|
||||
ESP_LOGV(TAG, "Invalid packet received of size %zd.", len);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ class E131Component : public esphome::Component {
|
||||
void set_method(E131ListenMethod listen_method) { this->listen_method_ = listen_method; }
|
||||
|
||||
protected:
|
||||
bool packet_(const std::vector<uint8_t> &data, int &universe, E131Packet &packet);
|
||||
bool packet_(const uint8_t *data, size_t len, int &universe, E131Packet &packet);
|
||||
bool process_(int universe, const E131Packet &packet);
|
||||
bool join_igmp_groups_();
|
||||
void join_(int universe);
|
||||
|
||||
@@ -116,11 +116,11 @@ void E131Component::leave_(int universe) {
|
||||
ESP_LOGD(TAG, "Left %d universe for E1.31.", universe);
|
||||
}
|
||||
|
||||
bool E131Component::packet_(const std::vector<uint8_t> &data, int &universe, E131Packet &packet) {
|
||||
if (data.size() < E131_MIN_PACKET_SIZE)
|
||||
bool E131Component::packet_(const uint8_t *data, size_t len, int &universe, E131Packet &packet) {
|
||||
if (len < E131_MIN_PACKET_SIZE)
|
||||
return false;
|
||||
|
||||
auto *sbuff = reinterpret_cast<const E131RawPacket *>(&data[0]);
|
||||
auto *sbuff = reinterpret_cast<const E131RawPacket *>(data);
|
||||
|
||||
if (memcmp(sbuff->acn_id, ACN_ID, sizeof(sbuff->acn_id)) != 0)
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user