dotnet / dotnet/wpf

Reduce Heap allocations

Open
#770 3 comments 1 reaction 0 assignees View on GitHub
Design Discussion Enhancement Requested Performance
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

I have found several instances where we can eliminate heap allocations via simple changes such as iterating over an IList via for loop to avoid the boxing allocation for the enumerator or calling ToString on value types to be passed of to string formatting. On further investigation we are allocating a ton of method group to delegate conversions. From the C# specification section 6.6, describing the run-time evaluation of a method group conversion:
A new instance of the delegate type D is allocated. If there is not enough memory available to allocate the new instance, a System.OutOfMemoryException is thrown and no further steps are executed.

The section for anonymous function conversions (6.5.1) includes this:

Conversions of semantically identical anonymous functions with the same (possibly empty) set of captured outer variable instances to the same delegate types are permitted (but not required) to return the same delegate instance.
And we have lots of places where these conversions happen. If wanted I would love to optimize these problems, the code will stay the same semantically but allocate less.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.