diff --git a/osu.Framework/Graphics/Rendering/Deferred/Allocation/ResourceAllocator.cs b/osu.Framework/Graphics/Rendering/Deferred/Allocation/ResourceAllocator.cs index 8dcaec9862..adf08edcd3 100644 --- a/osu.Framework/Graphics/Rendering/Deferred/Allocation/ResourceAllocator.cs +++ b/osu.Framework/Graphics/Rendering/Deferred/Allocation/ResourceAllocator.cs @@ -11,6 +11,8 @@ namespace osu.Framework.Graphics.Rendering.Deferred.Allocation { public class ResourceAllocator { + private const int min_buffer_size = 1024 * 1024; // 1MB + private readonly List resources = new List(); private readonly List memoryBuffers = new List(); @@ -36,7 +38,7 @@ public RendererMemoryBlock Allocate() int requiredSize = Marshal.SizeOf(); if (memoryBuffers.Count == 0 || memoryBuffers[^1].Remaining < requiredSize) - memoryBuffers.Add(new MemoryBuffer(memoryBuffers.Count, requiredSize)); + memoryBuffers.Add(new MemoryBuffer(memoryBuffers.Count, Math.Max(min_buffer_size, requiredSize))); return memoryBuffers[^1].Reserve(requiredSize); } diff --git a/osu.Framework/Graphics/Rendering/Deferred/EventList.cs b/osu.Framework/Graphics/Rendering/Deferred/EventList.cs index e452a848c2..6f88f2ded5 100644 --- a/osu.Framework/Graphics/Rendering/Deferred/EventList.cs +++ b/osu.Framework/Graphics/Rendering/Deferred/EventList.cs @@ -2,9 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using System; -using System.Buffers; using System.Collections.Generic; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using osu.Framework.Graphics.Rendering.Deferred.Events; @@ -22,15 +20,13 @@ public void Reset() public void Enqueue(T renderEvent) where T : unmanaged, IRenderEvent { - int size = Unsafe.SizeOf() + 1; - byte[] bytes = ArrayPool.Shared.Rent(size); + ReadOnlySpan eventBytes = MemoryMarshal.Cast(MemoryMarshal.CreateReadOnlySpan(ref renderEvent, 1)); - bytes[0] = (byte)renderEvent.Type; - MemoryMarshal.Write(bytes.AsSpan()[1..], ref renderEvent); + renderEvents.EnsureCapacity(renderEvents.Count + eventBytes.Length + 1); - renderEvents.AddRange(new ArraySegment(bytes, 0, size)); - - ArrayPool.Shared.Return(bytes); + renderEvents.Add((byte)renderEvent.Type); + foreach (byte b in eventBytes) + renderEvents.Add(b); } public EventListReader CreateReader() => new EventListReader(renderEvents);