-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
115 lines (81 loc) · 2.26 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using BenchmarkDotNet.Running;
using lowlevel_benchmark.Benchmarks;
using lowlevel_benchmark.Helpers;
namespace lowlevel_benchmark;
public class Program
{
public static void Main(string[] args)
{
#if DEBUG
var testInt = new NullTesting<int>();
var result = testInt.TryGet(0, out var val);
__ERROR.Assert(val != null);
//run in debug mode (can hit breakpoints in VS)
var summary = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, new BenchmarkDotNet.Configs.DebugInProcessConfig());
//run a specific benchmark
//var summary = BenchmarkRunner.Run<Parallel_Lookup>(new BenchmarkDotNet.Configs.DebugInProcessConfig());
#else
var summary = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
#endif
}
}
//public class SomeClass
//{
// private int indexCounter;
// public int GetTypeIndex<T>()
// {
// var typeLocal = new TypeLocal<int>();
// if (!typeLocal.TryGet<T>(out var index){
// index = indexCounter++;
// typeLocal.Set<T>(index);
// }
// return index;
// }
//}
///// <summary>
///// similar to ThreadLocal, but provides a value per type.
///// </summary>
///// <typeparam name="TValue"></typeparam>
//public interface TypeLocal<TValue>
//{
// public TValue Get<TType>();
// public bool TryGet<TType>(out TValue value);
// public void Set<TType>(TValue value);
// public void Remove<TType>();
// public bool HasValue<TType>();
// public IEnumerable<(Type type, TValue value)> All();
//}
public class NullTesting<TValue>
{
private TValue?[] storage = new TValue?[100];
public bool TryGet(int index, out TValue? value)
{
value = storage[index];
return value != null;
}
}
public class NullTesting_Struct<TValue> where TValue : struct
{
private Nullable<TValue>[] storage = new Nullable<TValue>[100];
public bool TryGet(int index, out TValue value)
{
var result = storage[index];
value = result.Value;
return result.HasValue;
}
}
public class NullTesting_Tuple<TValue> where TValue : struct
{
private (bool hasValue,TValue value)[] storage = new (bool valid, TValue value)[100];
public bool TryGet(int index, out TValue value)
{
var result = storage[index];
value = result.value;
return result.hasValue;
}
public ref TValue GetRef(int index)
{
return ref storage[index].value;
}
}