[Mono-dev] slow mono_runtime_invoke

Thomas Grill gr at grrrr.org
Fri Mar 10 12:05:11 EST 2006


Hi Miguel,
thank you, that's very helpful for my general understanding.
Now, there's a complication. In your example, the delegate is bound to a 
specific instance of the MyMethod class. Is there a fast way to call a 
specific class method on an instance of the class just when i want to 
call it?
In my application, i use a number of callbacks which are class, not 
instance specific. It would be great to save the memory needed to keep 
the delegates for each instance.

greetings,
Thomas


Miguel de Icaza schrieb:
> Hello,
>
>   
>> i'd like to use mono inside a realtime system, which means that i need 
>> to make the native->managed transition as cheap as possible.
>> Since mono_runtime_invoke is very slow, i'd like to ask whether there 
>> are strategies to speed this up.
>>     
>
> Do not use mono_runtime_invoke, which is dynamic.
>
> Instead define a delegate in C#, assign a value to it (to point to the
> method you want to call), then P/Invoke into unmanaged land to register
> the delegate, and use that in unmanaged land to call back.
>
> Something like:
>
> class Helper {
> 	delegate void Callback (void);
>
> 	[DllImport ("binding")]
> 	extern static void RegisterCallback (Callback cb);
> }
>
> class MyMethod {
> 	Callback cb;
>
> 	MyMethod ()
> 	{
> 		cb = new Callback (my_callback);
> 		Helper.RegisterCallback (cb);
> 	}
>
> 	void my_callback ()
> 	{
> 	}
> }
>
> On the C side, you use:
>
> typedef void (*Callback)(void);
> Callback the_cb;
>
> void RegisterCallback (Callback cb)
> {
> 	the_cb = cb;
> }
>
> Whenever you need to call into "my_callback" in managed land, you just
> call "the_cb" from C:
>
> void Do ()
> {
> 	(*the_cb) ();
> }
>
>
>   

-- 
Thomas Grill
http://grrrr.org





More information about the Mono-devel-list mailing list