Repository navigation
refactor(dither): extract binary dithering into a tested, zero-cost, disableable module (#4672) - #4673
Conversation
#4672) Temporal binary dithering moves out of PixelController into a header-only module with two policies sharing one static interface: - fl::BinaryDither: phase/offset/init/reseed/clear/active/step/stepChannel/apply - fl::NoDither: init zeroes d/e, everything else is a no-op or identity - fl::Dither: selected by NO_DITHERING PixelController keeps its public d[3]/e[3] (the AVR, M0 and Teensy drivers read them by name) and its method names; each now forwards to fl::Dither. init and reseed share one phase() instead of two copies of the bit reversal, and the three open-coded (e[0]|e[1]|e[2]) checks become ditherActive(). Fixes uninitialised d/e under NO_DITHERING (all FL_IS_AVR_LIMITED parts): init_binary_dithering() was compiled empty while the default mode is still BINARY_DITHER, so the AVR clockless asm added stack garbage to every byte whenever dithering was re-enabled after the first show() or a controller was driven directly. NoDither::init now clears them. The API takes u8 (&)[3] rather than u8* or fl::span<T,3>: with pointers or spans GCC keeps three induction pointers in the init loop (+16..+22 B on uno); array references fold back to member offsets of `this`. AVR image_flash vs master (Blink/Apa102/ColorPalette): uno, leonardo, nano_every -12 B each; attiny85 -28/-118/-28 B. RAM unchanged. Tests: tests/fl/gfx/binary_dither.cpp covers the phase table, byte-for-byte equality with the pre-extraction algorithm over every scale x frame, reseed == init, step involution, black floor and saturation, cycle-average accuracy, NoDither, and PixelController binding.
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. 🧰 Additional context used📚 Code guidelines (1)No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (2)
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review. 📝 WalkthroughWalkthroughThe pull request extracts binary dithering into a shared header with enabled and disabled policies. PixelController delegates dithering operations to the selected policy, and three callers use its activity check. ChangesBinary dithering
Priority: ⬇️ Low Estimated code review effort: 3 (Moderate) | ~20 minutes Change: Refactor · Severity of issue fixed: Low Merge Risk: ⚪ Minimal · up to Enabled dithering behavior is preserved, while disabled dithering now starts from cleared state. No material merge-blocking behavior change was identified. Architecture SummaryArchitecture risk: 🔵 Low · up to The change affects 2 systems. Changed systems: Architecture concerns Review detailsSystems and components
Before / after behavior
🚥 Pre-merge checks | ✅ 3 | ❌ 1 | ❓ 1❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
Full details: Linked Issues checkExplanation For [
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
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. Comment |
…er.h (#4672) binary_dither.h included led_sysdefs.h to see NO_DITHERING, which reaches fl/system/arduino.h -> <Arduino.h>. pixel_controller.h did not include that before, so every unity TU reaching it parsed the full platform SDK -- more compile memory on the ESP32 unity files that CI already OOM-kills (#4675). Read NO_DITHERING the way the replaced code did (defined by FastLED.h -> led_sysdefs.h before pixel_controller.h), and include binary_dither.h last in pixel_controller.h. ATtiny still selects NoDither: attiny85 Blink/ ColorPalette sizes unchanged (3464/4822 B).
|
Pushed 989aeea: removed Correction: I first linked this include to the exit -9 kills on ESP32 unity TUs. Investigation under #4675 shows those kills are not OOM. Peak compile memory is about 2.2 GB on a 16 GB runner, and the kernel logged no OOM kill. The kills also happen on Sizes on the same base (current
|
Closes #4672.
What
Temporal binary dithering moves out of
PixelControllerinto a header-only module,src/fl/gfx/binary_dither.h, with two policies that share one static interface:fl::BinaryDither:phase,offset,init,reseed,clear,active,step,stepChannel,apply.fl::NoDither:initzeroesd/e; everything else is a no-op or the identity.fl::Dither: the one in use, selected byNO_DITHERING. One switch disables it, and the work is compiled out rather than run against zeros.PixelControllerkeeps its publicd[3]/e[3]and its method names, so the AVR/ATtiny clockless asm and the M0, LPC, nRF51 and Teensy drivers are untouched. Each method is now a one-line forward.initandreseedshare onephase()instead of two copies of the bit reversal. The three open-coded(e[0]|e[1]|e[2]) != 0checks infl/channelsbecomepixels.ditherActive().Bug fixed
Under
NO_DITHERING(everyFL_IS_AVR_LIMITEDpart),init_binary_dithering()compiled to an empty body while the default mode is stillBINARY_DITHER, sod/ewere never written. The AVR asm adds them to every byte it sends. This happens whenever dithering is turned back on after the firstshow()(FastLED.setDither(BINARY_DITHER)), or when a controller is driven directly.NoDither::initnow clears them, and it costs nothing (see the sizes below).Why
u8 (&)[3]and notfl::span<T, 3>I measured each option on uno/Blink (master 5170 B):
enable_ditheringu8*fl::span<T,3>fl::span<T,3>+ force-inline accessorsu8 (&)[3]With pointers or spans, GCC keeps three induction pointers in the
initloop, which costs two extra register saves and pointer bookkeeping. Array references fold back to fixed offsets fromthis, the same codegen as the old member code.AVR size (
bash bloat <board> --example <ex> --build,image_flashin bytes)RAM is unchanged in all 12 builds.
Tests
tests/fl/gfx/binary_dither.cpp(10 cases):reseed == init;stepis an involution, andstepChanneltouches only its own channel;active;NoDither;PixelControllerbinding.Also run:
bash test --cpp: 316/316 unit, 95/95 examples.bash test binary_dither --debug(ASAN/UBSAN): clean.bash lint: clean.Production path
Every
CPixelLEDController::show()builds aPixelController, which now callsfl::Dither. TheChannel/SlimSpiBridgeController/ColorManagedPixelSourcepaths go throughditherActive()andreseed_binary_dithering().Summary by CodeRabbit