From f1bdcf2fdf00c3bce9c738dd3af19329aa3a4cee Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 14 Aug 2019 23:50:52 +0200 Subject: [PATCH] Minor performance improvements --- .../Extensions/GraphicsResourceExtensions.cs | 48 +++++++++++-------- .../ComputeSharp.NetCore.Tests/BufferTests.cs | 1 - 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs b/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs index 842ba39c..2f1d952e 100644 --- a/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs +++ b/src/ComputeSharp.Graphics/Buffers/Extensions/GraphicsResourceExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; using System.Reflection; @@ -12,6 +13,11 @@ namespace ComputeSharp.Graphics.Buffers.Extensions /// internal static class GraphicsResourceExtensions { + /// + /// The that maps types to the necessary data to create buffers, for quick lookup + /// + private static readonly Dictionary TypeMapping = new Dictionary(); + /// /// Allocates a new constant buffer with the specified generic value /// @@ -21,28 +27,32 @@ namespace ComputeSharp.Graphics.Buffers.Extensions [Pure] public static GraphicsResource AllocateReadOnlyBufferFromReflectedSingleValue(this GraphicsDevice device, object data) { - // Create the generic constant buffer - Type - dataType = data.GetType(), - bufferType = typeof(ReadOnlyBuffer<>).MakeGenericType(dataType); - ConstructorInfo constructor = bufferType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).First(); - GraphicsResource buffer = (GraphicsResource)constructor.Invoke(new object[] { device, 1 }); + Type dataType = data.GetType(); + if (!TypeMapping.TryGetValue(dataType, out var info)) + { + // Get the generic buffer constructor + Type bufferType = typeof(ReadOnlyBuffer<>).MakeGenericType(dataType); + info.Constructor = bufferType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).First(); - // Create the array with the input value - Type arrayType = dataType.MakeArrayType(); - ConstructorInfo[] arrayConstructors = arrayType.GetConstructors(BindingFlags.CreateInstance - | BindingFlags.Public - | BindingFlags.Instance - | BindingFlags.OptionalParamBinding); - Array array = (Array)arrayConstructors[0].Invoke(new object[] { 1 }); + // Create the reusable array + Type arrayType = dataType.MakeArrayType(); + ConstructorInfo[] arrayConstructors = arrayType.GetConstructors(BindingFlags.CreateInstance + | BindingFlags.Public + | BindingFlags.Instance + | BindingFlags.OptionalParamBinding); + info.Array = (IList)arrayConstructors[0].Invoke(new object[] { 1 }); - // Use the IList indexer to set the generic object - IList ilist = array; - ilist[0] = data; + // Set the input data to the new buffer + info.Setter = bufferType.GetMethod(nameof(HlslBuffer.SetData), new[] { arrayType }); - // Set the input data to the new buffer - MethodInfo setDataMethod = bufferType.GetMethod(nameof(HlslBuffer.SetData), new[] { arrayType }); - setDataMethod.Invoke(buffer, new object[] { array }); + // Cache for later reuse + TypeMapping.Add(dataType, info); + } + + // Create the buffer and set the content + GraphicsResource buffer = (GraphicsResource)info.Constructor.Invoke(new object[] { device, 1 }); + info.Array[0] = data; + info.Setter.Invoke(buffer, new object[] { info.Array }); return buffer; } diff --git a/tests/ComputeSharp.NetCore.Tests/BufferTests.cs b/tests/ComputeSharp.NetCore.Tests/BufferTests.cs index e2d4c621..048da74a 100644 --- a/tests/ComputeSharp.NetCore.Tests/BufferTests.cs +++ b/tests/ComputeSharp.NetCore.Tests/BufferTests.cs @@ -1,7 +1,6 @@ using System; using System.Linq; using ComputeSharp.Graphics.Buffers; -using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Buffers.Extensions; using ComputeSharp.NetCore.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting;