Skip to content

Commit

Permalink
refactor: c# 12, .net 8, upgraded all dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
paviad committed Jul 26, 2024
1 parent 0977cd6 commit ce14c0b
Show file tree
Hide file tree
Showing 172 changed files with 10,879 additions and 12,325 deletions.
7 changes: 6 additions & 1 deletion MarcusW.VncClient.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/CodeAnnotations/NamespacesWithAnnotations/=WpfVncClient_002EAnnotations/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/CodeInspection/CodeAnnotations/NamespacesWithAnnotations/=WpfVncClient_002EAnnotations/@EntryIndexedValue">True</s:Boolean>




</wpf:ResourceDictionary>
22 changes: 10 additions & 12 deletions benchmarks/Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>Exe</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
</ItemGroup>

</Project>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
</ItemGroup>
</Project>
143 changes: 71 additions & 72 deletions benchmarks/Benchmarks/BufferWriting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,95 +2,94 @@
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;

namespace Benchmarks
namespace Benchmarks;

public class BufferWriting
{
public class BufferWriting
private readonly byte[] _buffer = new byte[1920 * 1080 * 4];

[Benchmark]
public void ArrayIndexer()
{
private readonly byte[] _buffer = new byte[1920 * 1080 * 4];
for (var i = 0; i < _buffer.Length; i += 4)
SetPixelArrayIndexer(_buffer, i, 0xffffffff);
}

[Benchmark]
public void ArrayIndexer()
[Benchmark]
public unsafe void Pointer()
{
fixed (byte* ptr = &_buffer[0])
{
for (int i = 0; i < _buffer.Length; i += 4)
SetPixelArrayIndexer(_buffer, i, 0xffffffff);
for (var i = 0; i < _buffer.Length; i += 4)
SetPixelPointer(ptr + i, 0xffffffff);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetPixelArrayIndexer(byte[] buffer, int i, uint color)
[Benchmark]
public unsafe void PointerMemcpy()
{
fixed (byte* ptr = &_buffer[0])
{
buffer[i] = (byte)(color & 0xff);
buffer[i + 1] = (byte)((color >> 8) & 0xff);
buffer[i + 2] = (byte)((color >> 16) & 0xff);
buffer[i + 3] = (byte)((color >> 24) & 0xff);
for (var i = 0; i < _buffer.Length; i += 4)
SetPixelPointerMemcpy(ptr + i, 0xffffffff);
}
}

[Benchmark]
public void Span()
[Benchmark]
public unsafe void PointerReinterpretCast()
{
fixed (byte* ptr = &_buffer[0])
{
Span<byte> buffer = _buffer;

for (int i = 0; i < _buffer.Length; i += 4)
SetPixelSpan(buffer.Slice(i, 4), 0xffffffff);
for (var i = 0; i < _buffer.Length; i += 4)
SetPixelPointerReinterpretCast(ptr + i, 0xffffffff);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetPixelSpan(in Span<byte> span, uint color)
{
span[0] = (byte)(color & 0xff);
span[1] = (byte)((color >> 8) & 0xff);
span[2] = (byte)((color >> 16) & 0xff);
span[3] = (byte)((color >> 24) & 0xff);
}
[Benchmark]
public void Span()
{
Span<byte> buffer = _buffer;

[Benchmark]
public unsafe void Pointer()
{
fixed (byte* ptr = &_buffer[0])
{
for (int i = 0; i < _buffer.Length; i += 4)
SetPixelPointer(ptr + i, 0xffffffff);
}
}
for (var i = 0; i < _buffer.Length; i += 4)
SetPixelSpan(buffer.Slice(i, 4), 0xffffffff);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void SetPixelPointer(byte* ptr, uint color)
{
*ptr++ = (byte)(color & 0xff);
*ptr++ = (byte)((color >> 8) & 0xff);
*ptr++ = (byte)((color >> 16) & 0xff);
*ptr = (byte)((color >> 24) & 0xff);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetPixelArrayIndexer(byte[] buffer, int i, uint color)
{
buffer[i] = (byte)(color & 0xff);
buffer[i + 1] = (byte)((color >> 8) & 0xff);
buffer[i + 2] = (byte)((color >> 16) & 0xff);
buffer[i + 3] = (byte)((color >> 24) & 0xff);
}

[Benchmark]
public unsafe void PointerReinterpretCast()
{
fixed (byte* ptr = &_buffer[0])
{
for (int i = 0; i < _buffer.Length; i += 4)
SetPixelPointerReinterpretCast(ptr + i, 0xffffffff);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void SetPixelPointer(byte* ptr, uint color)
{
*ptr++ = (byte)(color & 0xff);
*ptr++ = (byte)((color >> 8) & 0xff);
*ptr++ = (byte)((color >> 16) & 0xff);
*ptr = (byte)((color >> 24) & 0xff);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void SetPixelPointerReinterpretCast(byte* ptr, uint color)
{
*(uint*)ptr = color;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void SetPixelPointerMemcpy(byte* ptr, uint color)
{
Unsafe.CopyBlock(ptr, &color, sizeof(uint));
}

[Benchmark]
public unsafe void PointerMemcpy()
{
fixed (byte* ptr = &_buffer[0])
{
for (int i = 0; i < _buffer.Length; i += 4)
SetPixelPointerMemcpy(ptr + i, 0xffffffff);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void SetPixelPointerReinterpretCast(byte* ptr, uint color)
{
*(uint*)ptr = color;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void SetPixelPointerMemcpy(byte* ptr, uint color)
{
Unsafe.CopyBlock(ptr, &color, sizeof(uint));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetPixelSpan(in Span<byte> span, uint color)
{
span[0] = (byte)(color & 0xff);
span[1] = (byte)((color >> 8) & 0xff);
span[2] = (byte)((color >> 16) & 0xff);
span[3] = (byte)((color >> 24) & 0xff);
}
}
49 changes: 20 additions & 29 deletions benchmarks/Benchmarks/ImmutableDictionaryLookup.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using BenchmarkDotNet.Attributes;

namespace Benchmarks
namespace Benchmarks;

public class ImmutableDictionaryLookup
{
public class ImmutableDictionaryLookup
{
private const int Index = 500;
private readonly IImmutableDictionary<int, object> _dictionary= Enumerable.Range(0, 1000).ToImmutableDictionary(i => i, i => new object());
private const int Index = 500;

[Benchmark]
public object Indexer()
{
if (!_dictionary.ContainsKey(Index))
return null;
return _dictionary[Index];
}
private readonly IImmutableDictionary<int, object> _dictionary =
Enumerable.Range(0, 1000).ToImmutableDictionary(i => i, _ => new object());

[Benchmark]
public object TryGet()
[Benchmark]
public object? Indexer() => CollectionExtensions.GetValueOrDefault(_dictionary, Index);

[Benchmark]
public object? TryCatch()
{
try
{
if (!_dictionary.TryGetValue(Index, out object value))
return null;
return value;
return _dictionary[Index];
}

[Benchmark]
public object TryCatch()
catch
{
try
{
return _dictionary[Index];
}
catch
{
return null;
}
return null;
}
}

[Benchmark]
public object? TryGet() => CollectionExtensions.GetValueOrDefault(_dictionary, Index);
}
29 changes: 14 additions & 15 deletions benchmarks/Benchmarks/LockOverhead.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
using BenchmarkDotNet.Attributes;

namespace Benchmarks
namespace Benchmarks;

public class LockOverhead
{
public class LockOverhead
{
private int _value;
private object _lock = new object();
private readonly object _lock = new();
private int _value;

[Benchmark]
public void IncreaseWithoutLock()
{
[Benchmark]
public void IncreaseWithLock()
{
lock (_lock)
_value++;
}
}

[Benchmark]
public void IncreaseWithLock()
{
lock (_lock)
_value++;
}
[Benchmark]
public void IncreaseWithoutLock()
{
_value++;
}
}
33 changes: 16 additions & 17 deletions benchmarks/Benchmarks/MaxValueToDepth.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
using System.Runtime.Intrinsics.X86;
using BenchmarkDotNet.Attributes;

namespace Benchmarks
namespace Benchmarks;

public class MaxValueToDepth
{
public class MaxValueToDepth
{
private const int MaxValue = 255;
private const int MaxValue = 255;

[Benchmark]
public uint WhileLoop()
{
uint val = MaxValue;
uint depth = 0;
while (val != 0)
{
depth++;
val >>= 1;
}
[Benchmark]
public uint PopCount() => Popcnt.PopCount(MaxValue);

return depth;
[Benchmark]
public uint WhileLoop()
{
uint val = MaxValue;
uint depth = 0;
while (val != 0)
{
depth++;
val >>= 1;
}

[Benchmark]
public uint PopCount() => Popcnt.PopCount(MaxValue);
return depth;
}
}
Loading

0 comments on commit ce14c0b

Please sign in to comment.