Back to all reviewers

Cache expensive computations

dotnet/runtime
Based on 5 comments
C#

Avoid recomputing expensive operations by caching results when they will be used multiple times. This applies to method calls, property access, and static property lookups. The performance impact of repeated calculations can be significant, especially in hot code paths.

Performance Optimization C#

Reviewer Prompt

Avoid recomputing expensive operations by caching results when they will be used multiple times. This applies to method calls, property access, and static property lookups. The performance impact of repeated calculations can be significant, especially in hot code paths.

Consider these approaches:

  1. Pass computed values as parameters: When a method needs a value that is expensive to compute, calculate it once and pass it to all methods that need it.
// Instead of this:
if (type.GetClassLayout().Kind != MetadataLayoutKind.Auto) { ... }
// And later:
return type.GetClassLayout().Kind switch { ... }

// Do this:
var layout = type.GetClassLayout();
if (layout.Kind != MetadataLayoutKind.Auto) { ... }
// And later:
return layout.Kind switch { ... }
  1. Cache static or repeated property lookups: Store values from static properties or repeated lookups in local variables.
// Instead of this:
if (IsHttp3Supported() && GlobalHttpSettings.SocketsHttpHandler.AllowHttp3 && _http3Enabled) { ... }
// And later again:
if (GlobalHttpSettings.SocketsHttpHandler.AllowHttp3) { ... }

// Do this:
bool allowHttp3 = GlobalHttpSettings.SocketsHttpHandler.AllowHttp3;
if (IsHttp3Supported() && allowHttp3 && _http3Enabled) { ... }
// And later:
if (allowHttp3) { ... }
  1. Pre-allocate collections with appropriate capacity: When creating collections that will grow, initialize them with an appropriate capacity to avoid expensive resize operations.
// Instead of this:
symbolRemapping = new Dictionary<ISymbolNode, ISymbolNode>();

// Do this:
symbolRemapping = new Dictionary<ISymbolNode, ISymbolNode>(
    (int)(1.05 * (previousSymbolRemapping?.Count ?? 0)));

Caching computed values not only improves performance but also makes code more readable by reducing duplication and making dependencies explicit.

5
Comments Analyzed
C#
Primary Language
Performance Optimization
Category

Source Discussions