Regular expressions are downright handy for a variety of string processing tasks. They are so handy that they are built straight in to the language in the form of the /regexpgoeshere/ syntax. Well, usually…
Weak References
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.
Curry Functions
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.
Case Statements
Runnables as Function Pointers
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!
XOR Swap
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?
Namespaces As Function Pointers
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.
Free Lists
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!)
Constructor Slowdown?
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?
Sorting Vectors
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.