AnnulusGames/BurstLinq

It's slow on IL2CPP build. isn't it?

mrsions opened this issue · 3 comments

[Mono Build] is better than Non BurstCompile

Editor
Win-Mono
Win-Mono-Dev

but [IL2cpp Build] is not fast than BurstCompile

Win-Il2cpp
Win-Il2cpp-Dev

TestCode

[BurstCompile(OptimizeFor = OptimizeFor.Performance)]
public unsafe static class BurstLinq
{
    public static int Sum(List<int> source)
    {
        var span = SpanHelper.AsSpan(source);
        fixed (int* ptr = span)
        {
            SumCore(ptr, source.Count, out var result);
            return result;
        }

    }

    [BurstCompile(FloatMode = FloatMode.Fast)]
    internal static void SumCore(int* ptr, [AssumeRange(1, int.MaxValue)] int length, out int result)
    {
        var sum = default(int);
        for (var i = 0; i < length; i++) sum += ptr[i];
        result = sum;
    }
}

public unsafe static class NoBurstLinq
{
    public static int Sum(List<int> source)
    {
        var span = SpanHelper.AsSpan(source);
        fixed (int* ptr = span)
        {
            SumCore(ptr, source.Count, out var result);
            return result;
        }

    }

    internal static void SumCore(int* ptr, [AssumeRange(1, int.MaxValue)] int length, out int result)
    {
        var sum = default(int);
        for (var i = 0; i < length; i++) sum += ptr[i];
        result = sum;
    }
}

public class TestBehaviour : MonoBehaviour
{
    private List<int> ints;
	private int = 1000000;

    void Start()
    {
        ints = Enumerable.Range(0,1000).ToList();
    }

    void Update()
    {
        var localValue = 0;

        Profiler.BeginSample("Linq");
        for (var i = 0; i < length; i++)
        {
            localValue += ints.Sum();
        }
        Profiler.EndSample();
        Profiler.BeginSample("NoBurstLinq");
        for (var i = 0; i < length; i++)
        {
            localValue += NoBurstLinq.Sum(ints);
        }
        Profiler.EndSample();
        Profiler.BeginSample("BurstLinq");
        for (var i = 0; i < length; i++)
        {
            localValue += BurstLinq.Sum(ints);
        }
        Profiler.EndSample();
    }
}

Interesting result. I will need to look into this in more detail.

apkd commented

These numbers look weird. Given that localValue isn't stored anywhere, perhaps IL2CPP optimized the computation away completely?