Clearing the state of a actionscript 3 app (aka, globals are eeevil)

Posted by Chuck Vose Tue, 16 Feb 2010 22:53:00 GMT

Cross posted on the Metal Toad Blog

Imagination time: Imagine for a moment that you have an app, it has one button and one little window. When you click the button it changes the text within the window to something else. Now, when you get into flash development it seems like the easiest and clearest way to do this is to wipe out the contents in the window, but you would be wrong for thinking so. While it is the most obvious I intend to prove to you that to do so is both slower and prevents you from turning on the juice later with caching.

On to story time: We’re building an app for the OpenPeak tabletop device, it’s totally rad and you’ll love it I promise but that’s really not the point of the post. When we started the app we had what I considered a pretty awesome solution, we’d clear the state, build it onto a global, then write the global out to a window on the app. It actually worked great for a long time until we decided to start doing some caching where it became wildly apparent that I had actually written all three parts of that app dead wrong. It’s pretty exciting to do something wrong enough that you can write about it later. :)

Building on Globals (Horrible)

Okay, lets start with the most awful part of the lesson for me, that using globals is horrible and should never be done. Oh sure, they make certain listeners easier but it really does blow up fast when you start trying to cache things or thread. Here’s two scenarios that will inevitably bite you if you start using globals:

  • When you start threading you’ll end up painting two scenes on to the global stage object at once.
  • Even if you try to be synchronous you have to descend into an event listener hell since AS3 often decides to not dispatch Event.COMPLETE which means you’ll end up with a deadlocked app while a URLLoader tries in vane to do nothing.
  • At some point you’ll accidentally use the same URLLoader twice or some other critical variable and the results will be completely random seeming as one gets overwritten on occasion.

So, how can you deal with this? As annoying as it is, define your variables at the beginning of your functions. I know it seems like a waste, but unlike that one global TextFormat object your URLLoaders really are totally different beasts and need to be treated as such. Same goes for almost all objects and display containers. I know it seems nice and elegant to use the same object all the time but what you may not be seeing is that all those globals need to have IPC methods defined on them or they’re completely useless in the inherently async world that is AS3.

Clearing the State (Wrong)

This one is simple really, I should have thought of it to begin with it but I wasn’t in OO space yet. Rather than thinking about pages or DisplayObjects as something physical that needs to be removed from the stage it’s much, much more efficient to just build a new Sprite() and replace the old stage that you were using. Best part is that when you just replace the display container you’re using you can easily stash this away in a cache object somewhere. Simple caching is only moments away but if you wipe the slate clean not only did you spend time killing all those children objects, you lost the ability to cache it away somewhere.

Metaphorical you of course, my apologies.

So what we did was to use an Object (we should have used a Dictionary but I didn’t know any better yet) with attributes that described the path we were writing to. We never wrote to a global stage object (we actually deleted those so we couldn’t). This way async calls could continue to write to the correct Sprite even in the background, the only place we needed to put a lock around was the drawing part as we drew out the content but rather than drawing a global DisplayObject we took the data from the cache and drew that which meant it was much, much faster, handled multiple threads just fine, and was extremely cacheable.

Conclusion

I hope this gives you some ideas; my hope was merely to get people thinking about two things: global objects require IPC, and that wiping the slate clean is wasteful. Let the garbage collector take care of clearing memory and make sure that you have only a few global objects.

Next time I’m going to talk about how I got a requirements engine running which would allow me to ensure that certain steps were always completed before anything else without creating messy spaghetti code. I think it’s pretty fantastic and I hope that you do too.

Posted in ,  | Tags , , , , , ,  | no comments

Short interesting proverbs

Posted by Chuck Vose Thu, 16 Apr 2009 13:15:00 GMT

Tale of three monks

In the beginning there was one monk on a mountain. There was a lake at the bottom of the mountain but his studies were at the top. When he was thirsty, and he usually was from carrying the water so far, he had to hike down and carry the heavy bucket all the way up the mountain.

At some point a second monk arrived and they lashed the bucket to a bamboo rod and balanced it between their shoulders. Their studies improved and they were less tired.

Thinking that more of the same would be a good thing when a third monk arrived they rejoiced but found that there was never any water when they wanted it. Everyone trekked down the mountain at some point but there was always some conflict about who would stay and rest while the other two hiked.

This is a loose telling constructed from what Ben told me. It may have missed some of the points but it came up when we were talking about why the dishes never get done. I believe the numbers are approximate and pretty much always work for numbers > 1. We see this all the time when we live alone versus living with one or more others.

What is the moral? Find a way to lash the task to a bamboo rod so that it is easier no matter how many people are in the house/group. When everyone works together at once there is no conflict. Of course this doesn’t scale at all unless by some fluke everyone in the house is home and not exhausted at the same time (a rarity in most college houses I believe).

There’s a lot of depth to this, lots to think about.

-Edit-

Ben suggests that maybe the answer is to sign a contract or kick one of the monks off the mountain. Watching him smile after he’s launched a very successful joke is just about the best thing I can think of.

Warm feet, cool head

This was something that Ben hadn’t heard of but when I told him about it he said that it made perfect sense considering the Chinese views on foot warmth.

The meaning is roughly that the way to success isn’t through rash actions but can be achieved through calmness and good self-care (self-care is a major part of Chinese life). The feet are the locus point for many acupuncture points and as such if they are cold perhaps your organs are suffering.

Interestingly enough, my Econ teacher independently confirmed the value of good foot care recently as well. He mentioned that in pre-college and in many areas of life sandals are considered poor form, not just because of the obvious fashion formality problems, but also because it shows a lack of respect to the person speaking. If you don’t take care of your organs then you can’t pay attention which means you don’t really care enough. So sandals are a sure-fire way to piss off your teacher and show them just how you feel!

773861 army

This isn’t a proverb, just interesting. When people started migrating to the cities for increased wages they say that the only people left in the towns were members of the 773861 army.

  • 77 is a symbol for old people. Possibly related to the first day of the second Sino-Japanese war but I’m not sure about that. Ben says this is a plausible explanation but he’s also unclear.
  • 3/8 is women’s day
  • 6/1 is children’s day

Conclusion

China is cool. All those things about getting in trouble because of some weird thing are really true here. Also, never mention pork in Xi’an, there’s actually a chance that you’ll get beaten to death. It’s happened before.

Posted in  | Tags , , , , , , ,  | 2 comments