electro-smith / DaisySP

A Powerful DSP Library in C++
https://www.electro-smith.com/daisy
Other
836 stars 131 forks source link

Improve Oscillator performance by avoiding fmodf #188

Closed ndonald2 closed 6 months ago

ndonald2 commented 11 months ago

Summary

Adds a fastmod1f method to the dsp utils header which is significantly faster than fmodf(x, 1.0f), and use this in Oscillator for nontrivial performance gains.

Details

This optimization really adds up when processing multiple oscillators in more complex applications and when using block-based DSP instead of sample-by-sample, but even with DaisySP defaults the savings are obvious.

I neglected to add this optimization to BlOsc because:

  1. The measured performance gains are negligible (the cost of the rest of the DSP outweighs the optimization gain)
  2. The sawtooth fmodf invocation has a variable second argument (not just 1.0f)

Performance Metrics

Measured using CpuLoadMeter and the basic seed oscillator example from DaisyExamples at Sample Rate 48kHz and Block Size 4, with the POLYBLEP_TRI waveform.

Before:

Load min:  2.196%    Max:  3.372%    Average:  2.602%

After:

Load min:  1.500%    Max:  2.316%    Average:  1.607%
stephenhensley commented 11 months ago

Nice! This is a great idea, and a nice little optimization. That type of performance improvement definitely adds up.

Thanks for the PR!

We'll review this and do some light testing, but I think it looks good as is. I'll let you know if there's anything to do before we merge it.

beserge commented 6 months ago

Looks great! Thank you for the thorough PR. 🎸