May 2021
Mo Tu We Th Fr Sa Su
<< >>


Haxe 2.03 with SWC Output

Posted on Mar 22 2009

Haxe 2.03 has just been released on

SWC Support

This new release includes some still experimental SWC output feature. SWC is the library format used by Flash. Haxe only supports SWC for Flash9+.

The idea is that a lot of people that write AS3 libraries would really love to use Haxe because of the language and its performances, but can't because their users are using AS3.

So far it was already possible to generate AS3 code from Haxe code, but at this time you were losing all Haxe compiler performances improvements. Now since SWC contains the bytecode as it was compiled by Haxe, you can write your library in Haxe and distribute the SWC for AS3 users.

Read for more informations on SWC output.

Once we are sure it works well, I guess the next step will be to add SWC input to Haxe, so you can reuse existing AS3 libraries directly (it's currently possible to do so by using --gen-hx-classes but it has some issues as well).

More Optimizations

Haxe 2.03 also adds new optimizations, including a strong constant expression optimizer. In fact, it often happens after doing some inlining that some expressions are useless.

Takes the following example :

class Test {
    static inline function foo(max,callb,flag) {
        for( i in 0...max * 3 )
            callb(flag ? i : -i);
    static function main() {
        var x = 0;
        foo(50,function(i) x += i,true);

Since we inline the function, it's the same as writing the following :

var x = 0;
for( i in 0...50*3 )
    (function(i) x += i)(true ? i : -i);

With new Haxe constant expression optimizer, it will reduce the expression to the following :

var x = 0;
for( i in 0...150 )
    x += i;

The constant expression optimizer can handle the following patterns :

  • reduce constant numerical expressions such as 1+3 and 2 > 1
  • reduce constant boolean expressions such as true && false
  • reduce if if condition is true or false
  • reduce while if condition is false
  • reduce immediate function calls such as (function(p) { ... })(p). This is very useful as in example above when you pass functions as parameters of inline methods since it actually eliminates the cost of calling the function N times.

  • yuku
    Mar 22, 2009 at 18:07

    Great! Looking forward to using haxe for my next as3 project ♪

  • Philippe
    Mar 22, 2009 at 22:38

    This is a smart move to motivate Haxe adoption!

    And supporting SWC libraries in haxe is the next big thing to add.

  • Hudson
    Mar 23, 2009 at 14:37

    I think SWC creation is going to be a huge help for HaXe adoption. Now I would think any 3D library, or any lib where optimization is a factor, would use HaXe for development and AS3 users could use the SWC without being concerned about HaXe adoption numbers; IOW, this could really get the ball rolling! And support for SWC libraries in HaXe will be really convenient for HaXe developers, particularly if it allows code completion in FlashDevelop, like you get with SWC files in AS3.
    Also, the constant expression optimizer is much more powerful than I was thinking it might be. When it was first discussed on the HaXe mailing list, I was only thinking about the case of pre-calculating constant expressions (which is very nice by itself), but the ability to reduce ifs and function calls is great. This is the kind of thing I find most compelling about using HaXe for development. It allows one to code in a clear straightforward manner while the ingenious compiler allows performance gains that would require confusing obscure code, if possible at all, in AS3.

  • Mar 24, 2009 at 01:09

    This is way too awesome! And makes you want more: would it be feasible to implement compile-time evaluation of constant expressions that invoke reentrant functions, like sin(4) or add(2,3)?

  • Mar 24, 2009 at 14:02

    Floating point operations are a bit more tricky because we need to be able to represent the calculated Float in the generated source (for instance in JS or PHP) with the exact same precision. That's why only float operations that returns limited precision floats are currently reduced.

    Math.PI and Math.* operations are not currently reduced, but that could be added easily if useful.

  • Bruno Garcia
    Apr 17, 2009 at 05:18

    Add SWC importing and I'll love you forever.

  • Apr 26, 2009 at 18:05

    Any chance someone could post a full haxe example that creates a SWC? I'm very excited about this feature, but I can't get my head around how to actually use it.

    For starters, what does this mean: "you need to call new flash.Boot(mc) in your application which initialize the SWC"
    Where exactly do I put that line of code? If I put it in static main, I get a compiler error.

Name : Email : Website : Message :