Skip to content

Instantly share code, notes, and snippets.

Avatar

Olivier Coanet ocoanet

View GitHub Profile
View Pipeline_15
| Method | Mean | Error | StdDev | Ratio |
|------------------- |----------:|----------:|----------:|------:|
| ReadWriteTyped | 0.6410 ns | 0.0016 ns | 0.0013 ns | 1.00 |
| ReadWriteSolution1 | 1.0355 ns | 0.0034 ns | 0.0030 ns | 1.62 |
| ReadWriteSolution3 | 1.0345 ns | 0.0039 ns | 0.0036 ns | 1.61 |
View Pipeline_14.cs
private static class TypeCache<T>
{
public static readonly bool CanUsePadding16 =
!IsReferenceOrContainsReferences(typeof(T)) && Unsafe.SizeOf<T>() <= 16;
// No static constructor
private static bool IsReferenceOrContainsReferences(Type type)
{
// Custom implementation using reflection
View Pipeline_13
| Method | Mean | Error | StdDev | Ratio |
|------------------- |----------:|----------:|----------:|------:|
| ReadWriteTyped | 0.6433 ns | 0.0041 ns | 0.0036 ns | 1.00 |
| ReadWriteSolution1 | 1.0042 ns | 0.0033 ns | 0.0027 ns | 1.56 |
| ReadWriteSolution3 | 1.0082 ns | 0.0055 ns | 0.0049 ns | 1.57 |
View Pipeline_12.cs
public void Init<T>() => GC.KeepAlive(TypeCache<T>.CanUsePadding16);
View Pipeline_11
| Method | Mean | Error | StdDev | Ratio |
|------------------- |----------:|----------:|----------:|------:|
| ReadWriteTyped | 0.6394 ns | 0.0013 ns | 0.0011 ns | 1.00 |
| ReadWriteSolution1 | 1.0342 ns | 0.0023 ns | 0.0020 ns | 1.62 |
| ReadWriteSolution3 | 4.2584 ns | 0.0021 ns | 0.0018 ns | 6.66 |
View Pipeline_10.cs
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Read<T>()
{
if (TypeCache<T>.CanUsePadding16)
return Unsafe.As<Padding16, T>(ref _valueStorage);
return (T)_defaultStorage;
}
public static class TypeCache<T>
View Pipeline_9
| Method | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated |
|------------------- |----------:|----------:|----------:|------:|------:|------:|------:|----------:|
| ReadWriteTyped | 0.6390 ns | 0.0005 ns | 0.0004 ns | 1.00 | - | - | - | - |
| ReadWriteSolution1 | 1.0009 ns | 0.0005 ns | 0.0004 ns | 1.57 | - | - | - | - |
| ReadWriteSolution2 | 0.9996 ns | 0.0007 ns | 0.0006 ns | 1.56 | - | - | - | - |
View Pipeline_8.cs
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Read<T>()
{
if (!RuntimeHelpers.IsReferenceOrContainsReferences<T>() && Unsafe.SizeOf<T>() <= 16)
return Unsafe.As<Padding16, T>(ref _valueStorage);
return (T)_defaultStorage;
}
View Pipeline_7
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
|------------------- |----------:|----------:|----------:|------:|--------:|-------:|------:|------:|----------:|
| ReadWriteTyped | 0.6415 ns | 0.0008 ns | 0.0007 ns | 1.00 | 0.00 | - | - | - | - |
| ReadWriteBoxing | 4.3225 ns | 0.0385 ns | 0.0360 ns | 6.74 | 0.06 | 0.0057 | - | - | 24 B |
| ReadWriteSolution1 | 1.0003 ns | 0.0023 ns | 0.0020 ns | 1.56 | 0.00 | - | - | - | - |
View Pipeline_6.cs
public class PipelineStepEventTyped<T>
{
private T _value;
public void Write(T value) => _value = value;
public T Read() => _value;
}