8000
Skip to content

[modbus] Use stack buffer for hex formatting in verbose logging - #12780

Merged
bdraco merged 1 commit into
devfrom
modbus
Jan 2, 2026
Merged

[modbus] Use stack buffer for hex formatting in verbose logging#12780
bdraco merged 1 commit into
devfrom
modbus

Conversation

@bdraco
@bdraco bdraco commented Jan 1, 2026
Copy link
Copy Markdown
Member

What does this implement/fix?

Replace heap-allocating format_hex_pretty(...).c_str() with stack-based format_hex_pretty_to() in the modbus component's verbose logging.

This eliminates std::string heap allocations in the LOGV paths for both send() and send_raw() functions. The buffers are wrapped in compile guards to avoid stack allocation when verbose logging is disabled.

Buffer size is 192 bytes (format_hex_pretty_size(64)) to cover typical Modbus frames while truncating unusually large ones.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Developer breaking change (an API change that could break external components)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

  • n/a

Pull request in esphome-docs with documentation (if applicable):

  • n/a

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx
  • nRF52840

Example entry for config.yaml:

# Standard modbus configuration - no changes needed
uart:
  tx_pin: GPIO17
  rx_pin: GPIO16
  baud_rate: 9600

modbus:

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

Copilot AI review requested due to automatic review settings January 1, 2026 02:35
@github-actions
github-actions Bot commented Jan 1, 2026
Copy link
Copy Markdown
Contributor

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#12780
    components: [modbus]
    refresh: 1h

(Added by the PR bot)

@codecov-commenter
codecov-commenter commented Jan 1, 2026
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.34%. Comparing base (3c9ed12) to head (528b374).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev   #12780   +/-   ##
=======================================
  Coverage   73.34%   73.34%           
=======================================
  Files          53       53           
  Lines       11284    11284           
  Branches     1531     1531           
=======================================
  Hits         8276     8276           
  Misses       2608     2608           
  Partials      400      400           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes verbose logging in the modbus component by replacing heap-allocated string formatting with stack-based buffers. The change eliminates std::string heap allocations in format_hex_pretty() calls by using the more efficient format_hex_pretty_to() function that writes directly to a stack buffer. The 192-byte buffers are conditionally compiled to avoid stack allocation when verbose logging is disabled.

  • Introduces MODBUS_MAX_LOG_BYTES constant (64 bytes) to limit logged frame size
  • Replaces format_hex_pretty(...).c_str() with stack-based format_hex_pretty_to() in both send() and send_raw() functions
  • Adds compile guards to avoid stack allocation when verbose logging is disabled

@github-actions
github-actions Bot commented Jan 1, 2026
Copy link
Copy Markdown
Contributor

Memory Impact Analysis

Components: modbus
Platform: esp8266-ard

Metric Target Branch This PR Change
RAM 29,044 bytes 29,044 bytes ➡️ +0 bytes (0.00%)
Flash 279,055 bytes 279,055 bytes ➡️ +0 bytes (0.00%)
📊 Component Memory Breakdown
Component Target Flash PR Flash Change
[esphome]modbus 1,723 bytes 1,729 bytes 📈 🔸 +6 bytes (+0.35%)
🔍 Symbol-Level Changes (click to expand)

Changed Symbols

Symbol Target Size PR Size Change
esphome::modbus::Modbus::dump_config() 101 bytes 111 bytes 📈 +10 bytes (+9.90%)
esphome::modbus::Modbus::parse_modbus_byte_(unsigned char) 792 bytes 788 bytes 📉 -4 bytes (-0.51%)

Note: This analysis measures static RAM and Flash usage only (compile-time allocation).
Dynamic memory (heap) cannot be measured automatically.
⚠️ You must test this PR on a real device to measure free heap and ensure no runtime memory issues.

This analysis runs automatically when components change. Memory usage is measured from a representative test configuration.

@bdraco bdraco left a comment
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@bdraco
bdraco merged commit b518873 into dev Jan 2, 2026
42 checks passed
@bdraco
bdraco deleted the modbus branch January 2, 2026 06:10
@github-actions github-actions Bot locked and limited conversation to collaborators Jan 4, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

0