Proposal: ref interface to avoid boxing #6142
Replies: 26 comments 2 replies
-
How would this work? |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h |
Beta Was this translation helpful? Give feedback.
-
@ygc369 I do not think so, for if you had, I would not have needed to ask the question. |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h |
Beta Was this translation helpful? Give feedback.
-
@ygc369 Well, all of it really.
What is a
How so? How does this work at all?
How do you pass a struct by ref from the call-site but deal with it as an interface at the use-site? Is this something the CLR can already do, or would this require CLR changes?
Language limitation, or CLR limitation?
This has been raised many times before with no good results - what happens if it needs to be lifted to a class member for an |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h
Language limitation.
Have you seen ref struct in C# 7.2? It simply forbids these cases. So this idea should do the same. |
Beta Was this translation helpful? Give feedback.
-
You can go ahead and say have You can not avoid boxing of struct when its cast to object. Objects have references, value types dont have that so when you cast it needs to be boxed so you can have reference to it |
Beta Was this translation helpful? Give feedback.
-
@MkazemAkhgary |
Beta Was this translation helpful? Give feedback.
-
I understand that you are trying to keep things on stack, which is some times a good thing. note that some times trying to keep things on stack leads to excessive complexity and if you just let them go on heap your code becomes simpler, easier to understand and even faster to run. I'm not telling that what you are trying to do is good or not, I just wanted to note that, its your decision which way to go. Having all of that said, imagine we had such thing as btw I don't think its that simple. probably there are lots of concerns, or maybe blockades on this way. edit: maybe |
Beta Was this translation helpful? Give feedback.
-
Do you know how to invoke virtual method? This needs the VTable (virtual method table). An instance of struct doesn't have the VTable pointer. This is why boxing is needed. |
Beta Was this translation helpful? Give feedback.
-
Could achieve similar with what exists today if you could define a public constraint ISomeInterface { ...... }
public struct A : ISomeInterface { ...... }
static void test<T>(ref T a) where T : ISomeInterface
{
......
}
static void main()
{
A a0 = new A();
test(ref a0);
} |
Beta Was this translation helpful? Give feedback.
-
e.g. you can currently do this to to avoid any boxing public interface ISomeInterface { ...... }
public struct A : ISomeInterface { ...... }
static void test<T>(ref T a) where T : ISomeInterface
{
......
}
static void main()
{
A a0 = new A();
test(ref a0);
} But I assume you want to eliminate the possibility that the interface could be used for boxing? |
Beta Was this translation helpful? Give feedback.
-
Yes. |
Beta Was this translation helpful? Give feedback.
-
So you don't wanna a new mechanism, just a new constraint to compiler enforce? |
Beta Was this translation helpful? Give feedback.
-
I think you only would need a way to indicate the interface was only for constraints; not for parameters. That would be more flexible as it could still work for classes. Then if you additionally wanted to force stack-only you could additionally use the static void test<T>(ref T a) where T : struct, ISomeInterface |
Beta Was this translation helpful? Give feedback.
-
Sounds like a shape to me. |
Beta Was this translation helpful? Give feedback.
-
I hope the "ref interface" could behave like this: when it is used as a parameter, it will pass struct by ref without boxing, and pass class just like before (not by ref, the class reference can't be changed after function returns). |
Beta Was this translation helpful? Give feedback.
-
For example: public ref interface ISomeInterface { ...... }
public struct A: ISomeInterface { ...... }
public class B: ISomeInterface { ...... }
static void foo(params ISomeInterface args[])
{ ...... }
var a = new A();
var b = new B();
foo(a, b); //a is struct, pass by ref, but b is class, need not pass by ref |
Beta Was this translation helpful? Give feedback.
-
This is still valid for method return type: struct SomeStruct : ISomeInterface
{
}
ISomeInterface GetSome()
{
return SomeStruct();
} this will perform boxing of value type, while marking interface as proposed could prevent that |
Beta Was this translation helpful? Give feedback.
-
How about his proposal going on, is it closed? It actually useful is someone want to reduce GCAlloc. And more the 'delegate' has the same issue. For example, the Array.Sort or anything need a predicator, and the predicator capture some local variables, there is no way to avoid boxing. This issue just change the 'delegate' to 'interface'. |
Beta Was this translation helpful? Give feedback.
-
Yes. As you can see, @ygc369 closed this a couple of years ago. |
Beta Was this translation helpful? Give feedback.
-
Can this be reopened, or would it require creating another issue? @aviadmini provides a good use case for such a feature. |
Beta Was this translation helpful? Give feedback.
-
Please create a new discussion. |
Beta Was this translation helpful? Give feedback.
-
As some people requires, I will reopen this issue. |
Beta Was this translation helpful? Give feedback.
-
Any news on this so far? |
Beta Was this translation helpful? Give feedback.
-
Related: #7608 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
C# should allow to declare ref interface, structs that inherit ref interface don't get boxed when used as the interface. Instead, pass by ref.
Only struct can inherit ref interface, class can't.
Ref interface can't be class member, can't be stored on the heap. (stack-only)
For example:
Beta Was this translation helpful? Give feedback.
All reactions