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.
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:
// 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 { ... }
// 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) { ... }
// 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.
Enter the URL of a public GitHub repository