12.05.2011

Recompress your Flash 11 SWFs with LZMA

Flash 11 can load SWFs that have LZMA compression; previously SWF contents were either uncompressed or zlib-compressed.

Since the mxmlc compiler doesn't yet support this compression, and since it was a PITA to compile Tinic Uro's zlib2lzma on anything but Windows (http://blog.kaourantin.net/?p=124), and since no one else seems to have done it... I wrote a Python script to do the conversion. Woot. It only requires that the input SWF be compiled with -swf-version=13 (or higher). It will fail if you feed it an LZMA-compressed SWF -- feel free to fork me on GitHub and fix that.

Usage: python swf2lzma.py in.swf out.swf

Grab the code from: http://github.com/jspiro/swf2lzma

7.19.2011

build decay chronograph

After hours of coding and reloading, I constantly forget whether I reloaded a browser tab under development, and otherwise question whether the SWF actually got rebuilt (and am often wrong on both counts).


Enter the BuildDecayChronograph, an object that shows you how long it's been since a given SWF was last built, and a quick visual indicator from green to red of how fresh it is (for me, 5 minutes). The only requirement is that the given SWF was compiled with mxmlc.

Usage:
addChild(new BuildDecayChronograph(5*60, 'engine'));

Download from GitHub.


This could not have been done without the brilliantly hacky org.igorcosta.hacks.SWF
Blog Text and Codes (Igor Costa ) / CC BY-NC-SA 3.0

7.14.2011

unable to transcode this meaningless error message

If you're compiling with mxmlc on Linux, and you get the 'unable to transcode' error message, and you're transcoding hundreds of files... and you've wasted two days on this (no, I'm not bitter):
>: ulimit -a
That may be really small. Like 1024 files. Try compiling again after:
>: ulimit -n 5000
Better? mxmlc swallows the Java IO error #fml >_<

9.30.2010

Tailing Flash

>: tail -f /Users/jspiro/Library/Preferences/Macromedia/Flash\ Player/Logs/flashlog.txt

How did it never occurred to me to tail with Flash before? Shameful.

Though, really, I wish I could control who gets to write to that log, or identify who IS writing to it -- there's so much spam from other running SWFs.

9.17.2010

Dragging's a Drag

For reasons that are beyond my comprehension, Flex List components have a property to allow moving of list items, but not one for copying.

If you allow moving, then you SURELY must also want to allow a poor implementation of copying, whereby your object is deep copied rather than cloned via some interface, putting Objects into your dataProvider rather than whatever class you were using. Also, copying is enabled by holding the control key -- not obvious to me, at least.

package
{
import mx.events.DragEvent;
import mx.managers.DragManager;

import spark.components.List;

/** Disables copy-dragging on List */
public final class NonCopyableList extends List
{
 override protected function dragDropHandler(event:DragEvent):void {
  event.ctrlKey = false;
  event.action = DragManager.MOVE;
  super.dragDropHandler(event);
 }
 
 override protected function dragOverHandler(event:DragEvent):void {
  event.ctrlKey = false;
  super.dragOverHandler(event);
 }
}
}

You're welcome.

7.27.2010

Best Practices when Listening

* Try to always use a weak listener, even though it's extra typing. The classes of bugs you are exposed to with strong listeners are mostly running out of memory, taxing the garbage collector, and killing performance as you take memory off the heap -- they creep up and are hard to detect. The classes of bugs with weak listeners are functionally worse -- but easy to detect -- stuff just doesn't work because the listeners got GCed (they are a little less performant too, due to tracking their weak-ness).

* When writing weak listeners: Make sure that objects you are listening on will not get prematurely garbage collected and are attached to something -- either the class, or some static Singleton.

* Make sure the object will get garbage collected eventually and write a comment about it.

* Assume that all weak listeners and objects will never get GC'd and will keep listening forever -- code for this. In other words, explicitly remove listeners when it is IMPERATIVE that you do not receive any more messages, do not rely on setting a parent to null to break the cycle.

* You DO NOT have to remove weak listeners if it doesn't matter if they continue to receive events until they GC -- e.g. a mouse move handler -- once you've removed the object from the display list, it doesn't matter that this handler MIGHT run. If you code your handlers to guard against runtime errors, then you never have to remove weak listeners. And if the listeners are still receiving after a long while... then you have a memory leak and the object you are listening on is not getting GC'd (see this post).

* If you are going to skip removing a listener, DOCUMENT IT at the line where you add the listener. If you won't document it, then don't do it.

* This is tricky stuff, so treat listeners with respect like you would NEW and DELETE in C++. Work them out in your head -- document the hell out of them. They are expensive and cause almost all memory and (many) performance leaks.

Random() Performance Tips

* Perceived performance is more important than actual performance, and is cheaper to implement. Learn about how to fake speed in progress bars, and never, ever, ever, ever let Flash lock up while doing processing -- break work across frames and do it asynchronously -- keep the UI responsive even if it's modally locked.

* Initializing variables to null is unnecessary, costly, and distracting (when you're trying to read code -- null or zero unless otherwise specified).

* Eliminate "this." wherever possible -- this may be outdated, but in the past it produced better bytecode.

* Don't use coding conveniences like lots and lots of extra variables, WITH blocks, extra "this" statements. They all add space and time in AS3 even though most other languages would compile down equivalent statements to the same thing.

* Long package and class names get stored in the SWF -- interned strings in the constant pool are a large percentage of SWF size.

* Keep lazy initialization of datastructures to a minimum (e.g. if (!foo) foo = new Foo() versus private var foo:Foo = new Foo()) -- class init and static init code is smaller and only runs once. The only advantages to lazy init are for data structures that are expensive to keep around if not being used, and/or you will null them periodically.

* Function arguments and local variables are cheapest to use because AS3 doesn't have to search up the scope chain when you access them. Global statics are awesome -- though caching their values locally is best if you're going to reference them a lot. Deep lookups like foo.bar.baz.getterFunction() are expensive if you're going to do a lot of work in baz -- try caching foo.bar.baz first.

* Anonymous functions: Don't get me started ;) See this post.

* If you use Flex, grep -r validateNow -- and eliminate them unless absolutely necessary. And if you don't remove it, leave a paragraph on why it must be there.

* Getter functions cost more than versus get() functions; and they often have hidden side effects. Don't write a getter unless that's all it does, and in constant time.

* Mark ever variable as const unless you need it to be variable. One day there may be a performance benefit, like finals in Java. But until then, bask in one fewer category of bugs.

* If you're embedding gobs of XML (damned if I know why you'd need to do this): Pasting the XML into a class as a static XML object offers the best performance and compression. The compile-time processing and optimization is significant versus runtime parsing, type checking, and decompression in a ByteArray (if you compressed it on the wire)