[api] Optimize protobuf decode loop for better performance and maintainability - #10277
Conversation
|
To use the changes from this PR as an external component, add the following to your ESPHome configuration YAML file: external_components:
- source: github://pr#10277
components: [api]
refresh: 1h(Added by the PR bot) |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #10277 +/- ##
=======================================
Coverage 60.45% 60.45%
=======================================
Files 51 51
Lines 10417 10417
Branches 1388 1388
=======================================
Hits 6298 6298
Misses 3760 3760
Partials 359 359 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes the protobuf decode loop in ProtoDecodableMessage::decode() by replacing error flag handling with early returns and switching from index-based to pointer-based buffer traversal. The changes improve performance by 1.2-7.5% for typical API message sizes while reducing flash usage by 224 bytes.
Key changes:
- Replaced error flag + break pattern with immediate returns for cleaner control flow
- Switched from array indexing to pointer arithmetic for better compiler optimization
- Consolidated boundary checks and improved error messages with offset reporting
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
What does this implement/fix?
Optimizes the protobuf decode loop in
ProtoDecodableMessage::decode()by eliminating redundant error handling and improving control flow. The changes result in cleaner, more maintainable code with measurable performance improvements for typical API message sizes.Key improvements:
Performance impact:
Types of changes
Related issue or feature (if applicable):
Pull request in esphome-docs with documentation (if applicable):
Test Environment
Example entry for
config.yaml:Checklist:
tests/folder).If user exposed functionality or configuration variables are added/changed: