fix prefixless search
This commit is contained in:
parent
280177a9cb
commit
a9b6ed9161
12 changed files with 1054 additions and 44 deletions
96
benchmarks/AgainstImmutableDict/AgainstImmutable.cs
Normal file
96
benchmarks/AgainstImmutableDict/AgainstImmutable.cs
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Running;
|
||||
using PersistentMap;
|
||||
|
||||
// Ensure your PersistentMap namespace is included here
|
||||
// using MyProject.Collections;
|
||||
|
||||
|
||||
[MemoryDiagnoser]
|
||||
public class ImmutableBenchmark
|
||||
{
|
||||
[Params(10, 100, 1000)]
|
||||
public int N { get; set; }
|
||||
|
||||
[Params(1000)]
|
||||
public int CollectionSize { get; set; }
|
||||
|
||||
private ImmutableDictionary<string, string> _immutableDict;
|
||||
private ImmutableSortedDictionary<string, string> _immutableSortedDict;
|
||||
|
||||
// 1. Add field for your map
|
||||
private PersistentMap<string, string, UnicodeStrategy> _persistentMap;
|
||||
|
||||
private string[] _searchKeys;
|
||||
|
||||
[GlobalSetup]
|
||||
public void Setup()
|
||||
{
|
||||
var random = new Random(42);
|
||||
var data = new Dictionary<string, string>();
|
||||
|
||||
while (data.Count < CollectionSize)
|
||||
{
|
||||
string key = GenerateRandomString(random, N);
|
||||
if (!data.ContainsKey(key))
|
||||
{
|
||||
data[key] = "value";
|
||||
}
|
||||
}
|
||||
|
||||
_immutableDict = data.ToImmutableDictionary();
|
||||
_immutableSortedDict = data.ToImmutableSortedDictionary();
|
||||
|
||||
// 2. Initialize your map.
|
||||
// ASSUMPTION: Standard immutable pattern (Add returns new instance).
|
||||
// Adjust if you have a bulk loader like .ToPersistentMap() or a constructor.
|
||||
_persistentMap = PersistentMap<string, string, UnicodeStrategy>.Empty(new UnicodeStrategy());
|
||||
foreach (var kvp in data)
|
||||
{
|
||||
_persistentMap = _persistentMap.Set(kvp.Key, kvp.Value);
|
||||
}
|
||||
|
||||
_searchKeys = data.Keys.ToArray();
|
||||
}
|
||||
|
||||
[Benchmark(Baseline = true)]
|
||||
public string ImmutableDict_Lookup()
|
||||
{
|
||||
var key = _searchKeys[CollectionSize / 2];
|
||||
_immutableDict.TryGetValue(key, out var value);
|
||||
return value;
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public string ImmutableSortedDict_Lookup()
|
||||
{
|
||||
var key = _searchKeys[CollectionSize / 2];
|
||||
_immutableSortedDict.TryGetValue(key, out var value);
|
||||
return value;
|
||||
}
|
||||
|
||||
// 3. Add the benchmark case
|
||||
[Benchmark]
|
||||
public string PersistentMap_Lookup()
|
||||
{
|
||||
var key = _searchKeys[CollectionSize / 2];
|
||||
// Adjust API call if your map uses a different method (e.g. Find, Get, indexer)
|
||||
_persistentMap.TryGetValue(key, out var value);
|
||||
return value;
|
||||
}
|
||||
|
||||
private string GenerateRandomString(Random rng, int length)
|
||||
{
|
||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
var buffer = new char[length];
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
buffer[i] = chars[rng.Next(chars.Length)];
|
||||
}
|
||||
return new string(buffer);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue