7.27.2010

JITing in the Flash VM

Three things that came to mind recently:

* Constructors should be as short and fast as possible -- don't do any computation in them. Flash never JITs the constructor function, it's always fully interpreted. (There are performance tests to show this, though I know it for fact.) If you must do some work in the constructor, or the constructor will get called often (e.g. not a Singleton) -- write a private function to do the work and call it from the constructor.

* If you are using an Array and indexing it numerically (i.e. not an associative array -- one that uses string indexes), you can improve access speed to the array by hinting as follows:
array[int(idx*2+1)]
Even if idx is already an int, the type coercion sends a hint to the JIT.

* Finally, not so important, but in tight loops or ones with many iterations: Best practice is to cache the length of an array to a const rather than inlining it.

It's been suggested to me that iterating over Arrays backwards is faster (and you can save a lookup by simply decrementing the index each iteration until it is -1 rather than testing for equality against the length). Though it very well might a) not be true b) be JIT/platform-dependent. It's confusing as hell, so I'd only suggest doing that if you're already doing something impossible-to-understand like dynamic programming or have shown benefits on at least one platform.

No comments: