Skip to content

teensy41 Apa102 binary 148KB / 88KB budget — async logger + HTTP/task pulled into SPI sketch #2656

Closed

Description

@zackees

Symptom

check_teensy41_size workflow has been red on master for many consecutive runs (continuously since at least 2026-05-30 15:25). Latest: run 26703433838 at 7d834f2.

The Blink example check passes:

teensy41 size 57340 is within the limit of 120000

But the Apa102 example check fails ~1.69x over budget:

teensy41 size 148476 is greater than max size 88000
##[error]Compiled size exceeds maximum allowed size

Root cause (preliminary)

A symbol dump of the teensy41 Apa102 firmware ELF shows the binary is dragging in code that has nothing to do with APA102 SPI LED output. Sampled symbols (full nm dump in the run log):

Async logger / task infrastructure:

  • fl::detail::AsyncLoggerServiceTask (ctor/dtor/InlinedLambda destroy)
  • fl::AsyncLogQueue<128, 4096> (full ringbuffer impl — push/pop/load/commit/atomicIncDropped/...)
  • fl::detail::async_log_flush_timer_isr

HTTP / task::Error machinery:

  • fl::net::http::StreamHandle::catch_(fl::function<void(const fl::task::Error&)>)
  • fl::function<void(const fl::task::Error&)>::NonConstMemberCallable vtable
  • fl::function<void(const fl::task::Error&)>::ConstMemberCallable vtable
  • fl::variant<shared_ptr<...>, FreeFunctionCallable, InlinedLambda, NonConstMember, ConstMember> move/destroy tables

None of this should be present in a sketch that does:

FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, BGR>(leds, NUM_LEDS);

The Blink example shows the same teensy41 chipset cleanly fits in 57.3 KB; the additional ~91 KB in Apa102 is being pulled in by something between SPI chipset selection and what gets linked.

Likely culprits

The async system was unified under fl/stl/async/ in early March (per recent CLAUDE.md memory). That move + the HTTP/Asio addition is a strong candidate for being eagerly included via a header chain ending at FastLED.h or one of the chipset headers. The split between "user includes FastLED.h" → "ends up with HTTP" is the regression vector.

Not raising the budget

