@joa recently posted a simple example to illustrate the power of his powerfull optimization framework called Apparat.
Apparat is a framework to optimize ABC, SWC and SWF files.
From the description of the Google Code page we get a hint of what Apparat actually does: “Apparat is a framework to optimize ABC, SWC and SWF files.”. In layman’s terms: “Apparat will speed up your Actionscript projects by optimizing certain method calls.”. The Actionscript Library for example provides some ( much ) faster ways of doing math and bitwise operations. Read on to understand more about how to get up and running with Apparat.
First, a confession. This post you are reading right now, is not the original one. After I thought I was done writing it, I sent it over to Joa and asked for a * small * review. A review turned into a complete rewrite as I missed an essential part of the project and made things more complicated for people who just want to integrate Apparat into their projects. So, credit where due: thank you Joa. Without his help this post wouldn’t be where it is now.
All in all, there are only three steps:
- Download Apparat ( the compiled version )
- Download Scala ( Apparat is ( being re-) written Scala )
- Integrate Apparat with a project ( we’ll use Joa’s example )
Download Apparat
Let’s get started by downloading Apparat:
http://code.google.com/p/apparat/downloads/detail?name=apparat.zip
This file contains the compiled SWC among other compiled build tools ( Reducer, TDSI, Apparat, … ) that are part of Apparat.
You can extract this file wherever you want but it makes sense to put it somewhere close to your projects so you can go back to it easily in the future.

Download Scala
We should install Scala from http://www.scala-lang.org/downloads. Scala is a general purpose programming language and states that “Code sizes are typically reduced by a factor of two to three when compared to an equivalent Java application”. Sounds interesting to me, but even more to Joa I guess, as he is using Scala to get Apparat to the next level.
The version you need is RC3 at the time of writing this post. Windows users can download this zip, while the Mac / Unix archive would be this tgz archive.
You can put the extracted folder anywhere, you don’t need to install anything. It does makes sense to put it somewhere next to your working directories. We’ll need the Scala directory in a moment, but first we’ll set up our ( example ) project.
Integrate Apparat with a project
For brevity’s sake and to make this howto as easy to follow along as possible, we’ll download Joa’s most recent example:
get it from http://blog.joa-ebert.com/2010/05/26/new-apparat-example/.
After you extracted the source folder of the example project, you should put it in a nice place. Somewhere next to your other Flash projects maybe. Fire up FDT* and click File > New > New Flash Project from the application menu ( top of the window ) and point to the example folder as your working directory.
* We’re using FDT because it allready comes with an ANT builder plugin. It should be possible to use Flash Builder but you’re on your own ( for now – maybe I’ll write something about that in a next post if there is interest ).
If you’re not familiar with ANT you will learn something sweet from this post. ANT is a build tool, that can automate the sometimes tedious process of building a project. Imagine you want to compile an FLA from within FDT or Flash Builder, ANT can do that for you. It can even upload a compiled file to your staging server if you want. Do read more about ANT to learn all about it; its definitely worth it.
In the introduction of Joa’s blog post we read:
A complete example is also available. Just change the paths in the build.properties file and compile everything using Ant.
So let’s do that. Open up the example project in your FDT workspace and edit the build.properties file. You can find all files related to ANT tasks in the “build” directory. You’ll find three empty variables there: FLEX_HOME, SCALA_HOME and APPARAT_HOME. Add the correct values for your machine. Windows users should use \ ( backslashes ) in stead of the forward slashes I’m using on my OS X machine. Update: everyone ( Mac & Win ) should use forward slashes.

Save your changes in the build.properties file.
Next we’ll link our project to the compiled Apparat.swc file. The SWC is included in the Apparat download we extracted earlier. In the readme file of the example project we learn that we need to pass an APPARAT_FLEX path to our Flash project in FDT. Let’s do that by rightclicking on our example project and choosing New > Linked Libraries. Click the “add” button and create a new path “APPARAT_FLEX”. The path should point to your Apparat folder containing the Apparat.swc file.
Now we should be able to compile the project using the ANT task. Rightclick on the build/build.xml file. Choose Run as > Ant Build from the context menu. You should see ANT triggering several tasks in the Console view, with success off course. The result is a compiled version of both the example-apparat.swf and example-as3.swf files in your “bin” folder.
You can see a clear difference in the framerate of the two swf files. On my recent MBP I get 28fps on the AS3 example and a whopping 40fps on the Apparat version. And don’t forget: that’s 80.000 particles flying by in front of you!
![]()
If you see some errors in the Console, try to figure out what’s wrong, chances are one of the variables we edited before in the build.properties file are wrong.
“What’s the difference between me and you?”
What makes the Apparat version so fast? Inline methods sir. If you check out the two class files in the “src” folder of the example project in your FDT workspace, you can see they look almost the same. The only difference is that the the Apparat class file is using FastMath. As you can see from this screenshot ( click for full size ):
FastMath is a class provided by the Apparat SWC ( which we linked before ) and speeds up some mathematical functions. These function are recognized and “inlined”, making them so much faster. Inlining means that the compiler will put the actual method in the executed code, in stead of calling it from memory. So in stead of storing “Fastmath.rsqrt” it will put the actual code of the FastMath.rsqrt method where ever it is needed when compiling.
E.g.: in stead of using:
// AS3 version
dt = 1.0 / Math.sqrt(dx * dx + dy * dy)
the Apparat class uses:
// the faster Apparat way, will get inlined by the compiler
dt = FastMath.rsqrt(dx * dx + dy * dy)
Don’t forget to initialize FastMath when using it in your own projects!
Conclusion
Getting Apparat into your project should be very easy once you have gone through these ( actually simple ) steps. The learning curve was a bit steep for me, but I’m so glad I finally pulled it off with the help of Mr. Ebert. Please comment, ask, criticize in the comments below and tell us about your progress with this howto.
If there is interest in articles like these, I’ll try to post more Apparat related articles in the nearby future.



