Proposal: Function pointers on instance methods #7331
Unanswered
hez2010
asked this question in
Language Ideas
Replies: 2 comments 1 reply
-
I'm curious if any thought has been given to creating struct delegate type system? We can also get rid of the (frankly useless) multi-cast feature, and with implicit convertibility to the existing class delegate types, it can be a viable transition and a decent performance gain across the board (not just unsafe). |
Beta Was this translation helpful? Give feedback.
0 replies
-
How does that syntax var c = new C();
delegate*<this object, void> f = &c.Foo;
f(c); That would use the |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal
We already have function pointers on static methods, I'm proposing adding support for instance methods as well.
Motivation
Constructing a delegate object is expensive, we already have function pointers on static methods so we can just use this feature in unsafe code to bypass constructing delegates.
We do can use reflection to get the function pointer for an instance method as well, but the same using reflection to get a method is extremely expensive.
So why not leverage function pointers for instance methods as well?
Syntax
Similar to function pointers we have today, we use
this
to represent a function pointer on an instance method, this is for distinguishing betweenstatic void Foo(object)
andvoid Foo()
:Emit strategy
Actually, CoreCLR already supports
calli
on instance methods, so here for the above code, the emitted code can be:You can run and verify the code here: https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AbEBLDaAuIUArgHYA+AAkgAQD6NA3jQL4CwAUJwHRgYCGAZ0E0Awp0aca0mtwC2MfAAsIAExrZSg/P1JgYNAG4Rs6gGIQIACgCUUmZI4znNDKu1QaAIgBmlr/YuNJQAjAAMHpoA5nZOQTSw+IGsnMnyiirq2vz42GBGJuoAsvyatjTJjvGyGBBg/Bgimtj4NFbJ1QDaYQC6NBDAAFYwYPhoHfGdIX2kOdiGBppJcUGx1ZQoUhPSpDAA7gODGlo6egbGpmIgILz40LbbNNq1YNxhj49uPvikx9lnBUuomuFmsa3izzq3BCHxWLjcLzen1UiJhcOc9QwGGwf1O+kBqge6JkiWS7DhaQUyjUNAADsRgNj8pp/viLupbvdwdIqvE3PwoFEkcTpJiMLjdGzCjROgBlACe2hgcm4ACUyLkFD0FUqVQB5IYjQg3UZcx6kuHk8lAA=
Another example that demonstrates a function pointer on an instance method with a single parameter:
https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AbEBLDaAuIUArgHYA+AAkgAQD6NA3jQL4CwAUJwHRgYCGAZ0E0Awp0aca0mtwC2MfAAsIAExrZSg/P1JgYNAG4Rs6gGIQIACk34AzACYAlFJmSOMzzQyr+UAObcAIyuXjSUQQAMto4uHmE0sPihrJwp8ooq6tr8+NhgRibqALL8mlZONCnuCbIYEGD8GCKa2Pg0Vim1ANqRALo0EMAAVjBg+GhdCd1BA6S52IYGtlPScbWUKFKrNKQwAO5DwxpaOnoGxqZiICC8+NAVO9r1YNyROzs+AGb4pCc550KV1ENws1hizie+BewQ+8TCPhh73hXh8r2wKG4IhQDk+qhhIRRnkaGAw2H+Z30QNUNlI9khRJkSRS7C4KIyyjUNAADsRgGSCpoAVTLuo7g91m48X5AsjaiSMBTdCKijRugBlACe2hgcm4ACUyHkFH0tTq9QB5EZjQi3cYSnbMlGs1lAA==
Alternatives
Instead of using
delegate*<this object, void>
, we can use a calling convention to specify the function pointer is for an instance method, such asdelegate*[ThisCall]<object, void>
.Beta Was this translation helpful? Give feedback.
All reactions