Per user directive on the equivalent esp32dev_binary_size situation (#2608): do not raise budgets to mask bloat regressions. The right fix is to find the include chain that pulls async/HTTP into a SPI-only sketch and break it (e.g., move impl into .cpp.hpp behind a feature-flag include guard, or push the logger init to a separate header users opt into).

Investigation entry points

  • Reproduce locally: bash compile teensy41 --examples Apa102 then arm-none-eabi-nm -S --size-sort .build/pio/teensy41/.pio/build/teensy41/firmware.elf | head -50 to see the largest contributors.
  • Trace which header pulls in fl::detail::AsyncLoggerServiceTask. Likely entry point is whatever FastLED.h ultimately includes for logging — that header probably unconditionally instantiates the async logger task instead of stubbing on bare-metal Arduino targets.
  • The fl::function<...task::Error...> infrastructure is a strong hint that task.h is being pulled into FastLED.h's transitive closure.

Filed but no PR

Filing this as an issue rather than a PR because the right fix is a multi-file include refactor and warrants explicit owner buy-in (could legitimately break public API expectations on what #include <FastLED.h> brings in). Loop iteration halts per "halt if can't fix" — but the bloat signature is now documented for whoever picks it up.

Related

Activity

  1. zackees commented on May 31, 2026

    @zackees
    MemberAuthor

    Same root cause hits teensy30 workflow too.

    Run 26703433897 at 7d834f2. Every example fails with the same signature — RAM region overflow (teensy30 = Kinetis MK20DX128, 16 KB RAM total):

    ld: region `RAM' overflowed by 22376 bytes   (AnalogOutput)
    ld: region `RAM' overflowed by 22372 bytes   (BeatDetection)
    ld: region `RAM' overflowed by 22372 bytes   (ParallelSPI)
    ld: region `RAM' overflowed by 22372 bytes   (PinMode)
    ld: region `RAM' overflowed by 22396 bytes   (RGBCalibrate)
    ld: region `RAM' overflowed by 22384 bytes   (Test)
    

    Overflowing by ~22 KB more than the chip's 16 KB RAM is comically over budget — the async logger's AsyncLogQueue<128, 4096> (4 KB arena + 128*32 B descriptors = ~8 KB) alone plus the fl::function<void(const fl::task::Error&)> callable storage explains the bulk of it.

    This is the same regression as the teensy41 Apa102 case in the issue body — just hits harder because teensy30's RAM is 16 MB smaller than teensy41's.

    Until the include-graph decoupling lands, teensy30 and check_teensy41_size will stay red. Filing only as a comment (not a new issue) since the root cause is identical.

  2. zackees commented on Jun 5, 2026

    @zackees
    MemberAuthor

    Investigation update — async logger / HTTP not the cause; real culprit is FASTLED_SPI_USES_CHANNEL_API routing APA102 through fl::channels

    Took another pass at the failing check_teensy41_size build (run 27006203252, sha 787de450). Working strictly from the linker map (firmware.map) — both the Discarded input sections dump and the kept sections dump — the original symbol hypothesis turns out to be wrong, and the real bloat lives somewhere else entirely.

    What's actually in the final Apa102 ELF

    -ffunction-sections -fdata-sections -Wl,--gc-sections are all active for the teensy toolchain — every .cpp.hpp function lands in its own .text._Z… section, and --gc-sections strips anything unreferenced. The async logger and HTTP symbols listed in the issue body show up in the Discarded input sections block of the map (fl.log+.cpp.o and fl.stl+.cpp.o), so they are present in libFastLED.a but not present in the linked firmware.

    Per-archive-member byte totals in the kept region of the Apa102 map (vs. Blink for the same revision):

    Object Blink Apa102 Delta
    libFastLED.a(fl.channels+.cpp.o) 0 55,158 +55,158
    libFastLED.a(platforms+.cpp.o) 6,570 31,446 +24,876
    libFastLED.a(fl.stl+.cpp.o) 5,332 7,688 +2,356
    libFastLED.a(fl.system+.cpp.o) 1,186 2,574 +1,388
    libFastLED.a(fl.gfx+.cpp.o) 3,475 3,823 +348
    libFastLED.a(fl.log+.cpp.o) 234 234 0
    libFastLED.a(fl.task+.cpp.o) 1,980 1,980 0
    FastLED .text delta total ~+84 KB

    fl.log+.cpp.o contributes exactly the same 234 bytes (fl::fastled_file_offset) in both builds — AsyncLoggerServiceTask, AsyncLogQueue<…>, async_log_service, async_log_flush_timer_isr, and the fl::function<void(const fl::task::Error&)> vtables are all in Discarded input sections for both Blink and Apa102. Same for fl::net::http::ChunkedReader, HttpRequestParser, HttpResponseParser — all discarded.

    So the original investigation entry point — "trace AsyncLoggerServiceTask" — is a dead end. Those symbols visible via nm on libFastLED.a are pre-link archive contents, not linked code.

    The actual include chain

    The link map's "Archive member included to satisfy reference by file (symbol)" block for the Apa102 link gives the real chain (one critical row):

    .pio/build/teensy41/libd58/libFastLED.a(fl.channels+.cpp.o)
                                  .pio/build/teensy41/src/main.cpp.o
                                  (fl::ChannelConfig::ChannelConfig(
                                       fl::SpiChipsetConfig const&,
                                       fl::span<fl::CRGB, 4294967295u>,
                                       fl::EOrder,
                                       fl::ChannelOptions const&))
    

    main.cpp.o (the user's Apa102 sketch) directly instantiates fl::ChannelConfig::ChannelConfig(SpiChipsetConfig const&, …). That's the entry point. It is reached from this header trail:

    examples/Apa102/Apa102.ino
      └── FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS)
           └── src/FastLED.h   line 95-101:
                 #if !defined(FASTLED_SPI_USES_CHANNEL_API)
                 #if FASTLED_HAS_CHANNELS && \
                     (defined(FL_IS_ESP32) || defined(FL_IS_TEENSY_4X))
                 #define FASTLED_SPI_USES_CHANNEL_API 1
                 #endif
                 #endif
           └── src/FastLED.h   line 1006-1024:
                 #elif FASTLED_SPI_USES_CHANNEL_API
                 template<ESPIChipsets CHIPSET, …>
                 ::CLEDController &addLeds(CRGB *data, …) {
                     …
                     fl::SpiEncoder encoder =
                         fl::SpiEncoder::spiEncoderForChipset(…);
                     fl::SpiChipsetConfig spiCfg(DATA_PIN, CLOCK_PIN, encoder);
                     fl::ChannelConfig config(spiCfg, …, RGB_ORDER);   ← HERE
                     …
                     sChannel = add(config);
                 }
    

    The addLeds<APA102, ...> template, on Teensy 4.x, does not call into APA102Controller/legacy SPIOutput — it constructs a fl::ChannelConfig and hands it to fl::ChannelManager::add(...). That single call drags the entire fl/channels machinery into the link (manager, dispatcher, the variant-of-PixelControllers, SpiChannelEngineAdapter, PixelIterator, function_list, quicksort, all SPI HW backends, etc.).

    Introduced by commit badef0c5d ("update spi with channels api where applicable", 2026-04-02) and gated on Teensy 4.x at src/FastLED.h:96.

    What's chewing the 55 KB of fl.channels+.cpp.o

    Top per-symbol byte counts in the kept region (filtered to fl.channels):

     5,652  fl::Channel::showPixels<PixelController<RGB,1,…>>
     1,696  fl::ChannelManager::addDriver(int, shared_ptr<IChannelDriver>)
     1,620  fl::detail::quicksort_impl<…ChannelManager::EngineEntry…>
     1,612  fl::(anonymous)::writeUCS7604(…)         ← UCS7604 path always pulled in
     1,568  fl::SpiChannelEngineAdapter::transmitBatch(…)
     1,180  fl::(anonymous)::ReorderingPixelIteratorAny::ctor
     1,176  fl::SpiChannelEngineAdapter::create(…)
       852  fl::PixelIterator::writeWS2812<vector_psram<u8>>   ← WS2812 always pulled
       832  fl::SpiChannelEngineAdapter::show()
       708  SPIClass::end() (x3 copies, different teensy bus instantiations)
       680  fl::PixelIteratorAny::init(…)
       640  fl::Channel::Channel(variant<ClocklessChipset, SpiChipsetConfig>, …)
       616  fl::Singleton<ChannelEvents,0>::instance()
       600  fl::function_list<…>::invoke (x3 for IChannel / IChannel+string / IChannel+ChannelData)
       564  fl::ChannelManager::selectDriverForChannel(…)
       428  fl::encodeHD108_HD(…)    ← all HD encoders, even though only APA102 used
       392  fl::encodeSK9822_HD(…)
       392  fl::encodeAPA102_HD(…)
       …
    

    platforms+.cpp.o adds ~25 KB more for the teensy spi_hw_2/4/manager backends and the channel-driver init list.

    Proposed fix (the surgical break)

    The right fix is the same kind of opt-in escape hatch that already exists for WASM (FASTLED_FAKE_SPI_FORWARDS_TO_FAKE_CLOCKLESS, FastLED.h:71-77) — but for the channel-API SPI path on Teensy 4.x:

    1. Keep the legacy SPI controller path (FASTLED_SPI_USES_CHANNEL_API == 0) available on Teensy 4.x and make the channel-API path opt-in via a user-defined macro:

      // src/FastLED.h ~ line 95
      #if !defined(FASTLED_SPI_USES_CHANNEL_API)
      #if FASTLED_HAS_CHANNELS && defined(FL_IS_ESP32)
      #define FASTLED_SPI_USES_CHANNEL_API 1
      #else
      #define FASTLED_SPI_USES_CHANNEL_API 0
      #endif
      #endif

      i.e. drop FL_IS_TEENSY_4X from the default-on list. ESP32 keeps the new API; Teensy 4.x reverts to the legacy clocked SPI controller (which was working until April 2). User code can re-opt-in with -DFASTLED_SPI_USES_CHANNEL_API=1.

    2. Stretch goal — make the channel-API SPI path lazier even when enabled: the Channel::showPixels template is instantiated for one PixelController<RGB,1,…> per addLeds call, but the variant in fl::channels carries every EOrder × every chipset, which is why quicksort_impl, function_list::invoke, the variant::visit_fn family, the three HD encoders, the WS2812 encoder, and the UCS7604 writer all link unconditionally even from a single APA102+RGB instantiation. That's the structural cost — splitting fl/channels/_build.cpp.hpp so SPI-only sketches don't link the clockless/UCS7604/HD-encoder fallbacks is a larger refactor (the right Phase-2 follow-up).

    API impact

    Path (1) is API-preserving for the user: FastLED.addLeds<APA102, …>(…) keeps the same signature. Internally the constructed controller type changes from fl::Channel/SpiChannelEngineAdapter back to APA102Controller / SPIOutput<…>. The runtime-discoverable fl::ChannelManager::forEach etc. won't see APA102 instances on Teensy 4.x — that's a behavior shift for anyone relying on the new channel registry on Teensy. Anyone who wants that on Teensy can #define FASTLED_SPI_USES_CHANNEL_API 1 before #include <FastLED.h>.

    Estimated size benefit

    Pulling fl.channels+.cpp.o out of the Apa102 link drops ~55 KB; the platforms+.cpp.o delta of ~25 KB is part dependent (SpiHw2/4 backends + channel-driver init list). Most of the latter is still needed for clockless drivers but the SPI-specific portion (~10–15 KB) should also fall away. Realistic landing zone: ~60–70 KB removed, putting Apa102 around 80–90 KB — within (or right at) the 88 KB budget.

    Why I'm filing as comment, not PR

    The default-flip on FASTLED_SPI_USES_CHANNEL_API is a public-surface decision (it changes which controller object addLeds<APA102,…> returns on Teensy 4.x, and which registry the LED strip is discoverable through). Wants @zackees ack before shipping. Happy to draft the one-line PR (drop FL_IS_TEENSY_4X from the default-on conditional + a news/ entry + add -DFASTLED_SPI_USES_CHANNEL_API=1 documentation) the moment that's blessed.

    The async-logger / HTTP angle in the issue body should be edited / struck — those symbols genuinely aren't in the linked firmware.

  3. zackees commented on Jul 16, 2026

    @zackees
    MemberAuthor

    Local fbuild bloat investigation (2026-07-16)

    The open APA102 regression is confirmed, but the original async logger/HTTP hypothesis is not: those symbols are present in discarded archive sections, not retained in the final ELF.

    Blink baseline with the current Teensy Audio LDF shim

    bash compile teensy41 --examples Blink followed by fbuild symbols showed:

    • final fbuild size: flash=100352 B, ram=72544 B
    • selected framework sources: Audio 91, SdFat 46, SD 1, SerialFlash 2, DmxSimple 1, plus the normal LED/core libraries
    • retained Audio symbols include AudioInputI2S::dma, AudioInputI2S2::dma, AudioStream::memory_pool_available_mask, i2s_rx_buffer, and i2s2_rx_buffer

    A controlled build with the active <Audio.h> include removed returned Blink to ram=72352 B and selected zero Audio sources; SerialFlash also disappeared. Thus the Audio shim has a measured +192-byte RAM cost on Blink and intentionally makes the full Audio library visible to fbuild.

    APA102 versus Blink

    On the same Teensy 4.1 target:

    • Blink: flash=100352 B, ram=72352 B without the temporary Audio include
    • APA102: flash=157696 B, ram=86720 B
    • fbuild symbols map delta: approximately +70543 B symbol-level flash and +41504 B .bss.dma

    The new retained path is the Teensy 4.x Channel API: fl::Channel::showPixels, fl::SpiChannelEngineAdapter, fl::ChannelManager, fl::ChannelEngineFlexIO, fl::BitBangChannelDriver, fl::ObjectFLED, and related channel-driver machinery. FASTLED_SPI_USES_CHANNEL_API is still defaulted to 1 for FL_IS_TEENSY_4X in src/FastLED.h lines 98–101.

    Remaining dependency leaks

    The no-Audio A/B build still selected SdFat (46 sources), SD (1), and DmxSimple (1). These are independent of the Audio shim and should be investigated as fbuild LDF false positives or overly broad FastLED framework/header roots. SerialFlash selection is transitive from Audio’s play_serialflash_raw.h path.

    The desired invariant for a minimal Blink is that no Audio, SD/SdFat, SerialFlash, DmxSimple, or unrelated Channel API implementation is retained unless the sketch actually uses the corresponding feature.

    This investigation is coordinated with FastLED/fbuild#1088; the Audio result there is intentional dependency discovery, but the measured Blink overhead should be considered in any follow-up design.

  4. added
    deferred
    on Aug 20, 2026
  5. zackees commented on Aug 20, 2026

    @zackees
    MemberAuthor

    Deferred after current-master verification. Teensy 4.1 APA102 is still 125,952 bytes and passes only because #2804 raised the ceiling to 165,000; FASTLED_SPI_USES_CHANNEL_API=1 still pulls the large Channel graph. Resolving this requires a Channel API/tree-shaking redesign, before/after symbol proof, restoration of the 88,000-byte ceiling, and an explicit ChannelManager opt-in path. Resume with Teensy hardware/size ownership for that redesign.

  6. zackees commented on Oct 7, 2026

    @zackees
    MemberAuthor

    Re-measured on master 7ac9a83 with bash compile teensy41 --examples Apa102: 49,152 B flash, 28,448 B RAM. That is down from 148,476 B and well under the original 88,000 B ceiling; 3.10.3 measures 55,296 B on the same tooling.

    An nm scan of the ELF finds no AsyncLog*, fl::net::http, ifstream, posix_filebuf or strerror. The only fl::task symbol is fl::task::run, which the generated main's fl::delay(0) pulls in. The over-link is gone; #4710 and earlier work removed it.

    The max_size_apa102: 165000 band-aid in check_teensy41_size.yml can now be tightened back toward 88000 (see docs/SIZE_THRESHOLD_HISTORY.md). Tracked in #4728.

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

    deferred

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions