Repository navigation
Closed
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Collections
Description
Background and motivation
In #27229 APIs that allow lookups by "alternate" key which behavior is described by interface IAlternateEqualityComparer<in TAlternate, T> where TAlternate : allow ref struct were approved. This allows allocation-free lookups using ref structs such as ROS<T>! However, this API was only approved for the hash-based Dictionary/Set types that take an IEqualityComparer and as such the API is incompatible with the sorted Dictionary/Set types such as SortedSet<T> and SortedDictionary<T> which take an IComparer instead.
To allow allocation-free lookups on SortedSet<T> and SortedDictionary<T>, I propose adding IAlternateComparer<in TAlternate, T> interface and set of alternate lookup APIs that enable this.
API Proposal
namespace System.Collections.Generic
{
public interface IAlternateComparer<in TAlternate, T> where TAlternate : allow ref struct
{
int Compare(TAlternate alternate, T other);
T Create(TAlternate alternate);
}
public static partial class CollectionExtensions
{
public static SortedDictionary<TKey, TValue>.AlternateLookup<TAlternateKey> GetAlternateLookup<TKey, TValue, TAlternateKey>(
this SortedDictionary<TKey, TValue> dictionary)
where TKey : notnull where TAlternateKey : allow ref struct;
public static bool TryGetAlternateLookup<TKey, TValue, TAlternateKey>(
this SortedDictionary<TKey, TValue> dictionary,
out SortedDictionary<TKey, TValue>.AlternateLookup<TAlternateKey> lookup)
where TKey : notnull where TAlternateKey : allow ref struct;
public static SortedSet<T>.AlternateLookup<TAlternate> GetAlternateLookup<TAlternate>(
this SortedSet<T> set)
where TAlternate : allow ref struct;
public static bool TryGetAlternateLookup(
this SortedSet<T> set, out SortedSet<T>.AlternateLookup<TAlternate> lookup)
where TAlternate : allow ref struct;
}
public class SortedDictionary<TKey, TValue>
{
public readonly struct AlternateLookup<TAlternateKey> where TAlternateKey : allow ref struct
{
public bool ContainsKey(TAlternateKey key);
public bool TryAdd(TAlternateKey key, TValue value);
public bool TryGetValue(TAlternateKey key, [MaybeNullWhen(false)] out TValue value);
public bool TryGetValue<TKey, TValue, TAlternateKey>(TAlternateKey key, [NotNullWhen(true)] out TKey? actualKey, [MaybeNullWhen(false)] out TValue value);
public bool Remove(TAlternateKey key);
public bool Remove(TAlternateKey key, [MaybeNullWhen(false)] out TValue value));
}
}
public class SortedSet<T>
{
public readonly struct AlternateLookup<TAlternate>
{
public bool Add(TAlternate item);
public bool Contains(TAlternate item);
public bool Remove(TAlternate item);
public bool TryGetValue(TAlternate equalValue, [MaybeNullWhen(false)] out T actualValue);
public virtual SortedSet<T> GetViewBetween(TAlternate lowerValue, TAlternate upperValue);
}
}
}
namespace System.Runtime.InteropServices
{
public static class CollectionsMarshal
{
public static ref TValue? GetValueRefOrAddDefault<TKey, TValue, TAlternateKey>(
SortedDictionary<TKey, TValue>.AlternateLookup<TAlternateKey> dictionary,
TAlternateKey key, out bool exists)
where TKey : notnull where TAlternateKey : allow ref struct;
public static ref TValue GetValueRefOrNullRef<TKey, TValue, TAlternateKey>(
SortedDictionary<TKey, TValue>.AlternateLookup<TAlternateKey> dictionary,
TAlternateKey key)
where TKey : notnull where TAlternateKey : allow ref struct;
}
}API Usage
SortedSet<string>.AlternateLookup<ReadOnlySpan<char>> d = ...;
ReadOnlySpan<char> slice = ...;
d.TryAdd(slice, value);Alternative Designs
No response
Risks
No response
Reacted by Badrish Chandramouli
Metadata
Metadata
Assignees
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Collections
Activity
eiriktsarpalis commentedon May 9, 2024
What's the use case? These collections are slower when compared to hash table based implementations (both in terms of asymptotic complexity and the allocations incurred by populating the search tree), so any application requiring ROS based lookups would surely be performance sensitive enough to make use of
Sorted*collections prohibitive.PaulusParssinen commentedon May 9, 2024
https://redis.io/glossary/redis-sorted-sets/ I guess.
But we're giving up on BCL's implementation of sorted set in Garnet anyways and are looking to implement more specialized data structure for our needs anyways.
So it was worth a try.
eiriktsarpalis commentedon May 9, 2024
That doesn't describe an in-memory collection though.
PaulusParssinen commentedon May 9, 2024
Oh right.. I guess there is no ask for this then so I'll close it 👍