From 1d2907fb4eace37776d93e5e36905c8d1ee0bb82 Mon Sep 17 00:00:00 2001 From: ArtisticRoomba <145879011+ArtisticRoomba@users.noreply.github.com> Date: Fri, 27 Jun 2025 04:37:00 -0700 Subject: [PATCH] Fix the sensor monitoring console forcing a GC every 3 seconds (#38146) * Optimize sensor monitoring window graph drawing * Add shared static Vector2 pool for all GraphView instances * Address requested changes * remove lock --- .../SensorMonitoringWindow.xaml.cs | 48 +++++++++++++++---- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/Content.Client/SensorMonitoring/SensorMonitoringWindow.xaml.cs b/Content.Client/SensorMonitoring/SensorMonitoringWindow.xaml.cs index 717ab40765..e833e7d7a9 100644 --- a/Content.Client/SensorMonitoring/SensorMonitoringWindow.xaml.cs +++ b/Content.Client/SensorMonitoring/SensorMonitoringWindow.xaml.cs @@ -25,6 +25,29 @@ public sealed partial class SensorMonitoringWindow : FancyWindow, IComputerWindo private TimeSpan _retentionTime; private readonly Dictionary _sensorData = new(); + /// + /// A shared array used to store vertices for drawing graphs in . + /// Prevents excessive allocations by reusing the same array across multiple graph views. + /// This effectively makes it so that each has its own pooled array. + /// + private Vector2[] _sharedVertices = []; + + /// + /// Retrieves a shared array of vertices, ensuring that it has at least the requested size. + /// Assigns a new array of the requested size if the current shared array is smaller than the requested size. + /// + /// The minimum number of vertices required in the shared array. + /// An array of representing the shared vertices. + /// This does not prevent other threads from accessing the same shared pool. + public Vector2[] GetSharedVertices(int requestedSize) + { + if (_sharedVertices.Length < requestedSize) + { + _sharedVertices = new Vector2[requestedSize]; + } + return _sharedVertices; + } + public SensorMonitoringWindow() { RobustXamlLoader.Load(this); @@ -145,7 +168,7 @@ public sealed partial class SensorMonitoringWindow : FancyWindow, IComputerWindo } }); - Asdf.AddChild(new GraphView(stream.Samples, startTime, curTime, maxValue * 1.1f) { MinHeight = 150 }); + Asdf.AddChild(new GraphView(stream.Samples, startTime, curTime, maxValue * 1.1f, this) { MinHeight = 150 }); Asdf.AddChild(new PanelContainer { StyleClasses = { StyleBase.ClassLowDivider } }); } } @@ -197,31 +220,37 @@ public sealed partial class SensorMonitoringWindow : FancyWindow, IComputerWindo private readonly TimeSpan _startTime; private readonly TimeSpan _curTime; private readonly float _maxY; + private readonly SensorMonitoringWindow _parentWindow; - public GraphView(Queue samples, TimeSpan startTime, TimeSpan curTime, float maxY) + public GraphView(Queue samples, + TimeSpan startTime, + TimeSpan curTime, + float maxY, + SensorMonitoringWindow parentWindow) { _samples = samples; _startTime = startTime; _curTime = curTime; _maxY = maxY; RectClipContent = true; + _parentWindow = parentWindow; } protected override void Draw(DrawingHandleScreen handle) { base.Draw(handle); - var window = (float) (_curTime - _startTime).TotalSeconds; - - // TODO: omg this is terrible don't fucking hardcode this size to something uncached huge omfg. - var vertices = new Vector2[25000]; + var window = (float)(_curTime - _startTime).TotalSeconds; var countVtx = 0; var lastPoint = new Vector2(float.NaN, float.NaN); + var requiredVertices = 6 * (_samples.Count - 1); + + var vertices = _parentWindow.GetSharedVertices(requiredVertices); foreach (var (time, sample) in _samples) { - var relTime = (float) (time - _startTime).TotalSeconds; + var relTime = (float)(time - _startTime).TotalSeconds; var posY = PixelHeight - (sample / _maxY) * PixelHeight; var posX = (relTime / window) * PixelWidth; @@ -242,8 +271,9 @@ public sealed partial class SensorMonitoringWindow : FancyWindow, IComputerWindo lastPoint = newPoint; } - - handle.DrawPrimitives(DrawPrimitiveTopology.TriangleList, vertices.AsSpan(0, countVtx), Color.White.WithAlpha(0.1f)); + handle.DrawPrimitives(DrawPrimitiveTopology.TriangleList, + vertices.AsSpan(0, countVtx), + Color.White.WithAlpha(0.1f)); } } } -- 2.51.2