Boost Your .NET Projects: Unleashing the Power of Spargine’s FastSortedList

Spargine is a set of open-source assemblies and NuGet packages for .NET 8, developed and maintained by me since the release of .NET 2. These assemblies are integral to my projects and are currently in production at my company. You can access the source code and NuGet packages through the following links:

Over a decade ago, when I began focusing on code performance, I noticed inefficiencies in creating sorted collections. While working on a contract, I encountered a scenario where thousands of items were being added to a SortedList. Observing its behavior, I realized that the collection appeared to sort itself every time an item was added, significantly impacting performance.

To mitigate this issue, I experimented with alternative approaches and discovered that using an unsorted collection and sorting it only when necessary was far more efficient. This insight led me to develop a specialized type in Spargine called FastSortedList. Unlike traditional sorted collections, FastSortedList defers sorting until explicitly required, optimizing performance while maintaining order when needed. By reducing unnecessary sorting operations, FastSortedList enhances efficiency, particularly in scenarios involving large data sets with frequent insertions. This approach has proven invaluable for improving application performance without sacrificing the benefits of sorted data structures.

FastSortedList

FastSortedList is extremely easy to use. Here is how to create it and add items.

var collection = new FastSortedList<Person>();

foreach (var person in people)
{
    collection.Add(person.Value);
}

Constructors

Below are the constructors for FastSortedList.

  • FastSortedList(): Initializes a new instance of the FastSortedList class.
  • FastSortedList(IComparer<T> comparer): Initializes a new instance of the FastSortedList. This constructor allows the user to specify a custom comparer for sorting the elements in the list.
  • FastSortedList(IEnumerable<T> collection): Initializes a new instance of the FastSortedList. This constructor initializes the list with the elements from the specified collection and uses the default comparer.
  • FastSortedList(IEnumerable<T> collection, IComparer<T> comparer): Initializes a new instance of the FastSortedList. This constructor initializes the list with the elements from the specified collection and uses the specified comparer for sorting.
  • FastSortedList(int capacity): Initializes a new instance of the FastSortedList that is empty and has the specified initial capacity.
  • FastSortedList(int capacity, IComparer<T> comparer): Initializes a new instance of FastSortedList that is empty and has the specified initial capacity. This constructor initializes the list with the specified initial capacity and uses the specified comparer for sorting.

Methods

Below are the methods for FastSortedList and their description.

  • Add(): Adds an object to the end of the list.
  • AddRange(): Adds the items to the end of the list.
  • Clear(): Removes all items.
  • GetEnumerator(): Returns an enumerator that iterates through the items. This is when the items are sorted.
  • Remove(): Removes the first occurrence of a specific object.
  • RemoveAt(): Removes the element at the specified index of the list.
  • ToArray(): Converts the items to an array.
  • ToImmutableList(): Converts the items to an ImmutableList.
  • ToList(): Converts the items to a List.

Benchmark Results

For this benchmark test, I created a new FastSortedList and SortedList and filled them with items via the constructor. For the FastSortedList, I also called Sort() after the collection was created. As you can see from these results, FastSortedList is 4.61 times faster than SortedList and allocates about half of the memory used to make the collection.

Summary

I am confident that FastSortedList in Spargine will greatly benefit your projects by improving performance and reliability. Detailed benchmark results are available on GitHub. The success of open-source projects like Spargine depends significantly on community involvement. If you find these updates useful or have ideas for further improvements, I encourage you to contribute. Whether by submitting a pull request, reporting issues, or suggesting new features, your input is invaluable.

Together, we can continue to make Spargine a powerful and essential tool for the .NET community. Your feedback and suggestions are highly appreciated, so please share them in the comments section.

If you are interested in contributing to this project or have any questions, feel free to contact me via email at dotnetdave@live.com. Your support and collaboration are greatly appreciated!

Thank you for your support, and happy coding!

Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

If you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave

© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.


Discover more from dotNetTips.com

Subscribe to get the latest posts sent to your email.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.