[Proposal] Pass instance in delegate to lambda to avoid allocation by closure capture #8914
Replies: 2 comments 1 reply
-
I don't think we need any new syntax to optimize the lambda. The compiler can just emit more optimized code if the lambda only captures |
Beta Was this translation helpful? Give feedback.
-
The assumption is incorrect. Both versions are using an instance method on The compiler already optimizes delegate creation with best effort. It's an implementation detail and no new syntax is required. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hooking a lambda to an event often creates a closure that purely contains "this", while "this" is already contained in the delegate, which makes it two allocations while there practially only needs to be one.
Example of the issue
Two variants of hooking a method to an event.
Version 1:
Version 2:
Idea for a solution
Allow the delgate to pass it's instance to the lamba.
Beta Was this translation helpful? Give feedback.
All reactions