Rework PriorityQueue for performance.

- Providing the comparer as a type argument that is a struct allows the calls to be devirtualised, leading to approx a 3x performance improvement.
- Use a single backing array, rather than a list of arrays.
This commit is contained in:
RoosterDragon
2023-03-22 18:04:14 +00:00
committed by abcdefg30
parent 7a4ac01348
commit 8a4303cc94
4 changed files with 90 additions and 49 deletions

View File

@@ -10,10 +10,10 @@
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using OpenRA.Mods.Common;
using OpenRA.Primitives;
using OpenRA.Support;
namespace OpenRA.Test
@@ -21,6 +21,11 @@ namespace OpenRA.Test
[TestFixture]
class PriorityQueueTest
{
readonly struct Int32Comparer : IComparer<int>
{
public int Compare(int x, int y) => x.CompareTo(y);
}
[TestCase(1, 123)]
[TestCase(1, 1234)]
[TestCase(1, 12345)]
@@ -51,7 +56,7 @@ namespace OpenRA.Test
var values = Enumerable.Range(0, count);
var shuffledValues = values.Shuffle(mt).ToArray();
var queue = new PriorityQueue<int>();
var queue = new Primitives.PriorityQueue<int, Int32Comparer>(default);
Assert.IsTrue(queue.Empty, "New queue should start out empty.");
Assert.Throws<InvalidOperationException>(() => queue.Peek(), "Peeking at an empty queue should throw.");
@@ -95,7 +100,7 @@ namespace OpenRA.Test
var mt = new MersenneTwister(seed);
var shuffledValues = Enumerable.Range(0, count).Shuffle(mt).ToArray();
var queue = new PriorityQueue<int>();
var queue = new Primitives.PriorityQueue<int, Int32Comparer>(default);
Assert.IsTrue(queue.Empty, "New queue should start out empty.");
Assert.Throws<InvalidOperationException>(() => queue.Peek(), "Peeking at an empty queue should throw.");