Naked JavaScript

Taking the DOM off JavaScript 
« Back to blog

Installing TraceMonkey on Mac OS X and Linux

TraceMonkey is the massively improved JavaScript engine in Firefox 3.5 which employs a trace based compiler to provide blindingly fast JavaScript interpretation. The trace tree code base, named NanoJIT, evolved from a paper published by the Computer Science department of the University of California, Irvine entitled "Making the Compilation 'Pipeline' Explicit: Dynamic Compilation Using Trace Tree Serialization". NanoJIT is a C++ based library from Mozilla that is used in both the Tamarin and TraceMonkey projects that provides a low level abstraction for "near-metal", native compilation of code segments that repetitively occur. Despite all this "geekery", what you need to know is that NanoJIT was a massive improvement to SpiderMonkey that allowed Mozilla to yield high performance JavaScript from their existing libraries.

To begin, you will need Mercurial installed in order to pull down the latest source tree from Mozilla. Mercurial is a source control management system that is designed to efficiently and effectively handle large distributed projects. Luckily installing Mercurial is relatively painless for both target platforms. You will also need autoconf version 2.13. It has to be autoconf version 2.13 otherwise things will go awry; trust me, just use autoconf 2.13. This is not always the default autoconf on either Mac OS X or Linux, but is available for both Mac OS X and Linux by way of package managers, just like Mercurial. We will install both at the same time. 

To install Mercurial on Mac OS X, use MacPorts with the following command:

 sudo port install mercurial autoconf213 

To install Mercurial on Debian/Ubuntu Linux, use apt-get with the following command:

 sudo apt-get install mercurial autoconf2.13 

This will install a variety of dependencies mainly all for Mercurial so its better just to let a proper package manager handle all of the complexities for you. If you prefer, there are also pre-built binaries for Mercurial available here and if you really want to install it from source, I recommend reviewing the documents provided on the Mercurial site.

To obtain the source code for TraceMonkey, first make sure you are in the "javascript" directory we created in the Getting Started process. We will be cloning the TraceMonkey repository locally so that we can compile it. This will also allow us to maintain an up-to-date copy by issuing the following command without the quotes, from within the tracemonkey directory we will be creating next.

 hg pull && hg update 

But before we do that, we need to actually get the source code. If you are on Mac OS X, enter the following commands from within the aforementioned "javascript" directory, without the quotes.

 If you are on Linux, enter the following commands from within the aforementioned "javascript" directory, without the quotes.

This may will take a fair bit of time, especially the clone process of Mercurial, so I recommend entering the commands and then grabbing an adult beverage, I find that helps the process a lot more than coffee. This will configure and build ONLY the JavaScript stack of TraceMonkey, but unfortunately you have to download the entire code base which includes a lot of unnecessary things. Once this process completes, there will be a fresh binary named js in the js/src directory, when you execute it you will notice that it is a world faster than the previously installed Rhino shell both in terms of shell loading and code execution. This why TraceMonkey and its predecessor, SpiderMonkey, have been the default JavaScript interpreter engine for Firefox, it is just incredibly fast in almost every way over Rhino. The only way that Rhino bests the *-Monkey suite is that through Rhino you access the entire JVM stack and all of its libraries. It is up to the reader to determine the value and virtue of each interpreter and use them based on the requirements and needs of the task.

When you execute the standard 


it will run the standard JavaScript shell and in this sense TraceMonkey is only the extension and improvement of the SpiderMonkey interpreter. For the aforementioned NanoJIT functionality to be loaded and used, you must start the shell with the '-j', without quotes. You can verify this with a simple test of running the following line of code:

for(i=0;i<10000;i++) { print(i); }

When you run it without the '-j' option that code will execute the same every time, with the '-j' option it will compile the process to native code and run the native code, so you will notice an incredible speedboost almost magically as you are running through the process. You now have one of the three fastest JavaScript interpreters available, we will install the other two next.

Comments (4)

Jul 22, 2009
Chris Williams said...
Download ubuntu 64bit right now. will post updates as appropriate.

Jul 22, 2009
Kris Kowal said...
Thanks for doing this series. This is probably my third or fourth failed attempt at building Spidermonkey/Tracemonkey; this time "make" erred out, presumably a transient bug. Could you share instructions for checking out a known working version?
Jul 22, 2009
Chris Williams said...
@Kris are you on OS X or on Linux? can you paste the error and I can help you (and others) get past it.

Thanks, Chris

Jul 22, 2009
Kris Kowal said...
Mac OS X.

master $ make
make export
make -C config/ nsinstall
gcc -o host_nsinstall.o -c -DXP_UNIX -DXP_MACOSX -DNO_X11 -O3 -I. -I. -I../dist/include -I../dist/include/nsprpub nsinstall.c
gcc -o host_pathsub.o -c -DXP_UNIX -DXP_MACOSX -DNO_X11 -O3 -I. -I. -I../dist/include -I../dist/include/nsprpub pathsub.c
gcc -o nsinstall -DXP_UNIX -DXP_MACOSX -DNO_X11 -O3 host_nsinstall.o host_pathsub.o
Creating /Users/kris/narwhal/packages/tracemonkey/hg/js/src/.deps
../config/ *** Makefile variable 'CSRCS' changed value after including Was , now .. Stop.
make[1]: *** [export] Error 2
make: *** [default] Error 2

Leave a comment...

Got an account with one of these? Login here, or just enter your comment below.