Ref delegates #5568
Replies: 5 comments 10 replies
-
You can try function pointer instead. |
Beta Was this translation helpful? Give feedback.
-
This should be "value delegates" discussed in csharplang repo. |
Beta Was this translation helpful? Give feedback.
-
The BCL has many places, especially in LINQ where we might want something like this. We know the delegate passed as an argument to My instinct about this is that it's going to be really prohibitive to multiply out all the new methods to ensure we benefit from this across the ecosystem. I know that a whole lot of ink has already been spilled about how we could optimize LINQ. I'm not up to speed on all of it but it's very likely that "value delegates" have been explored in some depth already. It would be good to dig that up and review what people have already considered. I would be interested to explore whether we can get this optimization without introducing a fundamentally new function type. This could happen via runtime escape analysis, or by compile-time annotations and checking, etc. I'm optimistic that this could get us to land where we want. |
Beta Was this translation helpful? Give feedback.
-
I might be falling into the "sufficiently smart compiler" trap here, but shouldn't the runtime be able to figure out this through escape analysis? |
Beta Was this translation helpful? Give feedback.
-
I understand the point made for IEnumerables but that is not really what I was talking about originally. Consider the following code
Because of the captured variable n, the function cannot be made static and the delegate will require a heap allocation at runtime. This can be manually solved by declaring a custom struct to carry the capture :
Notice that the delegate declaration can now be static and won't require any heap allocation at runtime. Edit : in this example the struct isn't required per see, but sometimes there are several captured variables and it's simply easier to pack them into a single struct. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The software I'm working on uses a fair amount of delegates (including Action, Func, lambdas and so on).
Some of them can point to static functions and allow us to save on heap allocations, but many of them are short-lived and use closures, requiring heap allocations at runtime. Short-lived meaning they only live for the duration of the function they're declared in (e.g. Parallel.ForEach with a lambda body).
The idea/question therefore is to wonder if the concept of ref delegates would make sense, in the same spirit as using ref structs. i.e. they would only live on the stack and would prevent any operation that would put them on the heap. With the goal of saving on allocations whenever possible.
I don't know enough about how delegates are handled under the hood to assess that idea, would that make any sense in c# ?
Edit : maybe that question should be in charplang ?
To be clear, we can avoid allocation already, but that requires making the called function static, and updating its signature to take a custom struct used to carry the captured variables. This works and is allocation free, but that's a lot of work and is not a viable long term solution. Maybe the compiler could do this automatically under the hood ?
Beta Was this translation helpful? Give feedback.
All reactions