-
|
I ran benchmarks to test the devirtualization and using System;
using System.Linq;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
namespace Benchmarks;
[MemoryDiagnoser(displayGenColumns: false)]
[HideColumns("Job", "RatioSD", "Alloc Ratio")]
[SimpleJob(RuntimeMoniker.Net90)]
[SimpleJob(RuntimeMoniker.Net10_0)]
public class StaticReadonlyStackallocBenchmarks
{
private const bool ConstUseStackalloc = false;
private static readonly bool UseStackalloc = ConstUseStackalloc;
private static readonly List<int> Ints = Enumerable.Range(1, 1_000).ToList();
[Benchmark(Baseline = true)]
public int Baseline()
{
int sum = 0;
foreach (int i in (IEnumerable<int>)Ints) sum += i;
return sum;
}
[Benchmark]
public int Const()
{
Span<byte> buffer = ConstUseStackalloc ? stackalloc byte[1] : stackalloc byte[0];
int sum = 0;
foreach (int i in (IEnumerable<int>)Ints) sum += i;
return sum;
}
[Benchmark]
public int StaticReadonly()
{
Span<byte> buffer = UseStackalloc ? stackalloc byte[1] : stackalloc byte[0];
int sum = 0;
foreach (int i in (IEnumerable<int>)Ints) sum += i;
return sum;
}
[Benchmark]
public int StaticReadonlySumMethod()
{
Span<byte> buffer = UseStackalloc ? stackalloc byte[1] : stackalloc byte[0];
return Sum(Ints);
static int Sum(IEnumerable<int> ints)
{
int sum = 0;
foreach (int i in ints) sum += i;
return sum;
}
}
}I understand that the compiler removes the whole first line of the
I am thankful for all answers and everything new that I am able to learn 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
Thanks for reporting this issue, it seems to be running into an edge case - stackalloc inside a method with loops, jit conservatively immediately self-promotes itself to the final tier (FullOpts): It is something we want to eventually fix, for now, we can workaround it - JIT shouldn't give up on small constant sized stackallocs (<=32b) as we promote those to locals. |
Beta Was this translation helpful? Give feedback.
Thanks for reporting this issue, it seems to be running into an edge case - stackalloc inside a method with loops, jit conservatively immediately self-promotes itself to the final tier (FullOpts):
It is something we want to eventually fix, for now, we can workaround it - JIT shouldn't give up on small constant sized stackallocs (<=32b) as we promote those to locals.