Optimizing C# collections with predictive sizing
Dynamic collections, such as the dynamic array (also known as a List in C#), are without a doubt one of the most common data structure, and not only in C# but in computer science in general. The dotnet base class library exposes plenty of dynamic collections to fulfil a wide number of usecases: (System.Collections.)List<T>, Dictionary<K, V>, HashSet<T>, Queue<T>, Stack<T>, and more. But they all share one thing in common: they are based on dynamic reallocation of backing arrays, which can lead to suboptimal performance and excessive memory allocation....