$ show Cysharp/ObservableCollections
ObservableCollections is a high-performance .NET library providing generic observable collections (ObservableList, ObservableDictionary, ObservableHashSet, ObservableQueue, ObservableStack, ObservableRingBuffer, ObservableFixedSizeRingBuffer) with synchronized views, filters, and writable views. It replaces the non-generic, allocation-heavy .NET ObservableCollection with zero-allocation event args using ref structs and ReadOnlySpan. The library is designed for MVVM architectures in WPF, Blazor, Avalonia, Unity, and WinUI, offering both read-only and writable synchronized views that can be bound via INotifyCollectionChanged. It also provides R3 reactive extensions for Rx-style event observation. The codebase is highly optimized, leveraging modern C# features like ref structs, spans, and custom data structures (e.g., RingBuffer, AlternateIndexList) for minimal allocations and maximum performance.
The LLM identified 4 files worth highlighting for reuse:
A custom list that maintains an alternate index ordering via binary search, enabling fast insertion, removal, and lookup by alternate index. Used internally by FiltableSynchronizedViewList to manage filtered view indices. Demonstrates efficient data structure design with a mutable IndexedValue struct implementing IComparable.
A high-performance circular buffer (ring buffer) with AddFirst, AddLast, RemoveFirst, RemoveLast, and span-based access via RingBufferSpan. Supports binary search and iteration. Used as the underlying storage for ObservableRingBuffer and ObservableFixedSizeRingBuffer. Showcases clean, optimized code with capacity doubling and bitwise masking.
A ref struct that efficiently clones an IEnumerable or ReadOnlySpan into an array rented from ArrayPool, avoiding multiple enumerations and reducing allocations. Used internally for range operations to safely snapshot data before mutations. Demonstrates advanced memory management patterns with proper disposal.
Provides ToNotifyCollectionChangedSlim, a lightweight synchronized view list that shares the underlying list directly without transformation or filtering. It offers the fastest possible INotifyCollectionChanged support for ObservableList, but does not support Range operations. Demonstrates a clear optimization trade-off for scenarios where no view transformation is needed.