Skip to content

Commit 40fa176

Browse files
authored
Add Invoke Benchmarks (#478)
1 parent b906dd7 commit 40fa176

File tree

136 files changed

+1259
-394
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+1259
-394
lines changed

Silk.NET.sln

Lines changed: 305 additions & 291 deletions
Large diffs are not rendered by default.

src/Core/Silk.NET.Core/Loader/LibraryLoader.cs

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System;
1111
using System.Diagnostics;
1212
using System.IO;
13+
using System.Linq;
1314

1415
namespace Silk.NET.Core.Loader
1516
{
@@ -25,20 +26,12 @@ public abstract class LibraryLoader
2526
/// <returns>The operating system handle for the shared library.</returns>
2627
public nint LoadNativeLibrary(string name)
2728
{
28-
var success = TryLoadNativeLibrary(name, out var result);
29-
30-
if (!success)
31-
{
32-
ThrowLibNotFound(name);
33-
return default;
34-
}
35-
36-
return result;
29+
return LoadNativeLibrary(name, PathResolver.Default);
3730
}
3831

39-
private static void ThrowLibNotFound(string name)
32+
private static void ThrowLibNotFound(string name, PathResolver resolver)
4033
{
41-
throw new FileNotFoundException("Could not find or load the native library: " + name);
34+
throw new FileNotFoundException($"Could not find or load the native library: {name} Attempted: {string.Join(", ", resolver.EnumeratePossibleLibraryLoadTargets(name).Select(x => "\"" + x + "\""))}");
4235
}
4336

4437
/// <summary>
@@ -49,9 +42,7 @@ private static void ThrowLibNotFound(string name)
4942
/// <returns>The operating system handle for the shared library.</returns>
5043
public bool TryLoadNativeLibrary(string name, out nint result)
5144
{
52-
var success = TryLoadNativeLibrary(new[] {name}, PathResolver.Default, out result);
53-
54-
return success;
45+
return TryLoadNativeLibrary(new[] {name}, PathResolver.Default, out result);
5546
}
5647

5748
/// <summary>
@@ -63,21 +54,13 @@ public bool TryLoadNativeLibrary(string name, out nint result)
6354
/// <returns>The operating system handle for the shared library.</returns>
6455
public nint LoadNativeLibrary(string[] names)
6556
{
66-
var success = TryLoadNativeLibrary(names, out var result);
67-
68-
if (!success)
69-
{
70-
ThrowLibNotFoundAny(names);
71-
return default;
72-
}
73-
74-
return result;
57+
return LoadNativeLibrary(names, PathResolver.Default);
7558
}
7659

77-
private static void ThrowLibNotFoundAny(string[] names)
60+
private static void ThrowLibNotFoundAny(string[] names, PathResolver pathResolver)
7861
{
7962
throw new FileNotFoundException
80-
($"Could not find or load the native library from any name: [ {string.Join(", ", names)} ]");
63+
($"Could not find or load the native library from any name: [ {string.Join(", ", names.Select(x => x + " Attempted: (" + string.Join(", ", pathResolver.EnumeratePossibleLibraryLoadTargets(x).Select(x2 => "\"" + x2 + "\"")) + ")"))} ]");
8164
}
8265

8366
/// <summary>
@@ -90,10 +73,7 @@ private static void ThrowLibNotFoundAny(string[] names)
9073
/// <returns>The operating system handle for the shared library.</returns>
9174
public bool TryLoadNativeLibrary(string[] names, out nint result)
9275
{
93-
var success = TryLoadNativeLibrary(names, PathResolver.Default, out var libPtr);
94-
result = libPtr;
95-
96-
return success;
76+
return TryLoadNativeLibrary(names, PathResolver.Default, out result);
9777
}
9878

9979
/// <summary>
@@ -114,7 +94,7 @@ public nint LoadNativeLibrary(string name, PathResolver pathResolver)
11494

11595
if (!success)
11696
{
117-
ThrowLibNotFound(name);
97+
ThrowLibNotFound(name, pathResolver);
11898
return default;
11999
}
120100

@@ -161,7 +141,7 @@ public nint LoadNativeLibrary(string[] names, PathResolver pathResolver)
161141

