[api] Avoid lambda IIFE and per-byte APIBuffer growth in proxy benchmarks

The InfraredRFReceiveEvent encode benchmark used a C++17 lambda IIFE
(`[]{...}()`) to seed a function-static vector, and the
InfraredRFTransmitRawTimingsRequest decode benchmark grew its APIBuffer
one byte at a time (~210 grow_() calls), each allocating a fresh
exact-fit buffer and memcpy'ing the prior contents. Both patterns are
fine under direct execution but appear to hit a CodSpeed/valgrind
edge case during the simulated benchmark run.

Switch to a plain heap-init pattern for the vector and build the wire
bytes into a stack array first, then resize+memcpy into the APIBuffer
once.
This commit is contained in:
J. Nick Koston
2026-04-29 21:25:46 -05:00
parent 4c027e87ba
commit f841de0664
2 changed files with 28 additions and 19 deletions
@@ -456,17 +456,17 @@ BENCHMARK(Encode_SerialProxyDataReceived);
#if defined(USE_IR_RF) || defined(USE_RADIO_FREQUENCY)
// Mark/space pairs simulating a typical RC-5 / NEC capture (100 timings).
static const std::vector<int32_t> &get_ir_timings_100() {
static const std::vector<int32_t> timings = [] {
std::vector<int32_t> v;
v.reserve(100);
// Mark/space pairs simulating a typical RC-5 / NEC capture.
static std::vector<int32_t> *timings = nullptr;
if (timings == nullptr) {
timings = new std::vector<int32_t>();
timings->reserve(100);
for (int i = 0; i < 100; i++) {
v.push_back((i % 2 == 0) ? 560 : -560);
timings->push_back((i % 2 == 0) ? 560 : -560);
}
return v;
}();
return timings;
}
return *timings;
}
static InfraredRFReceiveEvent make_infrared_rf_receive_event() {