Dynamic Compiling

[… several weeks later… ]

I really love Janino! ;D

Toying around is really really fast now. It compiles in an instant and well… I don’t have to restart the vm at all. Doing some changes… save… switch to my brainless shell… press a button… and one second later the new version is up and running (and that with a 500mhz machine with only 128mb ram).

It’s really amazing.

On the downside… new 1.5 syntax isn’t supported yet, the error messages are somewhat odd (just look at the line number and check it for yourself) and you need to adjust your framework a bit, but in return you avoid reloading media :wink: [At the end you can of course compile the usual way… so there is no need to distribute the lib if you just (ab)use it as dev tool]

Oh and if you find a bug and report it it gets usually fixed within 1-2 weeks. That’s a big plus imo :slight_smile:

Btw the easiest way for preventing getting a cached copy of a class (=not the newest) is using a freshly created JavaSourceClassLoader for loading the class. There is a cleaner way, but it’s not worth the trouble (given the rather frequent update rate of janino).

Well, you can’t use that approach for everything, but it’s great for prototyping or things like getting the collision detection/response just right.

thx onyx for this review. I have been a bit hasty in doing this in my game, but ive been given some confidence.

One final question. And probably only you can answer this oNyx. Is using Janino to dynamically compile a “script” and running it as a normal class a better option than using say…Groovy or BeanShell?

DP

I’m afraid I can’t answer that question in general, because it depends on the project if it’s “better” or not.

BeanShell is like usual java but slower (interpreted), but there is no compile time. Groovy is also interpreted, no compile time too, but you won’t be able to “convert” it to a class at the end.

With Janino it’s a bit different. You have compile time there, but it’s damn fast (the vm is still running therefore there is no need to start the vm [like javac does]). For example something like hello world compiles in 10msec (whereas javac takes several seconds[slow 500mhz mind you]). You can also customize the class loading a bit… eg if there is a class you load it if not you compile it on-the-fly with janino or you could use a properties file containing those classes which should be dynamically recompiled.

So basically you can keep the compile times of janino pretty low (<1sec), you get the full java-speed and you won’t need to ship the lib (and your source) at the end. Those are the reasons why I like it and those are also the reasons why I didn’t tried the other options. I want full/actual speed. I want to see/know if I do something horrible wrong (=slow).

Well, having said all that… janino has still several problems. It’s for example unable to compile TinyRivers (bombs out with: Operand stack underrun at offset 312…net.janino.CodeContext.flowAnalysis… yadda yadda). However, it seems to be good enough for smaller things (or a bunch of smaller things). It’s also good that you can replace it with “com.sun.tools.javac.Main.compile” if you want to (if you’ve only used the source class loader).

Janino can be also used for smaller things like expression evulator or as a script evulator (but the you’ll have to ship the lib).

I suggest giving it a try. It’s a pretty interesting piece of tech, which can speed up development (under specific circumstances) a lot.