Skip to content

Commit 8b8a4fa

Browse files
committed
Optimized things
1 parent 09822f5 commit 8b8a4fa

File tree

4 files changed

+67
-68
lines changed

4 files changed

+67
-68
lines changed

src/ZirconNet.Core/Extensions/ReadOnlySpanCharExtension.cs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,47 @@ namespace ZirconNet.Core.Extensions;
44

55
public static class ReadOnlySpanCharExtension
66
{
7-
public static T[] SplitToArray<T>(this ReadOnlySpan<char> span, char comparison, TryParseHandler<T> tryParseHandler, int startIndex = 0, int sliceLenght = 0, int size = 0)
7+
public static T[] SplitToArray<T>(this ReadOnlySpan<char> span, char comparison, TryParseHandler<T> tryParseHandler, int startIndex = 0, int sliceLength = 0, int size = 0)
88
{
9-
T[]? array;
109
if (size < 1)
1110
{
12-
array = new T[span.Length];
13-
}
14-
else
15-
{
16-
array = new T[size];
11+
size = span.Length;
1712
}
1813

19-
if (sliceLenght <= 0)
14+
if (sliceLength <= 0)
2015
{
21-
sliceLenght = span.Length;
16+
sliceLength = span.Length;
2217
}
2318

24-
sliceLenght--;
25-
26-
var length = 0;
19+
var array = new T[size];
2720
var arrayIndex = 0;
2821
var currentIndex = startIndex;
29-
foreach (var i in currentIndex..(currentIndex + sliceLenght))
22+
var length = 0;
23+
var endIndex = currentIndex + sliceLength - 1;
24+
25+
for (var i = currentIndex; i <= endIndex && arrayIndex < array.Length; i++)
3026
{
3127
length++;
3228

33-
if (arrayIndex == array.Length)
34-
{
35-
break;
36-
}
37-
3829
if (span[i] == comparison)
3930
{
40-
var slice = span.Slice(currentIndex, length - 1);
41-
tryParseHandler(slice, out var result);
42-
array[arrayIndex] = result;
31+
array[arrayIndex++] = ParseSlice(span.Slice(currentIndex, length - 1), tryParseHandler);
4332
currentIndex += length;
44-
arrayIndex++;
4533
length = 0;
46-
continue;
4734
}
48-
49-
if (i == (startIndex + sliceLenght))
35+
else if (i == endIndex)
5036
{
51-
var slice = span.Slice(currentIndex, length);
52-
tryParseHandler(slice, out var result);
53-
array[arrayIndex] = result;
37+
array[arrayIndex++] = ParseSlice(span.Slice(currentIndex, length), tryParseHandler);
5438
}
5539
}
5640

5741
return array;
5842
}
43+
44+
private static T ParseSlice<T>(ReadOnlySpan<char> slice, TryParseHandler<T> tryParseHandler)
45+
{
46+
tryParseHandler(slice, out var result);
47+
return result;
48+
}
49+
5950
}

src/ZirconNet.Core/Extensions/SpanCharExtension.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,39 @@ public static class SpanCharExtension
33
{
44
public static Span<char> Remove(this Span<char> chars, char charToRemove)
55
{
6-
Span<char> span = new char[chars.Length];
7-
86
var charPos = 0;
9-
foreach (var character in chars)
7+
for (var i = 0; i < chars.Length; i++)
108
{
11-
if (character == charToRemove)
9+
if (chars[i] != charToRemove)
1210
{
13-
continue;
11+
chars[charPos++] = chars[i];
1412
}
15-
16-
span[charPos++] = character;
1713
}
1814

19-
return span[..charPos];
15+
return chars[..charPos];
2016
}
2117

2218
public static Span<char> Remove(this Span<char> chars, char[] charsToRemove)
2319
{
24-
Span<char> span = new char[chars.Length];
25-
2620
var charPos = 0;
27-
foreach (var character in chars)
21+
for (var i = 0; i < chars.Length; i++)
2822
{
29-
if (charsToRemove.Contains(character))
23+
var shouldRemove = false;
24+
for (var j = 0; j < charsToRemove.Length; j++)
3025
{
31-
continue;
26+
if (chars[i] == charsToRemove[j])
27+
{
28+
shouldRemove = true;
29+
break;
30+
}
3231
}
3332

34-
span[charPos++] = character;
33+
if (!shouldRemove)
34+
{
35+
chars[charPos++] = chars[i];
36+
}
3537
}
3638

37-
return span[..charPos];
39+
return chars[..charPos];
3840
}
3941
}

