Skip to content

Instantly share code, notes, and snippets.

@JeremyKuhne
Created March 6, 2019 01:46
Show Gist options
  • Save JeremyKuhne/0c68e3dcefa2273b3d2817c43b812ee8 to your computer and use it in GitHub Desktop.
Save JeremyKuhne/0c68e3dcefa2273b3d2817c43b812ee8 to your computer and use it in GitHub Desktop.
Wrapping int comparison
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Runtime.CompilerServices;
namespace CoreMkRef
{
class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<Unboxed>();
long startBytes = GC.GetAllocatedBytesForCurrentThread();
long consumed = GC.GetAllocatedBytesForCurrentThread() - startBytes;
}
}
public class Unboxed
{
[Benchmark]
public void TestIt()
{
int i = 42;
var iref = __makeref(i);
i += Double(iref);
}
[Benchmark]
public void TestItVariant()
{
int i = 42;
i += DoubleVariant(i);
}
[MethodImpl(MethodImplOptions.NoInlining)]
static int Double(TypedReference typed)
{
if (__reftype(typed) == typeof(int))
return __refvalue(typed, int);
return 0;
}
[MethodImpl(MethodImplOptions.NoInlining)]
static int DoubleVariant(Variant variant)
{
if (variant.Type == VariantType.Int32)
return (int)variant;
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment