Archive for category AS3

Weak References

Tags: , , ,

Weak key support in the Dictionary class is one of those rarely-used features that can be greatly useful on occasion. This is the only place in the Flash API where weak references are used. In Java, there is another useful class for when you just want to make one weak reference, not a whole table of them: the aptly-named WeakReference class. Below is my own implementation, which is both simple and useful.

Read the rest of this article »

4 Comments

Curry Functions

Tags:

In the midst of discussing my runnables technique for implementing function pointers over on the Rausch Generator Blog, I commented about the usefulness of the currying technique, mostly in callbacks. It occurred to me that I’ve been using a tiny helper class to curry functions for years in both AS2 and AS3, but that many people may have never stopped to write such a basic (in my opinion) class. So I figured I might as well re-implement the class and post it here.

Read the rest of this article »

7 Comments

Case Statements

Tags: , , ,

The lowly switch statement and its attendant case statements is a basic element of most C-style languages. Still, I was surprised by it recently when it seemingly ate one of my functions. Read on to see how.

Read the rest of this article »

7 Comments

Runnables as Function Pointers

Tags: , , ,

Last Friday’s article expressed some longing for C-style function pointers. It attempted to use AS3’s Namespace class to fake a function pointer. Unfortunately, this resulted in far slower code than simple direct access. Today’s article shows a technique that actually results in far faster code!

Read the rest of this article »

16 Comments

XOR Swap

Tags: ,

The XOR swap algorithm is an old programming trick. I’ve never personally seriously entertained using it, even in C, but myths abound that it is somehow faster than simple swapping via a temporary variable. Is it fast or just fancy?

Read the rest of this article »

7 Comments

Namespaces As Function Pointers

Tags: , ,

One lovely trick C/C++ programmers can use is to replace conditional logic (eg. if statements) with function pointers if the result of the conditional logic is a loop invariant. Function pointers do exist in AS3 in the form of the Function class, but they are a dynamic class whose usage is very slow. AS3 also supports Namespaces, which can be used as a function pointer substitute. Are they any faster than directly using Functions? Read on for a quick test.

Read the rest of this article »

5 Comments

Free Lists

Tags: ,

The garbage collector (GC) in Flash Player 9 and 10 is a notorious source of performance problems. This is caught in the Flex Builder profiler as [mark] and [reap] steps as the GC marks objects to free and then actually frees them. Additionally, the actual allocation of new objects is slow. Today’s question asks “can we improve performance by managing some of our own memory?” This article shows one way to take a little control of memory management. (SPOILER: this technique can yield a 2100% speedup!)

Read the rest of this article »

7 Comments

Constructor Slowdown?

Tags:

I’ve heard from several sources that code in a constructor executes slower than code outside a constructor. So I decided to do a test. How much slower are constructors?

Read the rest of this article »

9 Comments

Sorting Vectors

Tags: , , ,

The Array class has a great function: sortOn(). It does a fast sort based on a property of each element of the array. Unfortunately, there is no equivalent in the Vector class. Below are some attempts to get around that limitation and preserve as much of the speed of sortOn() as possible.

Read the rest of this article »

16 Comments

Adding to Arrays and Vectors

Tags: , ,

Adding on to existing arrays and vectors is one of those really common tasks that sounds dreary. Everyone knows about push() and unshift() for single elements and concat() for lots of elements. But what if you want to add a lot of elements to an existing array or vector without allocating a new array or vector? I have the solution.

Read the rest of this article »

5 Comments