Skip to content

feat(perf): basic C# performance optimizations #344

Description

@currantw

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

    • Avoid LINQ (allocates enumerators and closures); prefer plain for / foreach loops.
    • Use string.Create() or pooled StringBuilder instead of string concatenation in loops.
    • Prefer ValueTask<T> over Task<T> for methods that frequently complete synchronously.
    • Use ConfigureAwait(false) to avoid unnecessary synchronization-context captures.
    • Avoid async on methods that simply return an already-completed task.
  • Collections

    • Pre-size List<T>, Dictionary<K,V>, etc. when the count is known.
    • Use FrozenDictionary<K,V> / FrozenSet<T> (.NET 8) for build-once, read-many lookup tables.
    • Use Dictionary.TryGetValue instead of ContainsKey + indexer (single lookup vs. two).
  • FFI/interop

    • Use [SkipLocalsInit] on hot interop methods to skip zero-initialization of locals.
    • Prefer nint / nuint over IntPtr / UIntPtr for cleaner pointer arithmetic.
  • General

    • Seal classes not designed for inheritance so the JIT can devirtualize calls.
    • Use static lambdas to avoid closure allocations when no state is captured.
    • Use the ThrowHelper pattern — move throw statements into separate non-inlined methods to keep the happy path compact (consider for GuardClause).

Acknowledgements

  • I may be able to implement this feature request
  • This feature might cause a breaking change

Related

⚪ None

Additional Information

Metadata

Metadata

Assignees

Labels

coreCore library (`sources/Valkey.Glide/`)performanceImproves performance

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions