CPU issue with one of my modules

Here’s the issue

Source code

As the issue says, if you load the module in a new patch, no problem. If you quit Rack and reopen it, the CPU usage jumps up to ~30%. Is this a Rack issue or do I something wrong in my code?

1 Like

I don’t understand why, but changing your internal counter reset to:

sr = sr+1;
if (sr > srIncrement){
    sr = 0;

seems to fix it?

1 Like

That’s a shame. I was planning to use that module extensively during Psychedelic December.

Thank you. I guess modulo is apparently really expensive. I found this discussion on stackoverflow where they measured it to be over 6x as slow: https://stackoverflow.com/questions/15596318/is-it-better-to-avoid-using-the-mod-operator-when-possible

But it is weird that it seems ok at first and then blows up after reloading rack.

update: @hamptonio did you happen to try:

sr >= srIncrement

When I use >= it makes the CPU jump up again. So weird, any ideas?

Oh don’t worry. I’m going to fix it and it’ll go out when they do the next build.

1 Like

My ancient article on efficient plugins mentions this, sort of “It used to be that division was slow, and all the transcendental functions (sine, exp, etc) were very, very, slow.” I guess division slowness still matters.

Here’s the article: [https://github.com/squinkylabs/SquinkyVCV/blob/main/docs/efficient-plugins.md]

I don’t think the issue here is how slow a modulus operation is. There is no way that causes 50% cpu usage. I think somehow the the check is failing (I don’t know how) and the image processing code is getting run every process call.

Thanks @Squinky I’ve actually read through some of that a while ago but will definitely go through it again.

1 Like

Yes, I would tend to agree with you.

That seems to make sense. Although I can’t understand why when you first load the module in a patch it works fine (and even ~1%). Well at least it’s working though, thanks again for your help.