Very well written. Thank you for this post. I really appreciate it very much…
Hopefully this wasn’t the last post on that. Would be nice to have all related things on you blog!
Thanks, Marcel
Thank you very much for the post. Just one note: I would recommend using slashes on Windows as well in the build.properties (e.g. “C:/dev/apparat”).
really welcome article ! thanks !!
Hey, I had heard of apparat before, but I’ve never seen it so clearly explained. Thank you. And yes, there is interest. At least I will be looking forward for more!
Thanks for the feedback. Appreciate it!
@joa: updated
I’d be really interested in a tutorial for setting up Apparat with Flex/Flash Builder.
Hey Rezmason
I’ve got an example up and running. Will add some directions for Flash Builder soon!
I got an error when trying to run the ANT
Buildfile: D:\workspace\apparatTest\build\build.xml
BUILD FAILED
D:\workspace\apparatTest\build\build.xml:52: The following error occurred while executing this line:
jar:file:/C:/apparat/apparat.jar!/apparat/embedding/ant/antlib.xml:22: typedef A class needed by class apparat.embedding.ant.stripper.StripperTask cannot be found: scala/ScalaObject
Any idea?
Found the problem, you need to setup the path of scala to C:/scala/ insteed of C:/scala/bin/
Glad you found the fix Jean-François. Thanks for sharing your experience in the comments.
How do you do this with flash develop?
Daniel, this should get you started: http://www.flashdevelop.org/community/viewtopic.php?f=4&t=6105
Basically you just have to be able to build an Ant task as discussed in the article. I have something ready for Flash Builder ( which uses the same principles ).
Let me know if you need more info!
This seems like a good AS3 alternative to Haxe if one needs to re-compile in Flash (to keep metadata annotations and such which Haxe currnetly doesn’t support).
“All in all, there are only two steps:”
And then you list three!
It looks like it doesn’t work with scala 2.8.0 final since it looks for scala 2.8.0.rc7, can you confirm this? Thank you!
Pingback: FlashApplications » How to get up and running with Apparat
Pingback: speed up as3 swf file by apparat framework « CODE@????
I try to use apparat with flash builder 4, but this does not work
Can you show us your example with flash builder and perhaps the latest version of apparat ?
Thanks
Fantastic work! Would love to see your Flash Builder implementation. My PV3D project needs a kick up the back-side and this is the way forward.
Pingback: Flash Builder and Apparat :: Cult Creative :: A Digital Agency
I completed this tutorial and generated the two .swf files “example-as3.swf” and “example-apparat.swf” with the following versions of Apparat and Scala:
Apparat: “apparat-aggregator-1.0.RC6″
Scala: “scala-2.8.0.final”
Everything seems to work great during the build process, I even got it working w/Flash Builder and the Eclipse Ant plugin.
example-as3.swf works fine (slow as heck) but
example-apparat.swf gives me this error when I run it:
ReferenceError: Error #1056: Cannot create property domainMemory on flash.system.ApplicationDomain.
at apparat.memory::Memory$/select()[/home/joa/Development/apparat-release/target/checkout/apparat-ersatz/src/main/as3/apparat/memory/Memory.as:64]
at ExampleApparat/init()
at ExampleApparat()
I tried running tdsi both in the ant script and from the command line with the following arguments (based on what I could gather from the apparat README)
tdsi -i example-apparat.swf -f true -a true -e true -m true
tdsi -i example-apparat.swf -f
No-dice in all cases, running the .swf produces the above exceptions. Any ideas?
Sigh… I’m an idiot I was pointing at flex sdk 3.5 not flex sdk 4.1. This is a great tutorial, thanks for putting the time into posting it.
Great to see you’ve figured it out by yourself. And thanks for the kind words, that’s the reason why one writes a tutorial I guess : )
Pingback: Weekly Digest for October 2nd — Hello. My name is Václav Van?ura.
Pingback: Flash Builder & Apparat example: BUILD FAILED “java.io.IOException: Cannot run program “…/mxmlc” (in directory “…/apparat-ant-example”): error=2, No such file or directory « timshaya