Skip to content

Refactor logString method for efficiency#88

Merged
techniccontroller merged 1 commit into
techniccontroller:mainfrom
deepstahl77:patch-2
Jul 11, 2026
Merged

Refactor logString method for efficiency#88
techniccontroller merged 1 commit into
techniccontroller:mainfrom
deepstahl77:patch-2

Conversation

@deepstahl77

Copy link
Copy Markdown
Contributor

Refactor logString to use snprintf for better performance and avoid heap fragmentation. The problem: logString() takes the String by copy (not const String&), AND internally builds yet another new string on top of that. So every single log call causes multiple heap allocations/deallocations instead of just one.
(Internal concatenation → snprintf into existing fixed buffer)

The fatal part: this logger gets triggered automatically every 30 seconds – on every NTP update:

Refactor logString to use snprintf for better performance and avoid heap fragmentation.

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 refactors UDPLogger::logString to reduce heap allocations by avoiding String concatenation and instead formatting into a fixed-size buffer (intended to reduce heap fragmentation on frequent logging, e.g., during periodic NTP updates).

Changes:

  • Changed logString to take const String& instead of taking String by value.
  • Replaced String concatenation + toCharArray with snprintf into _packetBuffer and reused that buffer for UDP output.
  • Adjusted Serial logging to print the formatted buffer (now also subject to truncation).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread udplogger.cpp
Comment thread udplogger.cpp
techniccontroller added a commit that referenced this pull request Jul 11, 2026
Integrate PR #88: Refactor logString method for efficiency
@techniccontroller techniccontroller merged commit f1f6db3 into techniccontroller:main Jul 11, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants