PersistentMap/TestProject1/StandardStrategy.cs
Linus Björnstam 9242c1c751 perf: Optimize non-prefix key strategies and
memory usage

- Conditionally allocate prefix buffers in `LeafNode` and introduce `PrefixInternalNode` to reduce memory overhead when prefixes are disabled.
- Bypass prefix calculation and logic entirely when `UsesPrefixes` is false.
- Add a binary search fallback for key scanning.
- Implement a dedicated `int` scanning fast-path, removing SIMD prefix usage from `IntStrategy`.
- Reorganize key strategies into separate files.
- Add a new benchmark project specifically for string keys.
2026-04-22 15:55:33 +02:00

38 lines
No EOL
1.4 KiB
C#

namespace PersistentMap.Tests;
using PersistentMap;
using System.Linq;
using Xunit;
public class StandardStrategy
{
private static string GenerateRandomString(int length, Random rnd)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
return new string(Enumerable.Repeat(chars, length).Select(s => s[rnd.Next(s.Length)]).ToArray());
}
[Fact]
public void Setup()
{
var N = 1000;
var _stdStrategy = new StandardStrategy<string>();
var _uniStrategy = new UnicodeStrategy();
var rnd = new Random(42);
var StringLength = 10;
// Build random strings
var _allKeys = Enumerable.Range(0, N).Select(_ => GenerateRandomString(StringLength, rnd)).Distinct().ToArray();
// Regenerate if Distinct() reduced array size (highly unlikely with length 8/50, but safe)
while (_allKeys.Length < N)
{
_allKeys = _allKeys.Concat(new[] { GenerateRandomString(StringLength, rnd) }).Distinct().ToArray();
}
var transStd = BaseOrderedMap<string, int, StandardStrategy<string>>.CreateTransient(_stdStrategy);
var transUni = BaseOrderedMap<string, int, UnicodeStrategy>.CreateTransient(_uniStrategy);
for (int i = 0; i < _allKeys.Length; i++)
{
transStd.Set(_allKeys[i], i);
transUni.Set(_allKeys[i], i);
}
}
}