Lowering CPU cycles needed by plugin

Feb 24, 2009 at 1:01 PM
I noticed that I could lower CPU usage considerably by skipping a few tests and some exception handling built into the VstAudioBuffer class.
Instead of writing


VstAudioBuffer outputBuffer;
for (int i = 0; i < sampleCount; ++i)
{
  outputBuffer[i] = someValue;
}


I could lower the CPU usage considerably by writing

unsafe
{
  VstAudioBuffer outputBuffer;
  float *rawOutputBuffer = ((IDirectBufferAccess32)outputBuffer).Buffer;
  for (int i = 0; i < sampleCount; ++i)
  {
    rawOutputBuffer[i] = someValue;
  }
}


Any comments on this approach?
Coordinator
Feb 25, 2009 at 5:03 AM
Edited Feb 25, 2009 at 5:07 AM
Great!

Thats was the whole idea of implementing direct buffer access into the audio buffers (32/64). VST.NET provides a safe way to do it (with al the checks) and compatible with "all" managed languages, but you can do it yourself using the unmanaged buffer pointer -but then all responsibility is on your shoulders too (buffer overrun etc). Note that not all .NET languages support working with unsafe code (like VB.NET).

The unmanaged pointer you receieve from the Buffer property is the exact same one the Interop receives from the Host. The Process(Replacing) methods are the only methods that are not marshalled into managed types and back (other than wrapping the buffer pointer in a VstAudioBuffer instance). This was done to create minimal overhead when processing audio. The unmanaged buffer access is a continuation of that train-of-thought.