Describe the feature
Consider basic, well-known C# performance optimizations across the codebase to improve performance.
Proposed Solution
Possible optimizations (non-exhaustive):
-
Memory and allocations
-
Async and concurrency
-
Collections
-
FFI/interop
-
General
Acknowledgements
Related
⚪ None
Additional Information
Describe the feature
Consider basic, well-known C# performance optimizations across the codebase to improve performance.
Proposed Solution
Possible optimizations (non-exhaustive):
Memory and allocations
Span<T>/ReadOnlySpan<T>/Memory<T>(see feat(performance): AdoptSpan<T>andMemory<T>#204).stackallocinstead ofnew byte[]for small, short-lived byte buffers.ArrayPool<T>.SharedorMemoryPool<T>for larger recycled buffers.Async and concurrency
for/foreachloops.string.Create()or pooledStringBuilderinstead of string concatenation in loops.ValueTask<T>overTask<T>for methods that frequently complete synchronously.ConfigureAwait(false)to avoid unnecessary synchronization-context captures.asyncon methods that simply return an already-completed task.Collections
List<T>,Dictionary<K,V>, etc. when the count is known.FrozenDictionary<K,V>/FrozenSet<T>(.NET 8) for build-once, read-many lookup tables.Dictionary.TryGetValueinstead ofContainsKey+ indexer (single lookup vs. two).FFI/interop
[SkipLocalsInit]on hot interop methods to skip zero-initialization of locals.nint/nuintoverIntPtr/UIntPtrfor cleaner pointer arithmetic.General
staticlambdas to avoid closure allocations when no state is captured.ThrowHelperpattern — movethrowstatements into separate non-inlined methods to keep the happy path compact (consider forGuardClause).Acknowledgements
Related
⚪ None
Additional Information
Span<T>andMemory<T>#204