src/ZirconNet.Core/Runtime/DynamicClass.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,39 @@
33
namespace ZirconNet.Core.Runtime;
44
public sealed class DynamicClass : DynamicObject
55
{
6-
private readonly Dictionary<string, KeyValuePair<Type, object?>> _fields;
6+
private readonly Dictionary<string, (Type Type, object? Value)> _fields;
77

88
public DynamicClass(IEnumerable<DynamicClassField> fields)
99
{
10-
_fields = new Dictionary<string, KeyValuePair<Type, object?>>();
10+
_fields = new Dictionary<string, (Type Type, object? Value)>();
1111
foreach (var field in fields)
1212
{
13-
_fields.Add(field.FieldName, new KeyValuePair<Type, object?>(field.FieldType, field.Value));
13+
_fields.Add(field.FieldName, (field.FieldType, field.Value));
1414
}
1515
}
1616

1717
public override bool TrySetMember(SetMemberBinder binder, object? value)
1818
{
19-
if (!_fields.ContainsKey(binder.Name))
19+
if (_fields.TryGetValue(binder.Name, out var fieldInfo))
2020
{
21-
return false;
21+
if (value?.GetType() == fieldInfo.Type || value == null)
22+
{
23+
_fields[binder.Name] = (fieldInfo.Type, value);
24+
return true;
25+
}
26+
throw new ArgumentException($"{value} type ({value?.GetType()}) is not the same as the Field ({fieldInfo.Type.Name})", nameof(value));
2227
}
23-
var type = _fields[binder.Name].Key;
24-
if (value?.GetType() == type)
25-
{
26-
_fields[binder.Name] = new KeyValuePair<Type, object?>(type, value);
27-
return true;
28-
}
29-
throw new ArgumentException($"{value} type ({value?.GetType()}) is not the same as the Field ({type.Name})", nameof(value));
28+
return false;
3029
}
3130

3231
public override bool TryGetMember(GetMemberBinder binder, out object? result)
3332
{
34-
result = _fields[binder.Name].Value;
35-
return true;
33+
if (_fields.TryGetValue(binder.Name, out var fieldInfo))
34+
{
35+
result = fieldInfo.Value;
36+
return true;
37+
}
38+
result = null;
39+
return false;
3640
}
3741
}

src/ZirconNet.WPF/Dispatcher/BufferedThreadDispatcher.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Concurrent;
2-
using System.Threading.Tasks;
32
using System.Windows;
43
using System.Windows.Threading;
54

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

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

2016
private readonly int _mainThreadId;
2117
private readonly System.Windows.Threading.Dispatcher _dispatcher;
2218
private readonly ConcurrentQueue<Action> _queue = new();
19+
private readonly CancellationTokenSource _cts = new();
2320

2421
private BufferedThreadDispatcher()
2522
{
2623
_mainThreadId = Application.Current.Dispatcher.Thread.ManagedThreadId;
2724
_dispatcher = Application.Current.Dispatcher;
28-
Task.Run(ProcessQueue);
25+
Task.Factory.StartNew(() => ProcessQueue(_cts.Token), _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
2926
}
3027

31-
private async void ProcessQueue()
28+
private async Task ProcessQueue(CancellationToken cancellationToken)
3229
{
33-
while (true)
30+
while (!cancellationToken.IsCancellationRequested)
3431
{
3532
if (_queue.TryDequeue(out var action))
3633
{
@@ -40,10 +37,10 @@ private async void ProcessQueue()
4037
}
4138
else
4239
{
43-
_dispatcher.Invoke(action, DispatcherPriority.Send);
40+
_dispatcher.Invoke(action, DispatcherPriority.Send, cancellationToken);
4441
}
4542
}
46-
await Task.Delay(Delay);
43+
await Task.Delay(Delay, cancellationToken);
4744
}
4845
}
4946

@@ -87,4 +84,9 @@ public async Task InvokeAsync(Action act)
8784
});
8885
await tcs.Task;
8986
}
90-
}
87+
88+
public void Dispose()
89+
{
90+
_cts.Cancel();
91+
}
92+
}

0 commit comments

Comments
 (0)