Skip to content

Commit 0a39be6

Browse files
Cory R. Kingclaude
andcommitted
fix(progmem): use __builtin_memcpy to eliminate function call overhead
Replace fl::memcpy with __builtin_memcpy in FL_PGM_READ_* macros. The fl::memcpy wrapper lives in a separate translation unit, preventing the compiler from inlining it. This adds unnecessary function call overhead on platforms using null_progmem.h (ESP32, ARM, etc.) where these macros are called 3x per LED in the gamma correction hot path. __builtin_memcpy is a compiler intrinsic that GCC/Clang will inline to a single load instruction for small constant sizes (1, 2, 4 bytes), maintaining strict aliasing correctness while eliminating the overhead. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent ac59596 commit 0a39be6

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/platforms/null_progmem.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#pragma once
33

44
#include "fl/int.h" // for FastLED integer types
5-
#include "ftl/cstring.h" // for memcopy (FastLED equivalent)
65

76
// Guard against PROGMEM redefinition on platforms that have their own definition
87
#if !defined(PROGMEM) && !defined(__IMXRT1062__) && !defined(__MK20DX128__) && !defined(__MK20DX256__) && !defined(__MK66FX1M0__) && !defined(__MK64FX512__) && !defined(__MKL26Z64__)
@@ -19,12 +18,14 @@
1918
#endif
2019
#endif
2120

22-
// Private helper function for safe memory copying to avoid strict aliasing issues
23-
// This uses fl::memcpy (FastLED equivalent) instead of standard memcpy
21+
// Private helper function for safe memory copying to avoid strict aliasing issues.
22+
// Uses __builtin_memcpy which is a compiler intrinsic that GCC/Clang will inline
23+
// to a single load instruction for small constant sizes, eliminating function call
24+
// overhead while maintaining strict aliasing correctness.
2425
template<typename T>
2526
static inline T fl_progmem_safe_read(const void* addr) {
2627
T result;
27-
fl::memcpy(&result, addr, sizeof(T));
28+
__builtin_memcpy(&result, addr, sizeof(T));
2829
return result;
2930
}
3031

0 commit comments

Comments
 (0)