osd: replace common tfp_sprintf patterns with direct formatting helpers#11405
Conversation
Replace 81 tfp_sprintf calls in osd.c with lightweight helpers that skip format-string parsing overhead: - osdFormatIntUnit: replaces "%Nd" and "%Nd%c" patterns (47 calls) - osdWriteChar/osdWriteChar2: replaces "%c" and "%c%c" (33 calls) - osdFormatTime_MMSS: replaces "%02d:%02d" pattern (2 calls) Removes one dead null-terminator write and collapses a redundant RSSI_DBM branch that became identical after conversion.
ⓘ You are approaching your monthly quota for Qodo. Upgrade your plan Review Summary by QodoReplace common tfp_sprintf patterns with direct formatting helpers in OSD
WalkthroughsDescription• Replace 81 tfp_sprintf calls with lightweight formatting helpers • Add five new static helper functions for common OSD formatting patterns • Reduce code size and CPU overhead by skipping format-string parsing • Simplify redundant RSSI_DBM display logic with unified formatting Diagramflowchart LR
A["tfp_sprintf calls<br/>81 instances"] -->|"osdFormatIntUnit<br/>47 calls"| B["Integer with unit<br/>formatting"]
A -->|"osdWriteChar<br/>21 calls"| C["Single char<br/>formatting"]
A -->|"osdWriteChar2<br/>12 calls"| D["Two char<br/>formatting"]
A -->|"osdFormatTime_MMSS<br/>2 calls"| E["MM:SS time<br/>formatting"]
B --> F["Reduced code size<br/>and CPU overhead"]
C --> F
D --> F
E --> F
File Changes1. src/main/io/osd.c
|
Code Review by Qodo
1. osdFormatTime_MMSS uses modulo wrap
|
|
Test firmware build ready — commit Download firmware for PR #11405 234 targets built. Find your board's
|
Resolves blank OSD regression caused by missing multiFunctionWarning_t definition from maintenance-9.x. Conflicts in osd.c resolved by: - Keeping HEAD's osdWriteChar2/osdWriteChar optimization helpers - Taking maintenance-9.x's brace style for if/else blocks - Taking maintenance-9.x's strlen(buff) bug fix for RSSI stats itoa call - Preserving HEAD's displayWrite call accidentally dropped by maintenance-9.x
On HD displays with valid efficiency data and 4-digit (DJI compat) mode, the efficiency string can reach 15 chars + null = 16 bytes, overflowing outBuff[15] by one byte. Adding an explicit null-termination guard at the end of all build paths prevents this and matches the inav3 reference.
Enables AddressSanitizer via -DASAN=ON. Use alongside -DDEBUG=ON for readable stack traces. Follows the existing DEBUG flag pattern.
afd3fa2 to
b72ae26
Compare
Summary
Replace 81
tfp_sprintfcalls inosd.cwith lightweight formatting helpers that skip format-string parsing,va_listmarshaling, and per-character function-pointer dispatch. For example, we had 21 calls of the form:Which just writes a single known character into the buffer.
It is about 97% faster to do:
Changes
Five new
statichelper functions added near the top ofosd.c:osdFormatIntUnit(buff, width, value, unit)— replaces"%Nd"and"%Nd%c"patterns (47 calls). Uses existingi2a()fromtypeconversion.c.osdWriteChar(buff, c)— replacestfp_sprintf(buff, "%c", c)with direct assignment (21 calls)osdWriteChar2(buff, c1, c2)— replacestfp_sprintf(buff, "%c%c", c1, c2)(12 calls)osdFormatTime_MMSS(buff, m, s)— replaces"%02d:%02d"with direct digit writes (2 calls)i2a_len(num, bf)— thin wrapper aroundi2a()that returns string lengthCalls that use the return value of
tfp_sprintf, zero-padded formats (%02d,%07d), or dynamic format strings were intentionally left unchanged.Testing
tfp_sprintfacross a range of values including negative numbers, zero, boundary values, and values wider than the field width