C# DynamicMethod prelink

Posted by soywiz on Stack Overflow See other posts from Stack Overflow or by soywiz
Published on 2012-12-01T01:16:08Z Indexed on 2012/12/01 5:04 UTC
Read the original article Hit count: 267

Filed under:
|
|
|

I'm the author of a psp emulator made in C#.

I'm generating lots of "DynamicMethod" using ILGenerator. I'm converting assembly code into an AST, and then generating IL code and building that DynamicMethod. I'm doing this in another thread, so I can generate new methods while the program is executing others so it can run smoothly.

My problem is that the native code generation is lazy, so the machine code is generated when the function is called, not when the IL is generated. So it generates in the program executing thread, native code generation is prettly slow as it is the asm->ast->il step.

I have tried the Marshal.Prelink method that it is suposed to generate the machine code before executing the function. It does work on Mono, but it doesn't work on MS .NET.

Marshal.Prelink(MethodInfo);

Is there a way of prelinking a DynamicMethod on MS .NET?

I thought adding a boolean parameter to the function that if set, exits the function immediately so no code is actually executed. I could "prelink" that way, but I think that's a nasty solution I want to avoid.

Any idea?

© Stack Overflow or respective owner

Related posts about c#

Related posts about Performance