Carakan
By Jens LindströmJensL. Wednesday, February 4, 2009 11:29:35 PM
Over the past few months, a small team of developers and testers have been working on implementing a new ECMAScript/JavaScript engine for Opera. When Opera's current ECMAScript engine, called Futhark, was first released in a public version, it was the fastest engine on the market. That engine was developed to minimize code footprint and memory usage, rather than to achieve maximum execution speed. This has traditionally been a correct trade-off on many of the platforms Opera runs on. The Web is a changing environment however, and tomorrow's advanced web applications will require faster ECMAScript execution, so we have now taken on the challenge to once again develop the fastest ECMAScript engine on the market.
The name Carakan, like the names of Opera's previous ECMAScript engines, Futhark, Linear A and Linear B, is the name of a writing system, or "script".
We have focused our efforts to improve upon our previous engine in three main areas:
Register-based bytecode
The last couple of generations of Opera's ECMAScript engine have used a stack-based bytecode instruction set. This type of instruction set is based around a stack of values, where most instructions "pop" input operands from the value stack, process them, and "push" the result back onto the value stack. Some instructions simply push values onto the value stack, and others rearrange the values on the stack. This gives compact bytecode programs and is easy to generate bytecode for.
In the new engine, we've instead opted for a register-based bytecode instruction set. In a register-based machine, instead of a dynamically sized stack of values, there's a fixed size block of them, called "registers". Instead of only looking at the values at the top of the stack, each instruction can access any register. Since there is no need to copy values to and from the top of the stack to work on them, fewer instructions need to be executed, and less data needs to be copied.
Native code generation
Although our new engine's bytecode instruction set permits the implementation of a significantly faster bytecode execution engine, there is still significant overhead involved in executing simple ECMAScript code, such as loops performing integer arithmetics, in a bytecode interpreter. To get rid of this overhead we are implementing compilation of whole or parts of ECMAScript programs and functions into native code.
This native code compilation is based on static type analysis (with an internal type system that is richer than ECMAScript's ordinary one) to eliminte unnecessary type-checks, speculative specialization (with regards to statically indeterminate types) where appropriate, and a relatively ambitious register allocator that allows generation of compact native code with as few unnecessary inter-register moves and memory accesses as possible.
On ECMAScript code that is particularly well-suited for native code conversion, our generated native code looks more or less like assembly code someone could have written by hand, trying to keep everything in registers.
The register allocator is designed to be architecture independent, and so is the code generation "frontend" where most of the complicated decisions are made. We have initially implemented a backend for 32- and 64-bit x86, but some preliminary work has already started to generate native code for other CPU architectures, such as ARM.
In addition to generating native code from regular ECMAScript code, we also generate native code that performs the matching of simple regular expressions. This improves performance a lot when searching for matches of simple regular expressions in long strings. For sufficiently long strings, this actually makes searching for a substring using a regular expression faster than the same search using String.prototype.indexOf. For shorter strings, the speed is limited by the overhead of compiling the regular expression.
For more complex regular expressions, native code generation becomes more involved, and improves performance less since the regular expression engine is already reasonably fast. The base regular expression engine is a newly developed one, but it's already made its debut in Presto 2.2 (the browser engine used in Opera 10 Alpha). It's fundamentally a typical backtracking regular expression engine, but does some tricks to avoid redundant backtracking, which usually avoids the severe performance issues a backtracking regular expression engine can have on specific regular expressions.
Automatic object classification
Another area of major improvement over our current engine is in the representation of ECMAScript objects. In the new engine, each object is assigned a class that collects various information about the object, such as its prototype and the order and names of some or all of its properties. Class assignment is naturally very dynamic, since ECMAScript is a very dynamic language, but it is organized such that objects with the same prototype and the same set of properties are assigned the same class.
This representation allows compact storage of individual objects, since most of the complicated structures representing the object's properties are stored in the class, where they are shared with all other objects with the same class. In real-world programs with many objects of the same classes, this can save significant amounts of memory. It can be expected that most programs that do create many objects still only have a few different classes of objects.
The shared lookup structures for properties also allow us to share the result of looking up a property between different objects. For two objects with the same class, if the lookup of a property "X" on the first object gave the result Y, we know that the same lookup on the second object will also give the result Y. We use this to cache the result of individual property lookups in ECMAScript programs, which greatly speeds up code that contains many property reads or writes.
Performance
So how fast is Carakan? Using a regular cross-platform switch dispatch mechanism (without any generated native code) Carakan is currently about two and a half times faster at the SunSpider benchmark than the ECMAScript engine in Presto 2.2 (Opera 10 Alpha). Since Opera is ported to many different hardware architectures, this cross-platform improvement is on its own very important.
The native code generation in Carakan is not yet ready for full-scale testing, but the few individual benchmark tests that it is already compatible with runs between 5 and 50 times faster, so it is looking promising so far.

1 2 3 4 Next »
Rafald.i.z. # Wednesday, February 4, 2009 11:52:42 PM
Charles SchlossChas4 # Wednesday, February 4, 2009 11:55:44 PM
look forward to testing
Aux # Wednesday, February 4, 2009 11:58:38 PM
PhoenixP3Kphoenixp3k # Thursday, February 5, 2009 12:01:03 AM
olli # Thursday, February 5, 2009 12:04:23 AM
Ottoottouk # Thursday, February 5, 2009 1:07:40 AM
Andrew Gregory # Thursday, February 5, 2009 1:38:21 AM
boo!barauswald # Thursday, February 5, 2009 1:50:07 AM
Can Opera use them (I mean, is it legal?), and upgrade for own needs?
Anyway, I hope your engine will be much better than already finished! :-)
safersurfer # Thursday, February 5, 2009 2:21:31 AM
For starters, the firefox extension "noscript" ( http://noscript.net/ ) is very handy in protecting against malicious attacks from untrusted sites.
I love Opera, but I feel safer when I can decide who gets to run what on my system. Otherwise, a faster javascript engine just lets some new attack run twice as quickly.
Charles SchlossChas4 # Thursday, February 5, 2009 2:33:09 AM
Michael A. Puls IIburnout426 # Thursday, February 5, 2009 3:27:53 AM
FataL # Thursday, February 5, 2009 3:59:08 AM
I hope there will be some test release of engine.
Personally I don't see a big need for JavaScript speed improvement (can anyone point where it really needed except benchmark results?).
Compatibility improvements and bugs fixes is much more important for me as user and web designer.
Charles SchlossChas4 # Thursday, February 5, 2009 4:09:44 AM
Charles SchlossChas4 # Thursday, February 5, 2009 5:30:50 AM
http://weblogs.mozillazine.org/asa/archives/2009/02/go_opera.html
MyOpera team, please fix this!fearphage # Thursday, February 5, 2009 5:46:54 AM
Haavardhaavard # Thursday, February 5, 2009 7:25:28 AM
Originally posted by safersurfer:
This really has very little to do with the actual JS engine. Please use the wishlist forum for feature requests, and avoid posting off-topic comments here. You should also try a forum search before posting requests, as NoScript-like functionality is already built into Opera.sabisan # Thursday, February 5, 2009 7:51:00 AM
FataL # Thursday, February 5, 2009 8:32:04 AM
sabisan # Thursday, February 5, 2009 11:41:28 AM
Bruno Casanobrunitoc # Thursday, February 5, 2009 2:14:56 PM
I hope you make the best Opera release ever !!!
Keep up the good work !!
mvonballmo # Thursday, February 5, 2009 2:33:31 PM
@Andrew Gregory: How is Opera's engine getting a bad rep now?
I saw this performance comparison recently and Opera is placed quite well: Dojo vs JQuery vs MooTools vs Prototype
Horicӑhdragomir # Thursday, February 5, 2009 2:46:35 PM
brain-bug # Thursday, February 5, 2009 3:07:28 PM
I see that performance is an issue, especially with modern web apps, but for me the main advantage of Opera - even over its speed and its standard conformity - is its low memory footprint. Right now I have ~120 open tabs in my Opera window (I dare you try that with firefox ;P), most of which I guess have at least some JS...
Sk0l # Thursday, February 5, 2009 3:36:45 PM
VarunVarunM # Thursday, February 5, 2009 3:47:32 PM
Opera is already 2 times slower than the fastest,so by its release date others will also have progressed.Opera to me seems to be skipping a generation.
Having said that Opera still feels faster,and to me JS wars is all about bragging rights which matter a lot too.
Haavardhaavard # Thursday, February 5, 2009 4:03:53 PM
Perhaps you should wait and see instead of worrying too much.
Strategies # Thursday, February 5, 2009 4:25:59 PM
MyOpera team, please fix this!fearphage # Thursday, February 5, 2009 4:29:42 PM
Originally posted by haavard:
That's easy to say when you're inside seeing the gears move, tickets filed and fixed, and progress being made. From out here, we get lots of talk, specs, reactions, and ideas. This chatter/discussion will seemingly last for a long time since peregrine is alpha now and these changes won't be in peregrine or core 2.3. So we'll be hearing a lot about it and won't see it for easily a year from now if history tells us anything.Haavardhaavard # Thursday, February 5, 2009 6:37:37 PM
Kamaleshkamalesh # Thursday, February 5, 2009 6:58:27 PM
How much tradeoff are you seeing with percent-increase of footprint & memory, anyway?
(And your blog comment actually seemed to be covered by a few good tech sites.)
FataL # Thursday, February 5, 2009 7:41:42 PM
Ankur Srivastava anchorschmidt # Thursday, February 5, 2009 8:04:56 PM
Haavardhaavard # Thursday, February 5, 2009 8:14:29 PM
Grrblt # Thursday, February 5, 2009 9:23:08 PM
lucideer # Thursday, February 5, 2009 9:49:47 PM
Originally posted by JensL:
This is something that's being talked about a lot in the competitors, but I've not seen partial compilation mentioned. Are the other engines doing this or is it unique to Carakan?
What is "ECMAScript code that is particularly well-suited for native code conversion"? Other than the exclusion of complex regexp.
Øzikzakatak # Thursday, February 5, 2009 9:51:16 PM
also hope firefox and chrome dont end "taking" from opera this work as its been doing it all day long.
thats why there is this sense that opera is missing one generation.
but im sure opera 10 will give enough to wait to see this engine fully used on a year or so...
rseiler # Thursday, February 5, 2009 10:00:47 PM
Haavardhaavard # Thursday, February 5, 2009 10:03:12 PM
rseiler: You are assuming that Opera will stand completely still while the others are moving. That's obviously not true. Considering that Carakan is currently being newly developed, it might make more sense to assume that it is a generation or two ahead of the others, or more "future-proof"...
Andrew Gregory # Friday, February 6, 2009 12:10:35 PM
When fairly mainstream news sites publish articles like:
http://news.cnet.com/8301-13505_3-10151584-16.html
CNet will have a much wider audience than the average blog, and the big graph gives a clear and easy to remember comparison of Opera and IE versus all the other browsers. The average person will read "industry standard Sunspider Javascript test" and believe everything.
Opera has had a bad Javascript reputation for years. Basic DHTML support existed in IE4 and NS4 around 1997, but Opera didn't get similar support until v7 in 2003, six years later. The bad impression from such a long lag is hard to shake.
Opera's JS support is vastly improved now. Sunspider is IMO merely a benchmark tweaked to show off SquirrelFish and V8 at their best. Opera still has their poor reputation hanging around for a variety of reasons, and these recent performance comparisons aren't doing anything to change that.
VarunVarunM # Friday, February 6, 2009 12:25:16 PM
@mgillespie
That was one major geeky comment mate,lol
Andre Ramalho Ortigao Correaandreortigao # Friday, February 6, 2009 12:31:44 PM
That would be nice to see
Not just for testing speed on real web, but also to check compatibility
Andrew Gregory # Friday, February 6, 2009 12:34:52 PM
Asbjørn Ulsbergasbjornu # Friday, February 6, 2009 1:37:54 PM
Haavardhaavard # Friday, February 6, 2009 3:22:34 PM
Simon Houstonshoust # Friday, February 6, 2009 3:25:41 PM
This is based on the assumption that there will be no more rendering engine changes from alpha to final. (apart from bug fixes of course)
Charles SchlossChas4 # Friday, February 6, 2009 4:06:05 PM
Øzikzakatak # Friday, February 6, 2009 6:42:03 PM
cheers on that one mate!... cause basically that will make way better opera if it takes good things out of the bad ones
i have the feeling that opera has set the route to fight back on the browser war.
strategy?: keep developing towards next generation of browsing experience and "not just browser incremental improvements", preparing to be on the stage when market demands it.
something smells like beta around this days.
mrThe # Saturday, February 7, 2009 9:38:40 AM
\
Привет хабрахабр!)
VarunVarunM # Saturday, February 7, 2009 12:25:24 PM
If Cnet does start shaping the Internet opinion and state then were all screwed.And its hardly a personal opinion.
"If other vendors can "take" from Opera, Opera can "take" from them"
Well even after giving out continuous versions with unique innovations,Opera is languishing in share % numbers,Chrome took less than 2 months to overtake it, which i am personally offended by.
Opera needs some entirely different stragety,than JS engine or new UI.
Maybe something along the lines of better marketing.
Alexander Sokolovalsokoloff # Saturday, February 7, 2009 1:17:56 PM
So when we gonna see this new engine ? Opera 10 ? 10.5 or 11 ?
Прорвемся!