Skip to content

Simplify Gamma8::getOrCreate to single-entry weak_ptr cache (~200-400 B) (#2886) #2928

Closed

Description

@zackees

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

  1. Gamma8::getOrCreate(float) uses single-entry cache.
  2. bash lint --cpp passes.
  3. Behavior preserved for the single-gamma case (which is every stock addLeds<> call).
  4. 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

🤖 Generated with Claude Code

Activity

  1. added a commit that references this issue on Jun 7, 2026
    73d27f5
  2. moved this from Triage to Done in FastLED Trackeron Jun 7, 2026
  3. added a commit that references this issue on Jun 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions