8000
Skip to content

[logger] Use LogString for UART selection strings (saves 28 bytes RAM on ESP8266) - #10615

Merged
kbx81 6870 merged 9 commits into
devfrom
logger_esp8266_progmem
Sep 5, 2025
Merged

[logger] Use LogString for UART selection strings (saves 28 bytes RAM on ESP8266)#10615
kbx81 merged 9 commits into
devfrom
logger_esp8266_progmem

Conversation

@bdraco
@bdraco bdraco commented Sep 5, 2025
Copy link
Copy Markdown
Member

What does this implement/fix?

This PR optimizes RAM usage on ESP8266 by converting the logger component's UART selection strings to use ESPHome's LogString pattern, which automatically stores strings in PROGMEM (flash memory) on ESP8266.

The logger component uses strings to identify UART selections ("UART0", "UART1", "UART0_SWAP"). These strings were previously stored in RAM, consuming valuable memory on the ESP8266 platform which only has 80KB of RAM total. By using ESPHome's standard LOG_STR() macro pattern, these strings are automatically placed in PROGMEM on ESP8266, freeing up RAM for application use.

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)
  • 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

Example entry for config.yaml:

# Example configuration - no changes needed
logger:
  hardware_uart: UART0  # or UART1, UART0_SWAP
  level: DEBUG

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:

Summary

Converts logger UART selection strings to use ESPHome's LogString pattern for automatic PROGMEM storage on ESP8266, saving 28 bytes of RAM.

Key Changes

  • Changed get_uart_selection_() to return const LogString* instead of const char*
  • Replaced array lookups with switch statements using LOG_STR() macro
  • Updated all platform implementations for consistency
  • Callers now use LOG_STR_ARG() for proper string extraction

Memory Impact

ESP8266:

  • RAM: -28 bytes (33,924 β†’ 33,896)
  • Flash: +8 bytes (414,393 β†’ 414,401)

ESP32:

  • RAM: No change (17,912 bytes)
  • Flash: +24 bytes (562,298 β†’ 562,322)

The trade-off of 8 bytes of flash for 28 bytes of RAM is excellent on ESP8266 where RAM is precious.

Benefits

  • Uses ESPHome's established patterns - no new dependencies
  • Type-safe with LogString* return type
  • Consistent implementation across all platforms
  • More maintainable than array indexing
  • Automatic PROGMEM handling on ESP8266

Testing

Verified on ESP8266 and ESP32:

  • Correct UART selection display
  • UART0_SWAP mode functionality
  • RAM reduction confirmed
  • No behavioral changes

Copilot AI review requested due to automatic review settings September 5, 2025 15:18
@bdraco
bdraco requested a review from a team as a code owner September 5, 2025 15:18
@github-actions
github-actions Bot commented Sep 5, 2025
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#10615
    components: [logger]
    refresh: 1h

(Added by the PR bot)

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 RAM usage on ESP8266 by moving the logger component's UART selection strings from RAM to PROGMEM (flash memory), saving 36 bytes of valuable RAM.

  • Moves three UART selection strings ("UART0", "UART1", "UART0_SWAP") to PROGMEM
  • Updates the string array and accessor function to read from flash memory
  • Maintains identical functionality while reducing RAM footprint

Comment thread esphome/components/logger/logger_esp8266.cpp Outdated
bdraco and others added 2 commits September 5, 2025 10:19
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@codecov-commenter
codecov-commenter commented Sep 5, 2025
Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 60.43%. Comparing base (86c2af4) to head (f2bde66).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev   #10615   +/-   ##
=======================================
  Coverage   60.43%   60.43%           
=======================================
  Files          51       51           
  Lines       10482    10482           
  Branches     1400     1400           
=======================================
  Hits         6335     6335           
  Misses       3789     3789           
  Partials      358      358           

β˜” 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.

@bdraco
bdraco marked this pull request as draft September 5, 2025 15:37
@bdraco
bdraco marked this pull request as ready for review September 5, 2025 15:45
@bdraco
bdraco marked this pull request as draft September 5, 2025 16:09
@esphome esphome Bot removed the small-pr PR < 30 lines label Sep 5, 2025
@bdraco bdraco changed the title [logger] ESP8266: Store UART selection strings in PROGMEM (saves 36 bytes RAM) [logger] Use LogString for UART selection strings (saves 28 bytes RAM on ESP8266) Sep 5, 2025
@bdraco
bdraco marked this pull request as ready for review September 5, 2025 18:33
@bdraco
bdraco marked this pull request as draft September 5, 2025 18:33
@bdraco
bdraco marked this pull request as ready for review September 5, 2025 18:48
@kbx81 kbx81 left a comment
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice, thanks! πŸ™πŸ»

@kbx81
kbx81 merged commit 1340665 into dev Sep 5, 2025
29 checks passed
@kbx81
kbx81 deleted the logger_esp8266_progmem branch September 5, 2025 22:47
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 7, 2025
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