I'm currently working on a project for which the customer requires the SWF to 
be very small, so I've been investigating how to reduce SWF sizes.

Flash Builder's compiler is smart enough to not include code for unreferenced 
classes, but it seems to miss some other seemingly obvious opportunities to 
reduce SWF sizes. In particular, I've found that the SWFs can get bloated by 
unreferenced functions and constants. 

The code below is a simple illustration of what I mean. CodeSizeTest class 
creates an instance of AnotherClass. AnotherClass has a static constant 
SOME_CONSTANT and a function vecSum(), neither of which are used. If I export a 
Release build using Flash Builder 4.5, I get an 830-byte SWF. Commenting out 
the declaration of SOME_CONSTANT reduces the size to 766 bytes. Commenting out 
the vecSum() function further reduces the size to 625 bytes. A few hundred 
bytes doesn't matter much, but in a larger project with many dozens of classes, 
each with many methods, one can easily add a 5-10 KB of unnecessary bloat to 
the SWF. 

One might wonder why I simply don't comment out unused constants and functions. 
The problem is that I'm using a library of classes that are used across many 
different projects; the classes are designed to be fairly general purpose, so 
they often include additional functionality that's used by some, but not all of 
the projects.

It seems to me that a good compiler should be able to identify unused functions 
and constants, and not include the corresponding bytecode in the SWF. Does 
anyone know of any compiler options or third-party optimization tools to do 
that?


// CodeSizeTest.as
package
{
    import flash.display.Sprite;
    public class CodeSizeTest extends Sprite
    {
        public function CodeSizeTest()
        {
            var waste:AnotherClass = new AnotherClass;
        }
    }
}

// AnotherClass.as
package
{
    public class AnotherClass
    {
        public static const SOME_CONSTANT:String = "Nobody ever uses this 
constant"; 

        public function AnotherClass()
        {
        }
        
        public function vecSum(x:Vector.<Number>):Number
        {
            var sum:Number = 0;
            var n:uint = x.length;
            for (var k:uint = 0; k < n; k++)
            {
                sum += x[k];
            }
            return sum;
        }
    }
}




_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to