Skip to content

Commit

Permalink
Added unsafe vs safe benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
wcabus committed Feb 7, 2024
1 parent 504b6fa commit 582a41e
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 4 additions & 1 deletion benchmarks/DoomSharp.Benchmarks.Runner/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using BenchmarkDotNet.Running;
using DoomSharp.Benchmarks.Runner.Workloads;

var test = new UnsafeVsSafe();

// BenchmarkRunner.Run<ArrayClearVsNewInstance>();
BenchmarkRunner.Run<StructsVsClasses>();
// BenchmarkRunner.Run<StructsVsClasses>();
BenchmarkRunner.Run<UnsafeVsSafe>();
101 changes: 101 additions & 0 deletions benchmarks/DoomSharp.Benchmarks.Runner/Workloads/UnsafeVsSafe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;

namespace DoomSharp.Benchmarks.Runner.Workloads;

[SimpleJob(RuntimeMoniker.Net60)]
[SimpleJob(RuntimeMoniker.NativeAot60)]
[SimpleJob(RuntimeMoniker.Net70)]
[SimpleJob(RuntimeMoniker.NativeAot70)]
[SimpleJob(RuntimeMoniker.Net80)]
[SimpleJob(RuntimeMoniker.NativeAot80)]
[MemoryDiagnoser]
public class UnsafeVsSafe
{
// Code sample: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/unsafe-code#pointer-types

[Benchmark]
public void Unsafe()
{
// Normal pointer to an object.
var a = new int[] { 10, 20, 30, 40, 50 };
// Must be in unsafe code to use interior pointers.
unsafe
{
// Must pin object on heap so that it doesn't move while using interior pointers.
fixed (int* p = &a[0])
{
// p is pinned as well as object, so create another pointer to show incrementing it.
int* p2 = p;
Console.WriteLine(*p2);
// Incrementing p2 bumps the pointer by four bytes due to its type ...
p2 += 1;
Console.WriteLine(*p2);
p2 += 1;
Console.WriteLine(*p2);
Console.WriteLine("--------");
Console.WriteLine(*p);
// Dereferencing p and incrementing changes the value of a[0] ...
*p += 1;
Console.WriteLine(*p);
*p += 1;
Console.WriteLine(*p);
}
}

Console.WriteLine("--------");
Console.WriteLine(a[0]);

/*
Output:
10
20
30
--------
10
11
12
--------
12
*/
}

[Benchmark]
public void Safe()
{
// Normal pointer to an object.
var a = new int[] { 10, 20, 30, 40, 50 };

var index = 0;
var index2 = index;
Console.WriteLine(a[index2]);
// Incrementing p2 bumps the pointer by four bytes due to its type ...
index2 += 1;
Console.WriteLine(a[index2]);
index2 += 1;
Console.WriteLine(a[index2]);
Console.WriteLine("--------");
Console.WriteLine(a[index]);
// Dereferencing p and incrementing changes the value of a[0] ...
a[index] += 1;
Console.WriteLine(a[index]);
a[index] += 1;
Console.WriteLine(a[index]);

Console.WriteLine("--------");
Console.WriteLine(a[0]);

/*
Output:
10
20
30
--------
10
11
12
--------
12
*/
}
}

0 comments on commit 582a41e

Please sign in to comment.