Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is so tricky to catch both when coding and in review. Is there a way a static analyzer could catch this? It would probably be super noisy.
I assume the best way to find these today is to look at the allocations in a trace?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They certainly jump out at you there. "What is <>__DisplayClass_52 and why is it being allocated 18,000 times?"
In other projects, every now and then I'll also load up all the relevant assemblies in ILSpy, search for DisplayClass, and just flip through all of them to see whether they make sense.
Of course, the real solution is to avoid the patterns that lead to this in the first place, e.g. don't use
Any(x => ...)
and you won't close over anything, or use APIs that allow the TState to be passed in. And if you can avoid closing over stuff, protect it by usingstatic
on the lambda so they don't then regress.