Menu:

Sponsor

Discover Master of Alchemy, our first iPad/iPhone and iPod touch game!

Follow Me

 

Forum's topics

Latest Files

Archives

Top Rated

Categories

Photo Gallery


Alessandro Crugnola on July 25, 2009 in actionscript , flex

[Bindable]? No, thanks

Working on huge projects sometimes let you take long optimization sessions.
During one of these sessions I was trying to find a way to optimize a bunch of code which was taking too long time to process.
The big big bottleneck I discovered, and I didn’t know before (my fault), was the [Bindable] attribute of one of the classes included in the process.
This process was creating something like 1 million instances of this class during its life. After a first optimization in which I created an object pooling, thus reducing the total execution time from 16 seconds to 13, I found this incredible trick which reduced the total execution time to 1 second!!

Let’s say you have a class like this:

package it.sepy.test
{
    [Bindable]
    public class RGB
    {
        private var _red: int;
        private var _green: int;
        private var _blue: int;

        public function RGB()
        {
        }

        public function get red():int
        {
            return _red;
        }

        public function set red(v:int):void
        {
            _red = v;
        }

        public function get green():int
        {
            return _green;
        }

        public function set green(v:int):void
        {
            _green = v;
        }

        public function get blue():int
        {
            return _blue;
        }

        public function set blue(v:int):void
        {
            _blue = v;
        }

    }
}



and a simple script which create 100000 instances of this class (my script wasn’t like this, but for simplicity I’m doing this example):

package
{
    import flash.display.Sprite;
    import flash.system.System;

    import it.sepy.test.Pool;
    import it.sepy.test.RGB;

    public class test extends Sprite
    {
        public function test()
        {
            startTest();
        }

        private function startTest(): void
        {
            var t1: Number = new Date().getTime();

            for( var i: int = 0; i < 100000; i++ )
            {
                var rgb: RGB = new RGB();
                rgb.red = i%255;
                rgb.green = i%255;
                rgb.blue = i%255;
            }

            var t2: Number = new Date().getTime();

            trace('total execution time:', (t2-t1), 'ms');
            trace('memory usage:', System.totalMemory/1024, 'kb' );
        }
    }
}



now launch the script and in the output I have this result

total execution time: 2381 ms
memory usage: 5924 kb



NOW. Remove the [Bindable] property from the RGB class and launch again the test script. See at the output console and the new results are:

total execution time: 236 ms
memory usage: 2720 kb



So, I know that bindable were not created for this kind of jobs, but if using it make 10 time slower my scripts, then I don’t see any valid reason to use it anywhere.

 

Bookmark and Share

 

 

3 comments
Note that it is mainly the assignments that take more time to perform when using [Bindable] and not the construction. I assume this is because the way in which bindable properties/accessors are wrapped in functions that perform the inequality testing and event dispatching.
Yeah, if you don't need it to be bindable, definitely don't use it. Also, if you used individual [Bindable (event="eventName"] tags on each property, it would be way faster than use the tag at the top of the class.


Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)


Type the characters you see in the picture above.





 

TrackBacks

TrackBack URL for this entry: http://www.sephiroth.it/cgi-bin/mt/mt-tb.cgi/267