162142
if (!success)
163143
{
164-
ThrowLibNotFoundAny(names);
144+
ThrowLibNotFoundAny(names, pathResolver);
165145
return default;
166146
}
167147

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Reflection;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
8+
using BenchmarkDotNet.Attributes;
9+
using BenchmarkDotNet.Configs;
10+
using BenchmarkDotNet.Order;
11+
using Silk.NET.Core.Contexts;
12+
13+
namespace InvokeBenchmarks
14+
{
15+
[CategoriesColumn]
16+
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
17+
public class BaseBenchmark
18+
{
19+
private TestContext _testContext;
20+
private SilkAPI _silkAPI;
21+
private static readonly byte[] _buff = new byte[5];
22+
23+
[GlobalSetup]
24+
public void GlobalSetup()
25+
{
26+
_testContext = new TestContext();
27+
_silkAPI = new SilkAPI(_testContext);
28+
}
29+
30+
[GlobalCleanup]
31+
public void GlobalCleanup()
32+
{
33+
_testContext.Dispose();
34+
_silkAPI.Dispose();
35+
}
36+
37+
[Benchmark]
38+
[BenchmarkCategory("EmptyInvoke")]
39+
public void EmptyInvokeSilk()
40+
{
41+
_silkAPI.RawInvoke();
42+
}
43+
44+
[Benchmark]
45+
[BenchmarkCategory("WithReturn")]
46+
public int WithReturnSilk()
47+
{
48+
return _silkAPI.WithReturn();
49+
}
50+
51+
[Benchmark(Baseline = true)]
52+
[BenchmarkCategory("EmptyInvoke")]
53+
public void EmptyInvokePInvoke()
54+
{
55+
PInvoke.RawInvoke();
56+
}
57+
58+
[Benchmark(Baseline = true)]
59+
[BenchmarkCategory("WithReturn")]
60+
public int WithReturnPInvoke()
61+
{
62+
return PInvoke.WithReturn();
63+
}
64+
}
65+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Reflection;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
8+
using BenchmarkDotNet.Attributes;
9+
using BenchmarkDotNet.Configs;
10+
using BenchmarkDotNet.Order;
11+
using Silk.NET.Core.Contexts;
12+
13+
namespace InvokeBenchmarks
14+
{
15+
[CategoriesColumn]
16+
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
17+
public class BoolBenchmark
18+
{
19+
private TestContext _testContext;
20+
private SilkAPI _silkAPI;
21+
22+
[GlobalSetup]
23+
public void GlobalSetup()
24+
{
25+
_testContext = new TestContext();
26+
_silkAPI = new SilkAPI(_testContext);
27+
}
28+
29+
[GlobalCleanup]
30+
public void GlobalCleanup()
31+
{
32+
_testContext.Dispose();
33+
_silkAPI.Dispose();
34+
}
35+
[Benchmark]
36+
[BenchmarkCategory("SystemBool")]
37+
public int WithReturnAndSystemBoolMarshalSilk()
38+
{
39+
return _silkAPI.WithReturnAndSystemBoolMarshal(true);
40+
}
41+
[Benchmark(Baseline = true)]
42+
[BenchmarkCategory("SystemBool")]
43+
public int WithReturnAndSystemBoolMarshalPInvoke()
44+
{
45+
return PInvoke.WithReturnAndSystemBoolMarshal(true);
46+
}
47+
[Benchmark]
48+
[BenchmarkCategory("VariantBool")]
49+
public int WithReturnAndVariantBoolMarshalSilk()
50+
{
51+
return _silkAPI.WithReturnAndVariantBoolMarshal(true);
52+
}
53+
[Benchmark(Baseline = true)]
54+
[BenchmarkCategory("VariantBool")]
55+
public int WithReturnAndVariantBoolMarshalPInvoke()
56+
{
57+
return PInvoke.WithReturnAndVariantBoolMarshal(true);
58+
}
59+
[Benchmark]
60+
[BenchmarkCategory("I2Bool")]
61+
public int WithReturnAndI2BoolMarshalSilk()
62+
{
63+
return _silkAPI.WithReturnAndI2BoolMarshal(true);
64+
}
65+
[Benchmark(Baseline = true)]
66+
[BenchmarkCategory("I2Bool")]
67+
public int WithReturnAndI2BoolMarshalPInvoke()
68+
{
69+
return PInvoke.WithReturnAndI2BoolMarshal(true);
70+
}
71+
[Benchmark]
72+
[BenchmarkCategory("I4Bool")]
73+
public int WithReturnAndI4BoolMarshalSilk()
74+
{
75+
return _silkAPI.WithReturnAndI4BoolMarshal(true);
76+
}
77+
[Benchmark(Baseline = true)]
78+
[BenchmarkCategory("I4Bool")]
79+
public int WithReturnAndI4BoolMarshalPInvoke()
80+
{
81+
return PInvoke.WithReturnAndI4BoolMarshal(true);
82+
}
83+
[Benchmark]
84+
[BenchmarkCategory("U2Bool")]
85+
public int WithReturnAndU2BoolMarshalSilk()
86+
{
87+
return _silkAPI.WithReturnAndU2BoolMarshal(true);
88+
}
89+
[Benchmark(Baseline = true)]
90+
[BenchmarkCategory("U2Bool")]
91+
public int WithReturnAndU2BoolMarshalPInvoke()
92+
{
93+
return PInvoke.WithReturnAndU2BoolMarshal(true);
94+
}
95+
[Benchmark]
96+
[BenchmarkCategory("U4Bool")]
97+
public int WithReturnAndU4BoolMarshalSilk()
98+
{
99+
return _silkAPI.WithReturnAndU4BoolMarshal(true);
100+
}
101+
[Benchmark(Baseline = true)]
102+
[BenchmarkCategory("U4Bool")]
103+
public int WithReturnAndU4BoolMarshalPInvoke()
104+
{
105+
return PInvoke.WithReturnAndI4BoolMarshal(true);
106+
}
107+
}
108+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
7+
<LangVersion>9</LangVersion>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\..\Core\Silk.NET.Core\Silk.NET.Core.csproj" />
16+
<ProjectReference Include="..\..\..\Core\Silk.NET.SilkTouch\Silk.NET.SilkTouch.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<None Update="TestLib.dll">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</None>
23+
</ItemGroup>
24+
25+
</Project>

0 commit comments

Comments
 (0)