Skip to content

Question about class-based effects #5294

Open

Description

@JoaDick

Is your feature request related to a problem? Please describe.
The current design of WLED with is working perfectly fine for effects that can calculate everything from scratch whenever their mode function gets called. However, many effects require data that persists between function calls. This can be achieved through the SEGENV macro, but doing so is quite cumbersome.

Describe the solution you'd like
The obvious idea to make life easier for those effects would be a class-based design. Then the persistent data could be implemented as member variables, and a function like MyEffect::show() would be called by WLED.

So, my question is: What are the reasons why such a class-based design hasn't found its way into WLED yet? Are there concerns because of code size or performance? Or compatibility issues with the existing effects? Or it just wasn't seen as important?

While implementing my first effect for WLED, I've studied a lot of its codebase - and some ideas came into my mind how such effect-classes could be integrated. It would be really interesting to know what your thoughts are on that topic, and what else you'd expect from such an upgraded design.

Activity

  1. DedeHai commented on Jan 13, 2026

    @DedeHai
    Collaborator

    So, my question is: What are the reasons why such a class-based design hasn't found its way into WLED yet? Are there concerns because of code size or performance? Or compatibility issues with the existing effects? Or it just wasn't seen as important?

    As you are aware, there have been attempts in this direction by @TripleWhy. What exactly the implications are regarding RAM use and general performance of such an approach I do not know. It may well be feasible (and beneficial) but unless there is a draft to evaluate it's hard to say for me what possible issues may be.

  2. TripleWhy commented on Jan 14, 2026

    @TripleWhy
    Contributor

    I do have a working prototype (all effects converted to separate classes etc.), and from what I can tell, it's not slower. Faster, actually in most cases, with some optimizations this design enables.
    And cleaner. So much cleaner.
    It also allows for the RAM usage to be tightly controlled, so that you know exactly how much memory segments or effects can possibly allocate at most. Without this, some effects like to use much more than allowed, or allocate more space before old one is cleared etc. The tradeoff is that that RAM is assigned at compile time, even if you are never going to use any heavy effects.
    And of course class effects would allow for some interesting features.

    The prototype was kinda mostly finished at some point. Unfortunately, someone decided to develop some major, partially conflicting changes in a non-git-like way. So I had no way of synchronizing or identifying and looking at those changes early. To be fair I was made aware of that and decided to go ahead anyway, which is certainly my fault. (Because I did have the time at that point, and knew I wouldn't later.) When the other changes were finally merged, merging those into my prototype was quite time consuming and mentally taxing (because of the state the code base), and I gave up at some point. But I still plan to pick it up again eventually. We'll see how relevant this is by then :)

    EDIT: regarding the "faster" statement: That observation was made before those major changes were merged, and those did contain some effectively similar optimizations. So the situation might be different nowadays.

  3. DedeHai commented on Jan 14, 2026

    @DedeHai
    Collaborator

    It also allows for the RAM usage to be tightly controlled, so that you know exactly how much memory segments or effects can possibly allocate at most.

    that is interesting, how does it work? Currently my updated RAM management allows for "as much as you need" until the set limit runs out.

    The tradeoff is that that RAM is assigned at compile time

    that sounds like a bad thing. Why is any RAM assigned to non-existing FX classes? or am I misunderstanding your statement?

  4. JoaDick commented on Jan 14, 2026

    @JoaDick
    ContributorAuthor

    Ah yes, @TripleWhy , I think I remember your PR from last year. It was using factories for creating the effect instances (which was quite welcomed), but would require the effects to be implemented in a pull-based way (which was not so much appreciated)? IIRC it looked quite impressive - even if I didn't understand why you implemented the polymorphism manually instead of using virtual methods.

    But probably I should admit that there's more than curiosity behind my question. During the holidays I eventually had the time to implement a proof of concept which I've been thinking about for quite a while. It grew much faster than I expected, and could soon be ready for review. However, it would be odd to throw that at you all right out of nowhere.
    And I would really like to know if there are any serious constraints from WLED's developer side, which have to be regarded!

    My drivers for this PoC were the following:

    • Having fun implementing that 😃
    • I need a class for storing persistent effect data!!!
    • It shall integrate into WLED on top of Segment, without too much changes in existing code.
    • Migration of existing effects should be possible gradually, no "big switch".
    • Preferably no breaking changes.
    • A cleaner, simpler and better documented API for implemening new effects (by newbies) would be great.
    • Maybe that's also an enabler for future, more serious refactorings of the Segment class?

    I suspect that this will clash (once again) with @TripleWhy 's PoC. But maybe we could finally merge the best from both worlds. My PoC doesn't care for factories; it just abuses the mode functions - which could definitely be done better. And your idea with the sparse setups could be integrated via a specialized base class - just for those effect which can benefit from that, but not for all.

    So, please tell me more about your thoughts and requirements, and I'll do my best to prepare a draft PR soon.

  5. TripleWhy commented on Feb 11, 2026

    @TripleWhy
    Contributor

    It also allows for the RAM usage to be tightly controlled, so that you know exactly how much memory segments or effects can possibly allocate at most.

    that is interesting, how does it work? Currently my updated RAM management allows for "as much as you need" until the set limit runs out.

    Well a "set limit" is the same as "allocate at most" right? Except that one is enforced and the other is not. As for how it works: Effects are class objects, and objects can own other objects. So effects have real and type save variables they can work on, compared to just a bunch of bytes it kinda hopefully owns and uses correctly. Where was I going with this? A, right, allocators. If you use real objects, and stuff them in allocator-aware containers, they will use an allocator for memory management. I add a simple allocator that is used basically exclusively for effects. When it's full, it's full. Try again later.

    The tradeoff is that that RAM is assigned at compile time

    that sounds like a bad thing. Why is any RAM assigned to non-existing FX classes? or am I misunderstanding your statement?

    The memory pool is just exclusively reserved for effects in the beginning. It's assigned to effects on demand. There is a hard limit on the memory that effects can use, but also, that memory is always used, not just when effects make use of it. A fixed amout of memory. Non-instantiated effects don't take up space, instantiated ones take up space inside this reserved region, but never outside of it.

    but would require the effects to be implemented in a pull-based way (which was not so much appreciated)?

    Yes and no. Technically speaking, yes, but I added a way for effects to work push-based internally. Most effects ran perfectly fine with that, almost unchanged. Apart from the code being moved inside a class method, and replacing the SEGMENT macros, of course.

    implemented the polymorphism manually instead of using virtual methods

    Both work, one is just a bit more performant and memory saving (not sure about the last part). Going back to virtual methods would mostly be a matter of removing some layers.

    I need a class for storing persistent effect data!!!

    Well, no, you don't. But this is the kind of problem I was trying to tackle. (Apart from it being clumsy.) WLED's approach to effects is hard to grasp and difficult to get into. My version would have been a lot more intuitive, or so I like to believe.

    @JoaDick if you want to compare or talk about versions more informally, find me on the WLED discord :)

  6. JoaDick commented on Feb 15, 2026

    @JoaDick
    ContributorAuthor

    Thank you very much @TripleWhy for your detailed explanations! Now I'm really curious again and will have a closer look at how you implemented all that. In particular the allocator idea is interesting; it seems to be similar to some thoughts I also had a while ago. My idea was to somehow make use of C++17's polymorphic memory resources for that. But unfortunately, the ESP32 compiler doesn't yet support that IIRC. So that memory topic went far down the priority list... But on the other hand, implementing specific PMR stuff for WLED shouldn't bee too complicated; the underlying idea is really simple, yet so powerful.

    I'll definitely reach out to you when I find some time to continue tinkering with these classes and the memory management!

  7. DedeHai commented on Feb 15, 2026

    @DedeHai
    Collaborator

    The memory pool is just exclusively reserved for effects in the beginning. It's assigned to effects on demand. There is a hard limit on the memory that effects can use, but also, that memory is always used, not just when effects make use of it.

    that is a really bad and limiting way of managing precious RAM. IDF V5 has heap_caps_add_region_with_caps() that may be able to do it dynamically at runtime, I briefly looked into that when writing a defrag function (which works btw but is very limited in IDF V4) but I did not check how it can be put to good use in practice and what the limitations or implications are.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions