Skip to content

refactor(fl/gfx): lazy-init rgb_2_rgbw user function via Singleton<T> - #2426

Merged
zackees merged 1 commit into
masterfrom
refactor/rgbw-singleton-2424
May 5, 2026
Merged

zackees merged 1 commit into
masterfrom
refactor/rgbw-singleton-2424

Conversation

@zackees

@zackees zackees commented May 5, 2026 •

Copy link
Copy Markdown
Member

Summary

Replaces the static-init form

rgb_2_rgbw_function g_user_function = rgb_2_rgbw_exact;

with a lazily-constructed fl::Singleton<Rgb2RgbwUserState> wrapper. The Singleton's instance is only constructed on first call to set_rgb_2_rgbw_function() or rgb_2_rgbw_user_function() — never at static init.

Refs #2424 (recommended source-level cleanup from cross-TU references analysis).

Empirical impact (be honest)

Measured on bash compile esp32s3 --examples Blink (default release, current master):

Metric Before After Delta
Binary size 830,272 830,304 +32 bytes
.flash.text 350,916 350,944 +28
.flash.rodata 385,900 385,900 0
rgb_2_rgbw_* family present in binary 5 functions 5 functions 0

This refactor does NOT shrink the binary. The 5 rgb_2_rgbw_* algorithm functions remain in the binary because pixel_controller.h:613 calls rgb_2_rgbw(rgbw_mode, ...) — a runtime switch over rgbw_mode that ODR-uses all 5 algorithm functions regardless of the static-init form. The lazy-init only addresses the static-init reference; the runtime-switch reference is unchanged.

The +32 bytes are: Singleton's init-guard variable + the new null-check fallback in rgb_2_rgbw_user_function.

Why merge anyway

Structural improvements that are independent of binary size:

  1. No static init order dependency on rgb_2_rgbw_exact. The previous form referenced it at static init time; the Singleton form doesn't reference any algorithm function until the first runtime call.
  2. Cleaner override pattern: Singleton<Rgb2RgbwUserState>::instance().fn = userFn is the same Singleton pattern already used elsewhere in fl/ (e.g. CFastLED::channels() uses Singleton<vector<ChannelPtr>>).
  3. Removes the special-case if (func == nullptr) reset semantics in set_rgb_2_rgbw_function — now nullptr simply means "use the lazy default at call time" with no asymmetry.

What this does NOT fix

The actual binary-size win for the rgbw family would require per-mode template dispatch for loadAndScaleRGBW so the compiler can statically eliminate the unused branches of the runtime switch. That's a larger refactor and out of scope here. Filed for follow-up consideration in #2424.

Test plan

  • bash lint --cpp clean
  • bash test --cpp 302/302 pass
  • bash compile esp32s3 --examples Blink succeeds (binary 830,304 bytes)
  • Symbol search: set_rgb_2_rgbw_function and rgb_2_rgbw_user_function still present and callable

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Improved internal state management for RGB to RGBW function handling. The public API remains unchanged, ensuring full backward compatibility. This refactoring enhances the reliability and stability of custom function handling in color conversion operations.

Replaces the previous static-init form
  rgb_2_rgbw_function g_user_function = rgb_2_rgbw_exact;
with a Singleton<Rgb2RgbwUserState> wrapper that is lazily constructed on
first call to set_rgb_2_rgbw_function() or rgb_2_rgbw_user_function().

Empirical impact (ESP32-S3 Blink, default release):
  binary 830272 -> 830304 bytes (+32 bytes)
  rgb_2_rgbw_* family stays in binary (still kept by the runtime switch
  in pixel_controller.h:613 over rgbw_mode, which references all 5
  algorithm functions; the Singleton change only addresses the static
  init reference)

Wins are structural rather than size-related:
  - no static init order dependency on rgb_2_rgbw_exact
  - cleaner override pattern (instance().fn = userFn)
  - matches the singleton pattern already used elsewhere in fl/

Refs #2424.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented May 5, 2026 •

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 302515a1-138b-40b8-954c-085b50e38d00

📥 Commits

Reviewing files that changed from the base of the PR and between cec6034 and 46077df.

📒 Files selected for processing (1)
  • src/fl/gfx/rgbw.cpp.hpp

📝 Walkthrough

Walkthrough

The PR refactors RGB-to-RGBW conversion function storage from a static global variable to a lazy-initialized singleton pattern. The setter now stores the function pointer directly without eager defaulting, and the getter defers fallback selection to call time.

Changes

RGB→RGBW Function State Management

Layer / File(s) Summary
Include & Type Definition
src/fl/gfx/rgbw.cpp.hpp (lines 10, 117–134)
Added singleton header and created Rgb2RgbwUserState struct with a nullable function pointer, backed by fl::Singleton.
Storage & Retrieval Logic
src/fl/gfx/rgbw.cpp.hpp (lines 135–149)
set_rgb_2_rgbw_function now stores the function pointer directly into singleton state without conversion. rgb_2_rgbw_user_function fetches from singleton and falls back to rgb_2_rgbw_exact only if the pointer is nullptr at call time.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

A rabbit hops through singleton dreams,
Where functions float on lazy beams.
No eager defaults, just fallback grace—
State finds its home in the right place! 🐰✨

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/rgbw-singleton-2424

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

1 participant