Repository navigation
Description
Goal
Simplify Gamma8::getOrCreate(float gamma) to use a single-entry weak_ptr cache instead of fl::flat_map<GammaKey, fl::weak_ptr<const Gamma8>>. For the common case (one fixed gamma per sketch, e.g. 2.8 for addLeds<NEOPIXEL>), the simpler cache is equivalent. For users who switch gammas at runtime, they pay one extra Gamma8Impl reconstruction per switch — acceptable trade-off for the byte savings.
Parent: #2886 — twelfth algorithmic-bloat reduction.
Why this is a concrete win
src/fl/math/ease.cpp.hpp:425-442:
fl::shared_ptr<const Gamma8> Gamma8::getOrCreate(float gamma) {
GammaKey key(gamma);
static fl::flat_map<GammaKey, fl::weak_ptr<const Gamma8>> sCache;
auto it = sCache.find(key);
if (it != sCache.end()) {
fl::shared_ptr<const Gamma8> existing = it->second.lock();
if (existing) {
return existing;
}
}
fl::shared_ptr<const Gamma8> ptr = fl::make_shared<Gamma8Impl>(gamma);
sCache[key] = ptr;
return ptr;
}The fl::flat_map<GammaKey, fl::weak_ptr<const Gamma8>> instantiation pulls in:
- flat_map's template machinery (insert, find, key compare, growth)
- The vector backing it (sorted-vector implementation)
fl::pair<GammaKey, fl::weak_ptr<...>>element type
All for a cache that typically holds one entry in stock NEOPIXEL Blink. The current function symbol is 535 B; expected drop is to ~200-300 B (a single-pointer compare + ctor call).
Deliverable
src/fl/math/ease.cpp.hpp — simplified single-entry cache
fl::shared_ptr<const Gamma8> Gamma8::getOrCreate(float gamma) {
static GammaKey sCachedKey(0.0f);
static fl::weak_ptr<const Gamma8> sCachedPtr;
static bool sCacheValid = false;
GammaKey key(gamma);
if (sCacheValid && key == sCachedKey) {
if (auto existing = sCachedPtr.lock()) {
return existing;
}
}
fl::shared_ptr<const Gamma8> ptr = fl::make_shared<Gamma8Impl>(gamma);
sCachedKey = key;
sCachedPtr = ptr;
sCacheValid = true;
return ptr;
}Behavior matrix:
| Scenario | Before | After |
|---|---|---|
| First call with gamma X | Allocate Gamma8Impl(X), cache it. | Allocate Gamma8Impl(X), cache it. |
| Repeated call with same gamma X (weak_ptr still alive) | Return cached. | Return cached. |
| Call with new gamma Y (X still alive elsewhere) | Cache both X and Y; both reusable while alive. | Cache replaced with Y; future calls to X reallocate Gamma8Impl(X). |
| Call after all references dropped | Reallocate. | Reallocate. |
The performance regression is "reallocates the LUT for the previous gamma when switching" — 256 integer operations + ~512 B alloc. Rare scenario; negligible cost.
Acceptance
Gamma8::getOrCreate(float)uses single-entry cache.bash lint --cpppasses.- Behavior preserved for the single-gamma case (which is every stock
addLeds<>call). - Measured saving: projected ~200-400 B on stock NEOPIXEL Blink. Baseline ratchets in same PR if positive.
Out of scope
- A multi-entry small-cache (e.g. 2-4 entries). The flat_map already handles multi-entry; the single-entry is an explicit simplification for the common case.
- Removing the cache entirely. The cache prevents per-call LUT recomputation in the common path — keep it.
- Changing the
Gamma8::getOrCreate(float)signature.
Related
- Meta: ESP32-S3 Blink bloat reduction (file:line concrete plan) — supersedes #2885 #2886 — parent meta + audit cycle.
- perf(gamma): use fl::s16x16::pow for Gamma8 LUT + applyGamma_video; drop __ieee754_pow #2911 — gamma LUT generator migration to
fl::s16x16::pow(the immediate context — same function chain). - perf(esp32): gate emit* cold helpers on FASTLED_LOG_RUNTIME_ENABLED (~800 B) #2918 / fix(esp32): correct FASTLED_ASSERT release no-op + ratchet baseline 344769 -> 340832 #2925 — pattern of collapsing template-heavy machinery in cold-or-rare paths.
🤖 Generated with Claude Code
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
perf(gamma): simplify Gamma8::getOrCreate to single-entry weak_ptr ca…
chore(bloat): ratchet baseline 340832 -> 339962 (post-#2929 gamma cac…