Skip to content

Executable Code Duplication due to non-type template params #1041

Closed

Description

@oschonrock

The non-type template parameters are a neat, compile-time way to configure the various classes. However, they result in massive code duplication when more then one LED strip is used.

Sample sketch...

TestFastLED.ino

#include <FastLED.h>
CRGB leds1[10];
CRGB leds2[10];

void setup() {
  FastLED.addLeds<NEOPIXEL, 5>(leds2, 10);
  FastLED.addLeds<NEOPIXEL, 6>(leds1, 10);
}

now compile and examine the .elf with objdump

arduino-cli compile -barduino:avr:uno TestFastLED
~/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino5/avr/bin/objdump -dCj.text TestFastLED.arduino.avr.uno.elf 

produces:

...

00000188 <ClocklessController<(unsigned char)5, 4, 10, 6, (EOrder)66, 0, false, 10>::showPixels(PixelController<(EOrder)66, 1, 4294967295ul>&)>:
 188:	4f 92       	push	r4
 18a:	5f 92       	push	r5
 18c:	6f 92       	push	r6
 18e:	7f 92       	push	r7
 190:	8f 92       	push	r8
 192:	af 92       	push	r10
 194:	bf 92       	push	r11
 196:	cf 92       	push	r12
 198:	df 92       	push	r13
 19a:	ef 92       	push	r14
 19c:	0f 93       	push	r16
 19e:	1f 93       	push	r17
 1a0:	cf 93       	push	r28
 1a2:	df 93       	push	r29
 1a4:	3c 01       	movw	r6, r24
 1a6:	eb 01       	movw	r28, r22
 1a8:	0e 94 62 00 	call	0xc4	; 0xc4 <micros>
 1ac:	f3 01       	movw	r30, r6
 1ae:	27 85       	ldd	r18, Z+15	; 0x0f
 1b0:	30 89       	ldd	r19, Z+16	; 0x10

....

00000632 <ClocklessController<(unsigned char)6, 4, 10, 6, (EOrder)66, 0, false, 10>::showPixels(PixelController<(EOrder)66, 1, 4294967295ul>&)>:
 632:	4f 92       	push	r4
 634:	5f 92       	push	r5
 636:	6f 92       	push	r6
 638:	7f 92       	push	r7
 63a:	8f 92       	push	r8
 63c:	af 92       	push	r10
 63e:	bf 92       	push	r11
 640:	cf 92       	push	r12
 642:	df 92       	push	r13
 644:	ef 92       	push	r14
 646:	0f 93       	push	r16
 648:	1f 93       	push	r17
 64a:	cf 93       	push	r28
 64c:	df 93       	push	r29
 64e:	3c 01       	movw	r6, r24
 650:	eb 01       	movw	r28, r22
 652:	0e 94 62 00 	call	0xc4	; 0xc4 <micros>
 656:	f3 01       	movw	r30, r6
 658:	27 85       	ldd	r18, Z+15	; 0x0f
 65a:	30 89       	ldd	r19, Z+16	; 0x10

...

This is just something that C++ does, because it has to, when you have class template parameters like that. It duplicates all the code for every method for each unique set of template params. Given each LED strip will definitely be on a separate pin, that will be for every LED strip.

I don't have an obvious syntactical/language solution other than to move those template params, which will ineivitably vary for each strip (eg PIN), back to normal constructor/begin() runtime params and sacrifice a byte of RAM in order to save thousands of bytes of PROGMEM.

I wanted to migrate from Adafruit_Neopixel library (which also has issues) to FastLED, but stopped immediately when I noticed the much larger PROGMEM usage. My application has just 2 strips, it would be proportionately worse with more...

Update

One possible solution is to have "config structs" which inherit from a base struct/class and take all those template params, those are "saved" into static const constants in those structs and the calling code constructs an instance of the "config struct" (using its template params) and passes it into the existing FastLED class hierarchy.

The "config structs" have no methods and no members, so "duplicating them" with template params has zero impact. We can essentially "pass around the config at compile time" without create duplicate code or chewing up RAM at runtime. -- This is related to the common C++ technique of "tag dispatching". -- this would obviously be a major architectural refactor.

Update2

The above idea with static config struct doesn't work. C++ can't do that, and if you think about it, that makes sense. You can't get code to behave 2 slightly different ways without having 2 versions of it, ie compile time specialisation of a template with multiple versions of code in PROGMEM. Alternatively you can write code, which uses a memory location to change its behaviour, ie config in RAM, in class member variables. You need one or the other.

So perhaps the answer is, "back to Plan A":

  1. Move those params which MUST vary by strip instance (eg the data_pin number) into a RAM member variable and pass them to constructor as an ordinary construction param. This way we pay the price of one or two bytes of RAM in order to save thousands of bytes of duplicate code in PROGMEM. This already happens with addLeds(... nLedsOrOffset..). I am simply suggesting that we review which params are likely to trigger "multiple instances" of PROGMEM code and "data_pin" is probably one of those.

  2. Leave those params as template params which are unlikely to vary among strips within a particular "hardware installation", eg chipset and rgb_order are "probably" going to be the same for the "several strips" attached to a single MCU. This way they don't chew up RAM nor PROGMEM because there is only one version of the code anyway.

Activity

  1. zackees commented on Apr 16, 2026

    @zackees
    Member

    Closing this issue — it was opened over 4 years ago with no activity. If this is still relevant with a current FastLED version, please reopen with a minimal reproduction sketch and version details. Thank you!

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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions