Author Topic: Flixel array access optimization  (Read 3308 times)

axcho

  • Active Member
  • ***
  • Posts: 174
  • Karma: +0/-0
    • View Profile
    • Evolution Live!
Flixel array access optimization
« on: Sun, Dec 20, 2009 »
I was just poking around the Flixel code and I noticed some opportunities to make some significant speed optimizations.

The idea is that one should store each element of an array in a typed local variable before using it. This is supposed to be considerably faster, since the Flash Player doesn't have to guess what type is in the array.

For example, this code could be optimized:
Code: [Select]
var cl:uint = _children.length;
for(var i:uint = 0; i < cl; i++)
if((_children[i] != null) && _children[i].exists && _children[i].visible && _children[i].onScreen()) _children[i].render();

To this:
Code: [Select]
var cl:uint = _children.length;
for(var i:uint = 0; i < cl; i++)
{
var child:FlxCore = _children[i];
if((child != null) && child.exists &&child.visible && child.onScreen()) child.render();
}

This is a simple fix and I'd really like to see it in the official Flixel code. Thanks for your consideration. :)

axcho

  • Active Member
  • ***
  • Posts: 174
  • Karma: +0/-0
    • View Profile
    • Evolution Live!
Re: Flixel array access optimization
« Reply #1 on: Sun, Dec 20, 2009 »
As you may be wondering, I found this optimization tip here.

Quote
Itís worth mentioning that the speed is actually about the same when using the array strong type method versus the Vector method, so if you already use the first method, thereís no need to use Vectors for performance reasons.

Quote
I havenít spent much time with flash 10, but itís amazing to hear that simply typing the elements of an array will grant you similar performance as using vectors in flash 10. Great collection of tips, thanks for the post.

Same as using Vector? :o That is reason enough for me...

Now it's just up to Adam Atomic to make it official. ;)

Adam Atomic

  • Founder
  • Key Contributor
  • *****
  • Posts: 852
  • Karma: +0/-0
  • new dad
    • View Profile
    • Adam Atomic
Re: Flixel array access optimization
« Reply #2 on: Sun, Dec 20, 2009 »
oh haha yea I should update those loops!  maybe i will do that right now??

OrpheusTheBard

  • Member
  • **
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: Flixel array access optimization
« Reply #3 on: Sun, Dec 20, 2009 »
Great tip! Thanks for the link too :) As an ActionScript begginer, it turned out to be quite helpful. And it makes sense too, but I'm so used to strong typed languages, that I forgot to think about the conversions that Flash would have to do everytime I used an array element directly.

ShooterMG

  • Member
  • **
  • Posts: 71
  • Karma: +0/-0
    • View Profile
Re: Flixel array access optimization
« Reply #4 on: Mon, Dec 21, 2009 »
If we're talking about speed optimization, wouldn't it be better to use Vectors anywhere you could, instead of Arrays?

jeanpier

  • Guest
Re: Flixel array access optimization
« Reply #5 on: Mon, Dec 21, 2009 »
Vectors are faster but require Flash Player 10 to run, I don't think is good idea yet.
Speaking about Arrays it's better and faster:

var myArray:Array = [];

instead:

var myArray:Array = new Array();

;)

axcho

  • Active Member
  • ***
  • Posts: 174
  • Karma: +0/-0
    • View Profile
    • Evolution Live!
Re: Flixel array access optimization
« Reply #6 on: Tue, Dec 22, 2009 »
To add to this conversation, here are some performance comparisons of various data structures, including Array and Vector: FlashPlayerContainerPerformance

Looks like Vector isn't even much faster? :-\