Skip to content

ArrayMath.MultiplyAdd on Unity Android #46

@danslobodan

Description

@danslobodan

Hi and thanks for making this great tool!

I'm using meltysynth to do some real-time rendering of audio in Unity, through the OnAudioFilterRead callback function.

When I used the IL2CPP to compile the project for android and ran it on multiple devices, the audio was stuttering horribly.

Upon running some profiling sessions I found that almost all of the CPU load was actually from the Garbage Collector, specifically in this function:

        public static void MultiplyAdd(float a, float[] x, float[] destination)
        {
            var vx = MemoryMarshal.Cast<float, Vector<float>>(x);
            var vd = MemoryMarshal.Cast<float, Vector<float>>(destination);
        
            var count = 0;
        
            for (var i = 0; i < vd.Length; i++)
            {
                vd[i] += a * vx[i];
                count += Vector<float>.Count;
            }
        
            for (var i = count; i < destination.Length; i++)
            {
                destination[i] += a * x[i];
            }
        }

Perhaps this is premature optimization, since changing the code to:

        public static void MultiplyAdd(float a, float[] x, float[] destination)
        {
            for (int i = 0; i < x.Length; i++)
            {
                destination[i] += a * x[i];
            }
        }

seems to have eliminated the load from this function entirely.

Perhaps this is not relevant to your intended use case, but I'd still like to let you know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions