Selecting a scripting engine – Part 4.

This is the fourth installment in the series (here are 1, 2, and 3) and this time it was the turn of Javascript. Yes I know a lot of buzz has been going on about the sudden jump in speed of the scripting language, thanks to the introduction of the blazingly fast V8 and, yes an even faster SquirrelFish Extreme (SFX) engines (oh and not to forget Mozilla’s Tracemonkey). Javascript has been at the back of my mind and I have been fiddling around with Javascript for some time now. I had actually tried out Javascript as a scripting language earlier for a completely different project altogether, but that prototype was left on the back-burner since there were more pressing issues to solve. Then, of course, I left the project and the experiment didn’t get beyond the prototyping phase. Too bad really. That was the time when Javascript was pretty slow, or should I say slower than it is today. However the story with the new generation Javascript engines is very different from the old ones. Most engines compile Javascript code to “fast native code” on the platform they run on, and that’s the reason for the sudden spike in speed. According to the SFX blog, SFX goes a step further and does something the call as “polymorphic inline cache” (explained on the link provided above) which they claim to be a exciting new optimization. I haven’t taken SFX for a ride yet, but I have had a peek at V8 and I must say I am more than impressed by Google’s implementation.

If you haven’t already guessed by now, my main interest in any scripting language is obviously an integration with our Game engine (, code named O2 Engine). From my previous posts it’s pretty clear; I am on a quest for an elusive perfect solution. A please all, perfect scripting language (, which I know I will not find). Unfortunately ideal solutions are impossible and most of the times it’s a compromise between what is available and/or what you can afford with the resources you have. With Javascript things are no different. All kinds of remarks have been made about the scripting language. Everything from, “Javascript is the worst language ever!”,  to “Javascript: The world’s most misunderstood language.” Google around and you will see what I mean. But there are two sides to every story. While most programmers who work with stricter languages frown upon Javascript, the fact remains, Javascript is easy to understand than most other languages. It is used by programmers and non-programmers alike and is used by countless web-sites to deliver Internet content.

Javascript’s shares it’s syntax to some extent with C and Java, but the similarities end there. It is not a very “strict” language and this is in fact by design and is actually intentional. Less strictness means non-programmers can adapt to the language more easily. Javascript is probably the least strict language among most other languages I have used. This can be good and bad, but while considering a scripting language, I can probably live with that given the rapid prototyping capabilities it offers. If you look at the language itself, Javascript shares most of the common properties found in other scripting languages (Lua, Python and others). The interesting thing about Javascript is the function-object duality the language proposes. This can be confusing for a programmer working on a language that uses an imperative approach (something like C++ or Java), maybe not so if you started out with Javascript or already know functional programming. However the thing that is really interesting is the fact that functions are first class citizens under Javascript. This concept itself is not something that is unique to Javascript, Lua has similar concept too and both languages can be used for functional (style) programming. Inheritance under Javascript is prototype based which is again similar to how Lua implements inheritance, but is different from the classical inheritance that defines object-oriented languages like C++ and Java. I don’t see any particular problem with that, given that there is a way to export class hierarchy.

Integrating or embedding Javascript with an application is no different than what it is for most other scripting languages. I found Google’s V8 code to be the easiest and cleanest to understand and integrate among all other languages/embedding libraries I have encountered. However, the entire process can be extremely repetitive and time consuming if a large library/classes/functions have to exported. Again, this is no different for most other scripting languages (except maybe Python, where you have an automatic process). I haven’t had time to look at SFX and TraceMonkey yet, but I am sure the process should be similar.

So what is special about Javascript? If you consider the language and the embedding platform/technique, nothing actually. Head-to-head it’s probably equivalent to Lua or any other embeddable scripting language out there. However, what sets Javascript apart from the rest of the pack today is actually mentioned in the first paragraph of this article. Yes,  it’s the new generation of scripting engines from Mozilla, Google and the Webkit team. They give a huge advantage to Javascript in terms of speed and performance. JIT compilation to native code and the optimizations that have been introduced, do result in significant increase in speed of the script code that is being executed.

I know most game developers (incuding myself) are performance junkies, but hold on there before you jump in and start integrating Javascript into your favourite game engine. There are some points here worth noting. For native code compilation to work, the Javascript compiler needs an assembler to convert the compiled code to native code instructions. So on platforms for which the Javascript compiler has no assembler, you are basically out of luck. V8 has assemblers for Intel and ARM as of now. So for any other architecture the V8 engine will be of no help, unless that is, you implement a version of the in-built assembler for that platform yourself. I am not too sure how this will scale to platforms like the PS3 and XBox and could very well be a sticking point. The other thing that caught my attention is that JIT compilation to native code means Javascript code has be distributed as plain text files. Such code could be subject to manipulation and a potential cause for concern if your code has to adhere to security specifications.

That said, given the fact that so much RnD is going into Javascript, makes the language a top contender for the post of a scripting engine.

Doofus 3D hits Code-Freeze.

Whew! The game has officially (finally) hit the last code-freeze today, actually yesterday but I tagged the repository today so it’s officially code freeze today. All the levels have been done and most of the internal (alpha) testing is complete. Yeah, the blog has been silent for a while; but that’s to be understood, I have been really hard pressed to finish this on time, yet I overshot my (mostly self-imposed) deadline by a good 15 days In part due to unforseen circumstances, in part due to my own faults, but that’s the way it goes (generally 😀 ). I am really glad it’s finally done and all but the most trivial issues remain. There is some artwork left though, I still have to finish up the initial screens and some sound-tracks and some sound-effects need to be incoroprated into the game as well. Some parts of the demo version also need to be finished up. However, I am going to hold on to that till I release the RC for one final bout of testing, that way I have a good feedback on which levels to include in the demo and which to hold off for the full version.

 

 

The world survives!

I had a dream last night where I wake up and peer out of my bedroom window to look at what usually is a huge tree, only this time in the dream I look out and see nothing! Yeah pitch blackness, and I wonder, “Oh so the LHC must have produced one after all.” The next though that enters my deranged nightmarish mind is “Have I crossed the event-horizon yet? Then maybe I still have a chance to run away, maybe someone has a spaceship and I might just get a lift to alpha centauri“, and that’s when I wake up to find the sun shining down my face. I guess the trip to alpha centauri will have to wait. So the world hasn’t ended yet albeit fears of earth gobbling black holes, strangelets, vaccum bubbles, magnetic monopoles or as someone on gamedev said, “We haven’t yet been invaded by head-crab hordes from Zen.” So I guess it’s time to congratulate the scientists working on the LHC, but I guess they took every bit of precaution and as someone said, “Even inviting Gordon Freeman just in case!” 

Gordon Freeman spotted at cern.

Hmm… where’s the hazmat suit dude?

It’s New, it’s Chrome, it’s Fast.

I know I have written too much about browsers on this blog, but I could not resist writing about the newest member of the Google family, aka the new browser Chrome. OK, so the browser is pretty fast, no kidding, I know how deceptive this can all be but you can benchmark the Javascript speed here and see that Chrome leaves the competition biting the dust. The above test will freeze IE, and for me it froze IE 7.0 for well over a minute with a dismal score of 26. Safari clocked 93 on the test, FireFox 116, Opera 161, but Chrome clocked an amazing 1183. Just shows you how much speed the Javascript engine of Chrome delivers. You can feel it when you browse other sites as well. The rendering engine is at par with FireFox and I couldn’t notice much difference between the two browsers when Javascript was not around.

The other thing I love about this browser, is the amount of real-estate it gives you. Gone is the menu-bar and the status-bar has shrunk down to a small strip which appears when needed. These are the kinds of GUI innovations that I really appreciate. Logical; since most of us rarely use browser menus when we surf, at least I don’t. An rare trip to the “Preferences” section to clear browsing history is all I need to do with the menus anyways, so I am not too bothered with the fact that the menus in Chrome have been shrunk down to two little corner drop-down buttons. As a matter of fact I have customized FF (FireFox) to do the same using an extension. As expected the navigation bar has a tight integration with the search engine and you will find suggestions being popped up as and when you write directly from Google search. There is still some work to be done on this front though, it would be wonderful to see something along the lines of what Yahoo has on it’s search.

The things that I really miss in Chrome is AdBlock and Noscript. I hope Google will allow some way of having extensions to Chrome so people can come-up with all those amazing things that FF currently has. In fact I miss the extensions thing that is so popular under FF. I also could not find an RSS reader or any method of importing RSS/ATOM links into the browser. I also felt that the status bar was a bit short and could not display longer web-links. Chorme crashed on me once and ironically it was Google’s very own site, google books and yes I was browsing Chrome’s very own book Google Chrome when that happened. It somewhat flies in the face of the claim about application not crashing if one instance crashed. While Google has touted memory management of Chrome as great, the browser does take up a significant chunk of memory with even a modest number of tabs open. It’s certainly a lot more than FF, about 20 – 40% or so more for the same number of tabs.

For some reason the browser continuously uses about 60 to 70% of both CPU cores on my machine and that happens even when the machine is sitting idle and there is no interactions with the browser. Interestingly I also found a lot of memory swapping taking place after the browser ran for 2 hours or more and the memory usage keeps increasing steadily. That would somewhat contradict what was written in the Chrome book about low memory usage. The thing worth mentioning here is the fact that each tab of the browser is a separate process, meaning every tab is actually a separate instance of a browser running.

In the end, Chrome is still a beta product and I would expect significant improvements in the coming versions. I would also hope that Google will at the very least release a Linux version of the browser. The speed of the browser is truly impressive, however it still rough around the edges. The interface is good, but that’s just my taste. I always prefer a minimalistic approach when it comes to GUI. I am however not sure how others may view this. I would conclude by saying, Chrome is a browser worth trying and certainly a browser to keep your eye on.