Skip to content

Commit

Permalink
Reduce some overheads
Browse files Browse the repository at this point in the history
  • Loading branch information
smoogipoo committed Jan 7, 2024
1 parent 1dbbf7f commit 929f547
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<object> resources = new List<object>();
private readonly List<MemoryBuffer> memoryBuffers = new List<MemoryBuffer>();

Expand All @@ -36,7 +38,7 @@ public RendererMemoryBlock Allocate<T>()
int requiredSize = Marshal.SizeOf<T>();

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);
}
Expand Down
14 changes: 5 additions & 9 deletions osu.Framework/Graphics/Rendering/Deferred/EventList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,15 +20,13 @@ public void Reset()
public void Enqueue<T>(T renderEvent)
where T : unmanaged, IRenderEvent
{
int size = Unsafe.SizeOf<T>() + 1;
byte[] bytes = ArrayPool<byte>.Shared.Rent(size);
ReadOnlySpan<byte> eventBytes = MemoryMarshal.Cast<T, byte>(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<byte>(bytes, 0, size));

ArrayPool<byte>.Shared.Return(bytes);
renderEvents.Add((byte)renderEvent.Type);
foreach (byte b in eventBytes)
renderEvents.Add(b);
}

public EventListReader CreateReader() => new EventListReader(renderEvents);
Expand Down

0 comments on commit 929f547

Please sign in to comment.