mirror of
https://github.com/esphome/esphome.git
synced 2026-10-01 17:00:21 +00:00
[api] Keep the decode loop register resident and inline the varint fast path
CodSpeed showed the single virtual costing 7 to 18 percent on the decode benchmarks. The x86-64 disassembly pointed at the call, not the switch: passing the field number and wire type alongside the tag plus a 16 byte union payload kept five values live across the call, so the compiler spilled this, the end pointer and half of the payload to the stack and reloaded them for every field. decode_field() now takes only the tag, the payload pointer (already the loop cursor) and one scalar that holds the varint or fixed32 value or the payload length. The generated override wraps them in a ProtoFieldValue that never exists in memory. On the host the switch key is the field number derived with one shift and the guard compares the whole tag against the constant the case declares, which is the same two instructions the old per wire type dispatch cost. The loop also handles single byte varints inline instead of going through the parse result struct, which drops the materialized consumed count and its add on every tag and small value.
This commit is contained in:
@@ -226,7 +226,9 @@ def test_decode_cases_carry_field_number_and_wire_type(
|
||||
assert len(cases) == 1, cases
|
||||
lines = cases[0].splitlines()
|
||||
assert lines[0] == f"case PROTO_DECODE_CASE({number}, {wire_type}):", cases[0]
|
||||
assert lines[1].strip() == f"PROTO_DECODE_GUARD(wire_type, {wire_type});", cases[0]
|
||||
assert lines[1].strip() == f"PROTO_DECODE_GUARD(tag, {number}, {wire_type});", (
|
||||
cases[0]
|
||||
)
|
||||
assert accessor in cases[0], cases[0]
|
||||
|
||||
|
||||
@@ -237,15 +239,16 @@ def test_message_gets_a_single_decode_field_override() -> None:
|
||||
desc.field.add(name="count", number=2, type=UINT32_T)
|
||||
desc.field.add(name="level", number=3, type=FLOAT)
|
||||
header, cpp, _ = build_message_type(desc, {}, {"Mixed": SOURCE_CLIENT})
|
||||
decl = "bool decode_field(uint32_t tag, uint32_t field_id, uint32_t wire_type, ProtoFieldValue value) override;"
|
||||
decl = "bool decode_field(uint32_t tag, const uint8_t *data, proto_varint_value_t scalar) override;"
|
||||
assert header.count(decl) == 1
|
||||
assert "decode_varint" not in header and "decode_length" not in header
|
||||
assert (
|
||||
cpp.count(
|
||||
"bool Mixed::decode_field(uint32_t tag, uint32_t field_id, uint32_t wire_type, ProtoFieldValue value) {"
|
||||
"bool Mixed::decode_field(uint32_t tag, const uint8_t *data, proto_varint_value_t scalar) {"
|
||||
)
|
||||
== 1
|
||||
)
|
||||
assert "switch (PROTO_DECODE_KEY(tag, field_id)) {" in cpp
|
||||
assert "switch (PROTO_DECODE_KEY(tag)) {" in cpp
|
||||
assert "const ProtoFieldValue value(data, scalar);" in cpp
|
||||
for number, wire_type in ((1, 2), (2, 0), (3, 5)):
|
||||
assert f"case PROTO_DECODE_CASE({number}, {wire_type}):" in cpp, cpp
|
||||
|
||||
Reference in New Issue
Block a user