Number of API method requests

Topics: APIs
Feb 11, 2015 at 3:32 PM
Greetings.

In this thread I would like to request several method implementations for different .NET collections and classes.

We develop a mission critical NoSQL database (STSdb project) and we need these methods to gain maximum performance. We already use the best indexing algorithms, we already have the best implementations for them - but in some places in our code we need to get a little more from the existing .NET classes.

I will group the request by the .NET type:

1 Dictionary<TKey, TValue>
-bool TryRemove<TKey, TValue>(TKey key, out TValue value)
2 SortedSet<T>
-bool TryGetValue(T key, out T value)
Since the SortedSet<T> does not work with a separate TKey/TValue model, there is no fast way to get an item. The only workaround is to use GetViewBetween():
public static bool TryGetValue<T>(this SortedSet<T> set, T key, out T value)
{
    foreach (var item in set.GetViewBetween(key, key))
    {
        value = item;
        return true;
    }

    value = default(T);
    return false;
}
This is not very slow, but it is also not very fast. A real TryGet method will prevent the creation of an internal SortedSet enumerator and the search complexity will be much lower.
-void Replace<T>(T item)
Currently there is an Add() method which works like 'insert an item if such does not exist'. But sometimes we need a replace logic - 'insert the item if such item does not exist, or replace it if it exists'. Such a logic can be implemented with the current state of functionality, but in most of the cases it will be at the cost of two SortedSet lookups:
public static void Replace<T>(this SortedSet<T> set, T item)
{
    if (set.Add(item))
        return;

    set.Remove(item);
    set.Add(item);
}
Here I would suggest an even more flexible Replace method:
bool Replace<T>(T item, Func<T, T, T> onExist)
which invokes onExist() only when such an item exists - and then the user decides which of two items to leave in the set.
-bool FindNext<T>(T key, out T value) - find the lower value >= key
-bool FindAfter<T>(T key, out T value) - find the lower value > key
-bool FindPrev<T>(T key, out T value) - find the higher value <= key
-bool FindBefore<T>(T key, out T value) - find the higher value <= key
For example, a slow workaround for FindNext() is:
public static bool FindNext<T>(this SortedSet<T> set, T key, out T value)
{
    if (set.Comparer.Compare(key, set.Max) > 0)
    {
        value = default(T);
        return false;
    }

    foreach (var item in set.GetViewBetween(key, set.Max))
    {
        value = item;
        return true;
    }

    value = default(T);
    return false;
}
-void ConstructFromSortedArray(T[] array, int index, int count)
This perhaps will be the most discussed method. There is such a method in the .NET framework but it is private. Sometimes it is very useful to be able to create a SortedSet instance from already sorted elements. Thus, the SortedSet can be created and filled with items without any comparisons (which is way faster than adding items one by one).
Someone could say that such a method is too concrete for the SortedSet abtract and it is mostly for its underlying implementation (which currently is a red-black tree). But we can see at things more generaly - as a fast way to fill an ordered collection with already ordered items. Thus, such a method has no relation with the current underlying structure used by the SortedSet
3 BitArray
-int FindNext(int index, bool value) - find index of the first item equal to the value flag, starting from the specified index forwards.
-int FindPrev(int index, bool value) - find index of the first item equal to the value flag, starting from the specified index backwards.
-void SetAll(int index, int count, bool value) - set all bits in the range to the specified value
4 Decimal
-int Digits { get ;} - indicates the number of digits after the decimal point
5 Task
-Task.WaitAny(params Task[] tasks, int index, int count)
-Task.WaitAll(params Task[] tasks, int index, int count)
WaitAny and WaitAll by part of the array, to prevent permanent copy of parts of the array.
Feb 11, 2015 at 5:12 PM
This is a forum for the compiler, not the .Net libraries (and even then, it's an obsolete forum for the compiler). I think this suggestion belongs to the CoreFX repo.
Feb 12, 2015 at 8:23 AM
I was not sure if this was the right place to post. Thank you! I will post the request on CoreFX repo.