Archive for April, 2013

Duetto: A faster and smarter alternative to Emscripten. And more.

We have seen a lot of hype on the Web recently after the announce­ment of Asm.js by Mozilla: a new “extra­or­di­nar­ily opti­miz­able, low-level sub­set of JavaScript”. The sys­tem builds on the work that has been done on Emscripten: a LLVM-based solu­tion which com­piles C++ to JavaScript, allow­ing for an easy port of appli­ca­tions and games to the Web. The excite­ment for Asm.js stems from the fact that, by using a spe­cial vir­tual machine inte­grated in Fire­fox, it can improve the per­for­mance of emscripten-generated code and get it even closer to native performance.

We (Lean­ing Tech­nolo­gies Ltd) would like to intro­duce Duetto, our own LLVM-based solu­tion for pro­gram­ming the Web using C++. And by the Web, we mean both the client and server side of it, but let’s talk about the client side first.

Emscripten han­dles C++ code by emu­lat­ing a full byte-addressable address space. This is def­i­nitely a good solu­tion, but sub­op­ti­mal. Javascript is not based on a byte-addressable address model, but on an object-addressable model: all the acces­si­ble mem­ory is con­tained in some object. But when you think about it, C++ is not that different.

Our solu­tion inte­grates with clang and the LLVM tool­chain and is able to map C++ object-oriented con­structs to native JavaScript objects. It turns out that access­ing objects on mod­ern JavaScript engines is faster than access­ing arrays. By using this (and a few more) tricks we man­aged to get the fol­low­ing, pre­lim­i­nary, results on micro benchmarks.

For each benchmark the best time in 10 runs has been selected. The V8 and Spidermonky JavaScript shells has been used. The respective commits are b13921fa78ce7d7a94ce74f6198db79e075a2e03 and b9d56a1e0a61. *The fasta benchmark has been modified by removing a memory allocation inside the main loop.

For each bench­mark the best time in 10 runs has been selected. The V8 and Spi­der­monky JavaScript shells has been used. The respec­tive com­mits are b13921fa78ce7d7a94ce74f6198db79e075a2e03 and b9d56a1e0a61. *The fasta bench­mark has been mod­i­fied by remov­ing a memory

 

We man­aged to do this by real­iz­ing that by dis­al­low­ing some unsafe C++ capa­bil­i­ties (such as type unsafe pointer cast­ing and pointer arith­metics inside struc­tures) it’s actu­ally pos­si­ble to cre­ate more effi­cient, smaller and faster JavaScript code from C++. Inter­est­ingly enough, we dis­cov­ered that, in most cases, the needed lim­i­ta­tions on the lan­guage are actu­ally spec­i­fied as unde­fined behav­iour!

So, yes, Duetto does need some min­i­mal port­ing to bring C++ code to the Web while emscripten makes it mostly free. What you get in exchange for that is faster per­for­mance with no need of a spe­cial VM and deep inte­gra­tion with the browser. Duetto cre­ates a really seam­less C++ pro­gram­ming expe­ri­ence for the Web:

  • Seam­less inte­gra­tion with the browser envi­ron­ment, com­plete access to the DOM and HTML5 tech­nolo­gies includ­ing WebGL. You can even access and use your favourite JavaScript library or exist­ing JavaScript from C++ by declar­ing the avail­able inter­faces in the C++ code using a sim­ple convention.
  • Seam­less client/server pro­gram­ming, using trans­par­ent RPCs in sin­gle code­base. The com­piler will split the code auto­mat­i­cally in the client part (com­piled to JavaScript) and server part (com­piled to native code).

The Duetto back­end is already in a very advanced state, and we believe it’s already suit­able to bring the first appli­ca­tions to the Web. Espe­cially games, which are our pri­mary tar­get. Unfor­tu­nately our front end is not yet as pol­ished as we would like, as we want to improve the error report­ing to make the port­ing expe­ri­ence as smooth as possible.

We are not yet ready to release Duetto, but we are eager to start open­ing col­lab­o­ra­tions, so if you are inter­ested in bring­ing your C++ appli­ca­tion or game to the web, feel free to con­tact me (alessandro@leaningtech.com). We believe that in six months or less from now we will be able to release a robust prod­uct, most prob­a­bly capa­ble of gen­er­at­ing even faster code. And we want to release it as open source.

For more infor­ma­tion please visit our site: http://www.leaningtech.com

