[Mono-dev] slow mono_runtime_invoke

Miguel de Icaza miguel at ximian.com
Fri Mar 10 11:37:29 EST 2006


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) ();
}



More information about the Mono-devel-list mailing list