First results of TAAS

TAAS in action

Finally I am able to present the first results of TAAS. It was a long way to get here. I was actually not sure at all if I will manage to show something at FOTB 09. Let me explain what happens here.

In the image is the original ActionScript code on the left. The ActionScript bytecode produced by the ASC is in the middle and the compiled TAAS version is on the right. You see that I am running a loop and call a method inside that loop. It would be nice to have small helper methods like this one inlined automatically. TAAS can do this for you now.

So how does this work? First of all an SWF file is parsed and the bytecode is extracted. This bytecode is transformed into a control flow graph. This control flow graph is converted into a graph of TAAS expressions which a lot of benifits. The conversion step works like the Flash Player and kind of executes your code. The result is that all methods and local variables are typed if that is possible.

It is much easier to perform a lot of optimizations now. Like inline expansion. In order to do that I can simply compile the method that is called to TAAS and connect the vertices together in the graph after some adjustments have been made. The inline expansion step will automatically inline methods when it thinks it is useful and possible. You can only inline methods that are private or final and make no use of reflections.

Inlined methods create a lot of overhead. Usually they contribute additional local variables and the code gets bigger. So one step after the inline expansion is to clean everything up again. In this example I can remove those registers thanks to copy propagation and dead code elimination.
And it gets even better. If you take a closer look at the example, the calc method expects two paramters that are of type Number. Therefore the original method uses an Add instruction. TAAS knows about the types and sees that it will pass two integers into this method. Since it makes no sense to convert from int to Number to int it stays with integer in this case.

So after inlining the method ends up with even less local variables. The original local variable t1 is considered useless since it is only used one time. TAAS will put the code getTimer() at the position where t1 has been used and we end up with a heavily optimized method. What I like the most is that I do not have to change the way I write my ActionScript code. All happens behind the scenes automatically.
Some other optimizations are implicit. TAAS will use AddInt instead of Add if both operands are typed int. A FlowOptimizer is also involved and will invert the if expression for the loop. This can reduce the number of required jumps in a method by 50%.

I know that the output is not perfect. But this is all a work in progress and really the first result that I can share. The SWFs speak for them self.

Related Posts

33 Responses to “First results of TAAS”


  1. 1 RTisserand

    Sounds like you did it !
    Congratulations, this is just astonishing news !

  2. 2 Mr.doob

    Joa Ebert. Showing Adobe how it’s done since 2005.

  3. 3 Richard Lord

    Hi Joa

    This is fantastic. Looking forward to your talk at FOTB.

    Richard

  4. 4 Monokai

    That is some astonishing work. Would be great if it could be incorporated in a standard compile work flow.

  5. 5 Roland Zwaga

    I wonder why Adobe hasn’t hired you yet…

    Pretty awesome stuff, congrats!

  6. 6 Kaspar

    Cool stuff. I must admit, I am a bit surprised, that such optimizations are possible (or necessary…). Isn’t that something you’d expect to be done by Adobe?

  7. 7 George Profenza

    Mindblowing!

  8. 8 Patrick

    Congratulation!.

    You ‘ve got a wonderfull and clever idea.

    It’s a pity you have to do it by yourself and not Adobe.

    Keep it up.

  9. 9 Scott

    This is a prime example of things I never knew were possible.

    The fact you are executing this code so fast is blowing my mind. I’m also a huge fan that you don’t need to change anything about your code (other than making sure your class is inherited from TAAS)

    I know it’s been said many times before, but fantastic job! :D

  10. 10 joa

    In general one may argument that bytecode optimizations are tedious since the FlashPlayer could optimize the code as well and even better since it knows about runtime constraints (like Java HotSpot does). But unless the player gets blazing fast the only option is to stick with a handwritten optimizer.

    Scott: You do not have to inherit any special things. You just run the TAAS compiler with your SWF as an input. The rest is done automatically.

  11. 11 alex

    this is so great! thank you for this.

    ps: love the courier new font in your input form ;)

  12. 12 Andre

    ich bin dagegen ;)

  13. 13 Erik

    Very cool!

    Must be a nice project to work on with all those new concepts.

    Greetz Erik

  14. 14 Martin

    wow this is awesome.

  15. 15 Anton Volkov

    Arrr! I can’t wait to use it!

    You’re the genius. Flash-world is watching :)

  16. 16 wonderwhy-er

    Very impressive. I think I will be testing this to see performance change on some of my Physics simulations things that are very heavy.

    Was thinking few times that flash community needs such thing but in AS3 not Java form. How do you think if it is possible to convert your code to AS3 from Java? You use some Java optimization library + adobe compiler in this right?

    Anyways having it in AS3 would allow a lot of things like self replicating programs (output itself with changes, like different picture or mp3 file or even different code) or writing browser based client side compiler of Flash. Such marvelous things I really want to be able to do :) But so far only collecting links on stuff like yours for later when I will have time and determination to try and start something like that myself.

  17. 17 Peter Andreas Moelgaard

    Woaaa… Looking SO much forward to your speech at FOTB !!! :-)

  18. 18 joa

    wonderwhy-er: No external components are used, it is all handwritten from scratch. I am thinking about something which would allow us to convert a set of Java *.class files to a *.swf.

    I have a strategy in my mind for doing this. But it is not something I can address in the next couple of months.

  19. 19 wonderwhy-er

    joa: I see. Checking Google code svn right now. Really cool that you are sharing it :) Great job and great contribution to Flash community. Will need to try to play with it later to see how hard it would be to port it to and to see how useful it is.

    As for .class to .swf porting. Not shore how useful that wold be. What I meant was to have open-source community driven Flash compiler with features like yours(recompiling already compiled SWF with optimizations or some changes) + other very useful features like runtime decompiling/compiling of SWF with possibility to change both ABC and embedded resources like images, mp3 or other data. That would allow to make say banner editors that on client side create SWF with embedded XML/images and other stuff and outputs without need to bother the server. Or games that output replays as SWF. And so on and so on.

    Anyways your project definitely goes to my AS3/SWF compilation related links collection.

  20. 20 valyard

    Wow! the speed difference is amazing!
    But in real world applications there’re not many places you can do such function calls inlining. I believe. But I’m noob in compilers and stuff.

  21. 21 a_[w]

    Hi!
    Have you looked decompiled code(through Sothink or Trillix decompiler) after SWF optimization?
    If optimization breaks source code generation in decompiler, it can be used as protection from decompilation.
    Great experiments indeed.

  22. 22 joa

    Writing an obfuscator using Apparat is simple as well. The code is after the optmization no longer exactly what it was before, so a decompiler will not be able to produce anything close to the input.

    By the way, TAAS can be used for a decompiler as well. It is actually very easy: you just have to emit AS3 source code instead of bytecode.

  23. 23 Conrad Winchester

    Are the TAAS optimisations applied when I use TDSI, like this

    java -jar tdsi.jar -integer-calculus true -dead-code-elimination true -input input.swf -output output.swf

    If not – how do I try them out.

    Conrad

  24. 24 joa

    Conrad: TAAS is currently highly experimental. In other words: I still have to do a lot of stuff ;) So it is not implemented in TDSI. TDSI is really just there to allow you to use the Alchemy opcodes.

    However, if you want to use the TaasCompiler, you might have a look at the TaasCompilerTest here.

    You will need a playerglobal.swc, the builtin.abc and toplevel.abc since the compiler uses them in order to type methods and variables correct.

  25. 25 Glassgiant

    Very impressive. Looks like one single brain is able do better work than dozens of engineers over at Adobe.

    Looking forward to see this project growing and feel inspired to get a better understanding what happens “behind the scenes”.

  26. 26 dvorcin

    Well, well, well, what do we have here. A magical swiff tool, huh? Some people say “the magic does not belong to IT” and looks like mister Joe can prove it actually does :)
    Frankly, it kicked my ass. Being a dedicated swf optimization fan, I use HaXe, since it was the first available tool which dared to stumble at adobes “final word” and dared to tweak the swf insides. But here we are, got TAAS at the end of 00s, here we are one step further into the future.

    I’ve got a question: I have downloaded your code via SVN and I am confused by lots of java classes. Please explain, how can I try to pass my swf via your masterpiece? Just a simple explanation to the noob, like “install Java, start cmd, type <>” :)

  27. 27 joa

    dvorcin: Sorry, but right now there is no easy way to trigger anything. You could build the project Apparat.Tests.AS3 and afterwards build Apparat.Core. Then you could run the TaasCompilerTests unit test and would have the TaasCompiler running and optimizing the SWF files of the Apparat.Tests.AS3 project.

  1. 1 Twitter Trackbacks for First results of TAAS at blog.joa-ebert.com – Blog of Joa Ebert [joa-ebert.com] on Topsy.com
  2. 2 Twitted by UnitZeroOne
  3. 3 Twitted by bryngfors
  4. 4 FOTB 2009 Day2 | The random utterances of David Arno
  5. 5 Apparat (TAAS) framework to optimize swf at Jozef Chúťka's blog
  6. 6 beerfug!! - Brian Deitte – Flex Speed Master!

Leave a Reply