Skip to content

Commit

Permalink
Optimized things
Browse files Browse the repository at this point in the history
  • Loading branch information
Simnico99 committed Mar 16, 2023
1 parent 09822f5 commit 8b8a4fa
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 68 deletions.
47 changes: 19 additions & 28 deletions src/ZirconNet.Core/Extensions/ReadOnlySpanCharExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,47 @@ namespace ZirconNet.Core.Extensions;

public static class ReadOnlySpanCharExtension
{
public static T[] SplitToArray<T>(this ReadOnlySpan<char> span, char comparison, TryParseHandler<T> tryParseHandler, int startIndex = 0, int sliceLenght = 0, int size = 0)
public static T[] SplitToArray<T>(this ReadOnlySpan<char> span, char comparison, TryParseHandler<T> tryParseHandler, int startIndex = 0, int sliceLength = 0, int size = 0)
{
T[]? array;
if (size < 1)
{
array = new T[span.Length];
}
else
{
array = new T[size];
size = span.Length;
}

if (sliceLenght <= 0)
if (sliceLength <= 0)
{
sliceLenght = span.Length;
sliceLength = span.Length;
}

sliceLenght--;

var length = 0;
var array = new T[size];
var arrayIndex = 0;
var currentIndex = startIndex;
foreach (var i in currentIndex..(currentIndex + sliceLenght))
var length = 0;
var endIndex = currentIndex + sliceLength - 1;

for (var i = currentIndex; i <= endIndex && arrayIndex < array.Length; i++)
{
length++;

if (arrayIndex == array.Length)
{
break;
}

if (span[i] == comparison)
{
var slice = span.Slice(currentIndex, length - 1);
tryParseHandler(slice, out var result);
array[arrayIndex] = result;
array[arrayIndex++] = ParseSlice(span.Slice(currentIndex, length - 1), tryParseHandler);
currentIndex += length;
arrayIndex++;
length = 0;
continue;
}

if (i == (startIndex + sliceLenght))
else if (i == endIndex)
{
var slice = span.Slice(currentIndex, length);
tryParseHandler(slice, out var result);
array[arrayIndex] = result;
array[arrayIndex++] = ParseSlice(span.Slice(currentIndex, length), tryParseHandler);
}
}

return array;
}

private static T ParseSlice<T>(ReadOnlySpan<char> slice, TryParseHandler<T> tryParseHandler)
{
tryParseHandler(slice, out var result);
return result;
}

}
32 changes: 17 additions & 15 deletions src/ZirconNet.Core/Extensions/SpanCharExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,39 @@ public static class SpanCharExtension
{
public static Span<char> Remove(this Span<char> chars, char charToRemove)
{
Span<char> span = new char[chars.Length];

var charPos = 0;
foreach (var character in chars)
for (var i = 0; i < chars.Length; i++)
{
if (character == charToRemove)
if (chars[i] != charToRemove)
{
continue;
chars[charPos++] = chars[i];
}

span[charPos++] = character;
}

return span[..charPos];
return chars[..charPos];
}

public static Span<char> Remove(this Span<char> chars, char[] charsToRemove)
{
Span<char> span = new char[chars.Length];

var charPos = 0;
foreach (var character in chars)
for (var i = 0; i < chars.Length; i++)
{
if (charsToRemove.Contains(character))
var shouldRemove = false;
for (var j = 0; j < charsToRemove.Length; j++)
{
continue;
if (chars[i] == charsToRemove[j])
{
shouldRemove = true;
break;
}
}

span[charPos++] = character;
if (!shouldRemove)
{
chars[charPos++] = chars[i];
}
}

return span[..charPos];
return chars[..charPos];
}
}
32 changes: 18 additions & 14 deletions src/ZirconNet.Core/Runtime/DynamicClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,39 @@
namespace ZirconNet.Core.Runtime;
public sealed class DynamicClass : DynamicObject
{
private readonly Dictionary<string, KeyValuePair<Type, object?>> _fields;
private readonly Dictionary<string, (Type Type, object? Value)> _fields;

public DynamicClass(IEnumerable<DynamicClassField> fields)
{
_fields = new Dictionary<string, KeyValuePair<Type, object?>>();
_fields = new Dictionary<string, (Type Type, object? Value)>();
foreach (var field in fields)
{
_fields.Add(field.FieldName, new KeyValuePair<Type, object?>(field.FieldType, field.Value));
_fields.Add(field.FieldName, (field.FieldType, field.Value));
}
}

public override bool TrySetMember(SetMemberBinder binder, object? value)
{
if (!_fields.ContainsKey(binder.Name))
if (_fields.TryGetValue(binder.Name, out var fieldInfo))
{
return false;
if (value?.GetType() == fieldInfo.Type || value == null)
{
_fields[binder.Name] = (fieldInfo.Type, value);
return true;
}
throw new ArgumentException($"{value} type ({value?.GetType()}) is not the same as the Field ({fieldInfo.Type.Name})", nameof(value));
}
var type = _fields[binder.Name].Key;
if (value?.GetType() == type)
{
_fields[binder.Name] = new KeyValuePair<Type, object?>(type, value);
return true;
}
throw new ArgumentException($"{value} type ({value?.GetType()}) is not the same as the Field ({type.Name})", nameof(value));
return false;
}

public override bool TryGetMember(GetMemberBinder binder, out object? result)
{
result = _fields[binder.Name].Value;
return true;
if (_fields.TryGetValue(binder.Name, out var fieldInfo))
{
result = fieldInfo.Value;
return true;
}
result = null;
return false;
}
}
24 changes: 13 additions & 11 deletions src/ZirconNet.WPF/Dispatcher/BufferedThreadDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Concurrent;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

Expand All @@ -8,29 +7,27 @@ namespace ZirconNet.WPF.Dispatcher;
/// <summary>
/// Prevent the ui freeze by using a buffer to execute the UI Updates from multiple threads in a sort of queue.,
/// </summary>
public sealed class BufferedThreadDispatcher
public sealed class BufferedThreadDispatcher : IDisposable
{
public static BufferedThreadDispatcher Current { get; } = new();

/// <summary>
/// Delay to wait between the screen refresh.
/// </summary>
public TimeSpan Delay { get; set; } = TimeSpan.FromMilliseconds(1);

private readonly int _mainThreadId;
private readonly System.Windows.Threading.Dispatcher _dispatcher;
private readonly ConcurrentQueue<Action> _queue = new();
private readonly CancellationTokenSource _cts = new();

private BufferedThreadDispatcher()
{
_mainThreadId = Application.Current.Dispatcher.Thread.ManagedThreadId;
_dispatcher = Application.Current.Dispatcher;
Task.Run(ProcessQueue);
Task.Factory.StartNew(() => ProcessQueue(_cts.Token), _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}

private async void ProcessQueue()
private async Task ProcessQueue(CancellationToken cancellationToken)
{
while (true)
while (!cancellationToken.IsCancellationRequested)
{
if (_queue.TryDequeue(out var action))
{
Expand All @@ -40,10 +37,10 @@ private async void ProcessQueue()
}
else
{
_dispatcher.Invoke(action, DispatcherPriority.Send);
_dispatcher.Invoke(action, DispatcherPriority.Send, cancellationToken);
}
}
await Task.Delay(Delay);
await Task.Delay(Delay, cancellationToken);
}
}

Expand Down Expand Up @@ -87,4 +84,9 @@ public async Task InvokeAsync(Action act)
});
await tcs.Task;
}
}

public void Dispose()
{
_cts.Cancel();
}
}

0 comments on commit 8b8a4fa

Please sign in to comment.