1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-12 21:38:38 +00:00

Merge pull request #765 from Artemis-RGB/feature/reduce-allocs

Random misc fixes
This commit is contained in:
RobertBeekman 2023-03-03 12:51:59 +01:00 committed by GitHub
commit 41dcec7d9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 19 deletions

View File

@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Artemis.Core.Properties;
@ -26,7 +27,7 @@ public abstract class CorePropertyChanged : INotifyPropertyChanged
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected bool RequiresUpdate<T>(ref T storage, T value)
{
return !Equals(storage, value);
return !EqualityComparer<T>.Default.Equals(storage, value);
}
/// <summary>

View File

@ -28,6 +28,8 @@ public abstract class PerLedLayerBrush<T> : PropertiesLayerBrush<T> where T : La
/// <returns>The color the LED will receive</returns>
public abstract SKColor GetColor(ArtemisLed led, SKPoint renderPoint);
private readonly SKPoint[] _points = new SKPoint[2];
internal override void InternalRender(SKCanvas canvas, SKRect bounds, SKPaint paint)
{
// We don't want rotation on this canvas because that'll displace the LEDs, translations are applied to the points of each LED instead
@ -37,25 +39,21 @@ public abstract class PerLedLayerBrush<T> : PropertiesLayerBrush<T> where T : La
using SKPath pointsPath = new();
foreach (ArtemisLed artemisLed in Layer.Leds)
{
pointsPath.AddPoly(new[]
{
new SKPoint(0, 0),
new SKPoint(artemisLed.AbsoluteRectangle.Left - Layer.Bounds.Left, artemisLed.AbsoluteRectangle.Top - Layer.Bounds.Top)
});
_points[0] = new SKPoint(0, 0);
_points[1] = new SKPoint(artemisLed.AbsoluteRectangle.Left - Layer.Bounds.Left, artemisLed.AbsoluteRectangle.Top - Layer.Bounds.Top);
pointsPath.AddPoly(_points);
}
// Apply the translation to the points of each LED instead
if (Layer.General.TransformMode.CurrentValue == LayerTransformMode.Normal && SupportsTransformation)
pointsPath.Transform(Layer.GetTransformMatrix(true, true, true, true).Invert());
SKPoint[] points = pointsPath.Points;
TryOrBreak(() =>
{
for (int index = 0; index < Layer.Leds.Count; index++)
{
ArtemisLed artemisLed = Layer.Leds[index];
SKPoint renderPoint = points[index * 2 + 1];
SKPoint renderPoint = pointsPath.GetPoint(index * 2 + 1);
if (!float.IsFinite(renderPoint.X) || !float.IsFinite(renderPoint.Y))
continue;

View File

@ -10,12 +10,25 @@ namespace Artemis.Core;
/// </summary>
public static class LogStore
{
private static readonly object _lock = new();
private static readonly LinkedList<LogEvent> LinkedList = new();
/// <summary>
/// Gets a list containing the last 500 log events.
/// </summary>
public static List<LogEvent> Events => LinkedList.ToList();
public static List<LogEvent> Events
{
get
{
List<LogEvent> events;
lock (_lock)
events = LinkedList.ToList();
return events;
}
}
/// <summary>
/// Occurs when a new <see cref="LogEvent" /> was received.
@ -24,9 +37,13 @@ public static class LogStore
internal static void Emit(LogEvent logEvent)
{
LinkedList.AddLast(logEvent);
while (LinkedList.Count > 500)
LinkedList.RemoveFirst();
lock (_lock)
{
LinkedList.AddLast(logEvent);
while (LinkedList.Count > 500)
LinkedList.RemoveFirst();
}
OnEventAdded(new LogEventEventArgs(logEvent));
}

View File

@ -14,7 +14,7 @@ namespace Artemis.UI.Screens.Debugger.Logs;
public class LogsDebugView : ReactiveUserControl<LogsDebugViewModel>
{
private int _lineCount;
private TextEditor _textEditor;
private TextEditor? _textEditor;
public LogsDebugView()
{
@ -31,7 +31,7 @@ public class LogsDebugView : ReactiveUserControl<LogsDebugViewModel>
protected override void OnInitialized()
{
base.OnInitialized();
Dispatcher.UIThread.Post(() => _textEditor.ScrollToEnd(), DispatcherPriority.ApplicationIdle);
Dispatcher.UIThread.Post(() => _textEditor?.ScrollToEnd(), DispatcherPriority.ApplicationIdle);
}
private void OnTextChanged(object? sender, EventArgs e)

View File

@ -48,8 +48,11 @@ public class LogsDebugViewModel : ActivatableViewModelBase
});
}
private void AddLogEvent(LogEvent logEvent)
private void AddLogEvent(LogEvent? logEvent)
{
if (logEvent is null)
return;
using StringWriter writer = new();
_formatter.Format(logEvent, writer);
string line = writer.ToString();
@ -60,7 +63,7 @@ public class LogsDebugViewModel : ActivatableViewModelBase
private void RemoveOldestLine()
{
int firstNewLine = Document.Text.IndexOf('\n');
int firstNewLine = Document.IndexOf('\n', 0, Document.TextLength);
if (firstNewLine == -1)
{
//this should never happen.

View File

@ -52,7 +52,7 @@ public class StartupWizardViewModel : DialogViewModelBase<bool>
DeviceProviders = new ObservableCollection<PluginViewModel>(pluginManagementService.GetAllPlugins()
.Where(p => p.Info.IsCompatible && p.Features.Any(f => f.AlwaysEnabled && f.FeatureType.IsAssignableTo(typeof(DeviceProvider))))
.OrderBy(p => p.Info.Name)
.Select(p => settingsVmFactory.PluginViewModel(p, null)));
.Select(p => settingsVmFactory.PluginViewModel(p, ReactiveCommand.Create(() => new Unit()))));
CurrentStep = 1;
SetupButtons();