12 Comments

Duetto: A faster and smarter alternative to Emscripten. And more.

We have seen a lot of hype on the Web recently after the announce­ment of Asm.js by Mozilla: a new “extra­or­di­nar­ily opti­miz­able, low-level sub­set of JavaScript”. The sys­tem builds on the work that has been done on Emscripten: a LLVM-based solu­tion which com­piles C++ to JavaScript, allow­ing for an easy port of appli­ca­tions and games to the Web. The excite­ment for Asm.js stems from the fact that, by using a spe­cial vir­tual machine inte­grated in Fire­fox, it can improve the per­for­mance of emscripten-generated code and get it even closer to native performance.

We (Lean­ing Tech­nolo­gies Ltd) would like to intro­duce Duetto, our own LLVM-based solu­tion for pro­gram­ming the Web using C++. And by the Web, we mean both the client and server side of it, but let’s talk about the client side first.

Emscripten han­dles C++ code by emu­lat­ing a full byte-addressable address space. This is def­i­nitely a good solu­tion, but sub­op­ti­mal. Javascript is not based on a byte-addressable address model, but on an object-addressable model: all the acces­si­ble mem­ory is con­tained in some object. But when you think about it, C++ is not that different.

Our solu­tion inte­grates with clang and the LLVM tool­chain and is able to map C++ object-oriented con­structs to native JavaScript objects. It turns out that access­ing objects on mod­ern JavaScript engines is faster than access­ing arrays. By using this (and a few more) tricks we man­aged to get the fol­low­ing, pre­lim­i­nary, results on micro benchmarks.

For each benchmark the best time in 10 runs has been selected. The V8 and Spidermonky JavaScript shells has been used. The respective commits are b13921fa78ce7d7a94ce74f6198db79e075a2e03 and b9d56a1e0a61. *The fasta benchmark has been modified by removing a memory allocation inside the main loop.

For each bench­mark the best time in 10 runs has been selected. The V8 and Spi­der­monky JavaScript shells has been used. The respec­tive com­mits are b13921fa78ce7d7a94ce74f6198db79e075a2e03 and b9d56a1e0a61. *The fasta bench­mark has been mod­i­fied by remov­ing a memory

 

We man­aged to do this by real­iz­ing that by dis­al­low­ing some unsafe C++ capa­bil­i­ties (such as type unsafe pointer cast­ing and pointer arith­metics inside struc­tures) it’s actu­ally pos­si­ble to cre­ate more effi­cient, smaller and faster JavaScript code from C++. Inter­est­ingly enough, we dis­cov­ered that, in most cases, the needed lim­i­ta­tions on the lan­guage are actu­ally spec­i­fied as unde­fined behav­iour!

So, yes, Duetto does need some min­i­mal port­ing to bring C++ code to the Web while emscripten makes it mostly free. What you get in exchange for that is faster per­for­mance with no need of a spe­cial VM and deep inte­gra­tion with the browser. Duetto cre­ates a really seam­less C++ pro­gram­ming expe­ri­ence for the Web:

  • Seam­less inte­gra­tion with the browser envi­ron­ment, com­plete access to the DOM and HTML5 tech­nolo­gies includ­ing WebGL. You can even access and use your favourite JavaScript library or exist­ing JavaScript from C++ by declar­ing the avail­able inter­faces in the C++ code using a sim­ple convention.
  • Seam­less client/server pro­gram­ming, using trans­par­ent RPCs in sin­gle code­base. The com­piler will split the code auto­mat­i­cally in the client part (com­piled to JavaScript) and server part (com­piled to native code).

The Duetto back­end is already in a very advanced state, and we believe it’s already suit­able to bring the first appli­ca­tions to the Web. Espe­cially games, which are our pri­mary tar­get. Unfor­tu­nately our front end is not yet as pol­ished as we would like, as we want to improve the error report­ing to make the port­ing expe­ri­ence as smooth as possible.

We are not yet ready to release Duetto, but we are eager to start open­ing col­lab­o­ra­tions, so if you are inter­ested in bring­ing your C++ appli­ca­tion or game to the web, feel free to con­tact me (alessandro@leaningtech.com). We believe that in six months or less from now we will be able to release a robust prod­uct, most prob­a­bly capa­ble of gen­er­at­ing even faster code. And we want to release it as open source.

For more infor­ma­tion please visit our site: http://www.leaningtech.com

12 Comments