"Only called" delegate parameters #6287
Replies: 4 comments 8 replies
-
|
I think this is relevant: #1060 I think trying to make this work with normal delegate types would require Object Stack Allocation. |
Beta Was this translation helpful? Give feedback.
-
|
My idea was, the stackframe of struct <MANGLED>Closure
{
string prefix; // the parameters come first
string suffix; // the locals come next
}The struct OnlyCalledDelegate<T> // this is added to the BCL
{
IntPtr ClosureStructRef;
T Delegate;
}
TValue GetOrAdd(TKey key, OnlyCalledDelegate<Func<TKey, TValue>> valueFactory) { ... }And when invoking var x = dict.GetOrAdd(..., OnlyCalledDelegate.Create(key=> { ... }));
I hope what I wrote above isn't too crazy :) |
Beta Was this translation helpful? Give feedback.
-
|
What if the function is inside a ref struct so it cant be stored and you can hide the Func<E,T> by making it private and call it some other way |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, some methods which accept a delegate parameter, have an overload for passing an argument to said delegate, in order to avoid creating a closure context, like
Dictionary.GetOrAdd, etc.I came up with an idea of how to accomplish the goal - allow closures without allocations - in a way which is universal. For methods which are known to call the delegate only and do not store it anywhere, we introduce a new keyword -
onlycalled:What happens in the above case is that
GetOrAddsignature guarantees thatvalueFactoryis called only during execution ofGetOrAdd, andsuffixcan therefore be accessed directly from the stack frame ofFoo.The next question became, how to implement this, and here I'm not 100% if it's feasible or how expensive it is.
Beta Was this translation helpful? Give feedback.
All reactions