Performance questions from a newbie

May 23, 2009 at 1:10 PM
Edited May 23, 2009 at 1:51 PM

EDIT:  When I wrote this post, I assumed that it is possible to load and invoke dynamic language code from within precompiled C#/VB code.  I don't see any examples of projects which do something this in the SDK samples, so now I'm wondering whether that was a false assumption, in which case this entire post is moot. (I thought that was what DLR Console was doing.  Whoops).


I'm working on a Silverlight game engine where a key feature is supporting user-created content and logic.  This means, of course, that I need to support a dynamic language.  I don't know much of anything about the DLR or the SLDL SDK, and at the moment I'm curious whether what I'd like to do is within the realm of possibility using the DLR or by any other means.

I would like to have most or all of my game objects using dynamic language scripts to dictate their logic. Obviously, my foremost concern is performance.  I understand this is probably highly variable, but broadly speaking, how much slower can one expect an IronPython/Ruby function to perform compared to its precompiled C# equivalent?  For each object, I might need to invoke a simple behavior-determining function up to 60 times per second.  Is there a performance overhead each time a DL script is invoked?  Are scripts executed synchronously or asynchronously?  And are there any other factors which would affect performance that I would need to be concerned about, such as the cache size of the JIT compiler?

Another question is whether it would be possible to save and load precompiled scripts, but I believe this is being discussed in another thread.

Thanks for any help, and I apologize for my obliviousness.

May 26, 2009 at 4:23 PM

This if very possible, as one of the DLR's main use case is to embed scripts in a codebase not dependent on scripting in the past. As far as performance, the first time a method is called it will be noticeably slower than a C# equivalent, but subsequent calls with the same parameter types will be very close to an equivalent C# method call. If you're script is generally doing the same thing over and over again, it should be fast, but that's a sweeping generalization as I don't know exactly what code is being run. =) IronPython is generally faster than IronRuby at the moment, but that's because of the language and not the implementation; both have comparable throughput speed compared to their native implementations.

Scripts are executed synchronously; to get asynchronous behavior you'll have to run them in a separate thread.

IronPython and IronRuby will interpret the first couple method calls, but after that it will JIT that call on another thread and start running the compiled code when compiling is done.

Pre-compiling scripts can happen at many layers: ScriptSource.Compile will pre-generate the DLR trees for that ScriptSource, which can be stored via serialization. Generating trees isn't a very expensive operation, but it will save you some time. Pre-compiling to actual IL and then loading that is untested, though Michael Foord was discussing this on the IronPython mailing list.

May 27, 2009 at 9:32 PM

Jimmy, thank you for your very informative response.  I noticed a lot of helpful material on your blog, so thanks for that too!  It looks like the DLR provides just what I need.  I will look into the pre-compilation stuff, as this would be extremely useful to me.  Short of that, I wonder if there is a way to force JIT compilation on the first call of a method.

I must say that being able to do this stuff in Silverlight is quite amazing.  Thanks for making it possible.

May 27, 2009 at 9:49 PM

You’re welcome! And yes, you can force compilation by setting the NoAdaptiveCompilation option in ScriptRuntimeSetup.Options.