Home  >  

Iterative Data Storage in AS3

Author photo
December 27, 2009 | | Comments (3)
AddThis Social Bookmark Button

Pretty much every developer encounters situations where she needs to deal with a group of objects in a similar manner. Whether that's a set of movie clips for a game, a set of questions in a survey, or a set of data for a group of users that you need to display, this is a fairly common problem. Most of us, hopefully, graduate fairly quickly from this type of syntax

 
mc_1.x += 2;
mc_1.y += 2;
mc_2.x += 2;
mc_2.y += 2;
mc_3.x += 2;
mc_3.y += 2;

or even this syntax

 
var num_mcs:int=3;
for (var i:int=1; i<=num_mcs; i++) {
    var mc:MovieClip=this["mc_"+i];
    mc.x += 2;
    mc.y += 2;
}

To something more like this

 
var mcs:Array=[mc_1, mc_2, mc_3]
for each (var mc:MovieClip in mcs) {
    mc.x += 2;
    mc.y += 2;
}

And arrays make programming this type of functionality easier. But recently I've started thinking a lot more about performance (disclaimer: I'm a lot more of a Flex developer than a Flash developer, so a lot of the time raw performance still has to take a back seat to convenience of data access offered by ListCollectionView). Arrays perform fairly well, but for performance-critical applications, we might need to consider other options, such as the Vector data type introduced in Flash 10. I thought I'd compile some of the research I've done on some of these structures for others to use.

I found some benchmark tests run by Polygonal labs, and I'll be referring to these benchmarks throughout the article.

Vector

A Vector is similar to an Array where all of the elements have the same data type. In fact, its API is nearly identical to the Array API, with the exception that it also has a fixed property that allows you to set the Vector to a certain length, where you can't add to it or remove from it. The type safety of Vector offers obvious advantages over using an Array, since this can potentially reduce the amount of error checking you need to do. However, since the Vector data type does not have a property or method that tells you what the data type is that it holds, this may be of limited value to Flex developers and Flash Component developers, who often have to accept a collection that another developer built.

In the Polygonal benchmarks, Vector offers some speed gain when reading and writing number data types, but Array wins out on Booleans, Strings, and Objects.

For more on using Vectors, check out Mike Chambers' Using Vectors in Actionscript 3 and Flash Player 10.

Linked Lists

I think the title "linked list" is a bit of a misnomer, because a linked list is actually a collection of objects where each object contains a pointer to the next object. In other words, there isn't really a list, and the linking is pretty much between the nodes themselves. This means that each node must be a custom data type that has at least two properties:

  1. The information being stored
  2. The pointer to the next link.

In a doubly linked list, the links also have a pointer that points to the previous node.

The list itself, at the very least, holds a direct reference to one of the nodes that acts as a starting point to the collection. Some developers choose to also add code to the linked list class to manage iteration through the list. My preference is to provide a separate cursor type class to allow for multiple "views" of the same linked list and to keep this as light as possible.

The polygonal benchmarks are run with a fairly heavy implementation of a linked list and still perform relatively well, so possibly it's not important to keep the weight of the link manager class down. For more on using linked lists, check out Deciding whether to use arrays or linked lists. One point I disagree with is their contention that linked lists are inherently one dimensional. Just because most people aren't implementing linked lists with an up and down direction, for example, doesn't mean you couldn't. I can see where such an addition would be very useful for my blitting example to allow for scaling the rotating happy face.

ByteArray

ByteArrays can be useful both in highly compressing data for transfer and for number crunching. For example, you could use a ByteArray to send information about the layout of tiles for a tile game, and this file would be smaller than a comparable XML file. This would improve the load time, resulting in a smoother user experience. Another example is that you could use ByteArrays to check whether a given item exists in each of two Arrays. Honestly, this is a method I am just beginning to understand, but all the cool kids are doing it.

Unfortunately, the Polygonal benchmarks did not say exactly what they did in their failing benchmarks with ByteArray, but Doug Muccune's example of checking two collections against one another shows it as being faster than an Array loop, hash table, or BitmapData.

For more information on using ByteArrays, see ByteArrays for Beginners.

Alchemy Memory

Alchemy memory is essentially direct access to the computer memory used by the Flash Player, and the only reason I'm mentioning it in this article is that someone will probably ask, since it's mentioned in the Polygonal benchmarks. This is a technique that's way above my pay grade at this time, but most of the references I could find seem to indicate that it is not something that works well from Actionscript.

I hope you will find this article useful. If you're using special structures to optimize your data storage and retrieval, please share your experiences in the comments section.

Read more from Amy Blankenship. Amy Blankenship's Atom feed

Comments

3 Comments

kastanien extrakt said:

Hi,
Very informative post.I feel for many people, this is the ideal way to keep important information safe and accessible.Thank you very much for sharing.
kastanien extrakt

Jochen Szostek said:

Very useful article!

Love the linked ByteArray presentation a lot.

Thanks

Antheor said:

Thx for your article.
You stop your code example with :

var mcs:Array=[mc_1, mc_2, mc_3]
for each (var mc:MovieClip in mcs) {
mc.x += 2;
mc.y += 2;
}

What would be a better/faster code with the vector class ?

Leave a comment


Type the characters you see in the picture above.


Tag Cloud

Technical Speakers

Who is the best technical speaker you have seen?

Answer

Latest Features

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.