define('DISALLOW_FILE_EDIT', true); Fast AS3 Signals with SignalsLite – unFocus Projects – Kevin Newman and Ken Newman

Fast AS3 Signals with SignalsLite

I was playing around and ended up writing a lite Signals class (ok, 3 classes). The set works like a basic AS3 Signal, minus most of the extra functionality of AS3 Signals (run-time dispatching argument type checking as one example). The goal was to create a very fast Signal dispatcher, with very little overhead, and to dispatch with absolutely no heap allocation (check, check and check) – targeted mostly for mobile (AIR). Regular AS3 Signals does well, but it seemed to have a lot of extra stuff that I don’t need – and this was a fun kind of exercise anyway.

Some quick numbers from the performance-test with 1,000,000 iterations on a Core 2 Duo 2.6GHz (in milliseconds):

Func call time: 15
Runnable call time: 5
Event (1 listener) time: 863
Signal (1 listener) time: 260
SignalLite (1 listener) time: 232
RunnableSignal (1 listener) time: 56

Func call (10 listeners) time: 190
Runnable call (10 listeners) time: 399
Event (10 listeners) time: 2757
Signal (10 listeners) time: 741
SignalLite (10 listeners) time: 725
RunnableSignal (10 listeners) time: 221

The bold line is a vanilla SignalLite, and the line above Robert Penner’s AS3 Signals. They are pretty close, but SignalsLite takes a modest edge. But let’s look at the same test on iOS (iPhone 4S) with 100,000 iterations:

Func call time: 171
Runnable call time: 26
Event (1 listener) time: 3723
Signal (1 listener) time: 789
SignalLite (1 listener) time: 481
RunnableSignal (1 listener) time: 117

Func call (10 listeners) time: 2004
Runnable call (10 listeners) time: 1892
Event (10 listeners) time: 9217
Signal (10 listeners) time: 4030
SignalLite (10 listeners) time: 2074
RunnableSignal (10 listeners) time: 498

On iPhone you can see that SignalLite is almost twice as fast as AS3 Signals – a more substantial difference than on desktop. I’m not sure why that is, maybe the AOT compiler can optimize something about SignalLite better – IDK, but it sure is fast!

Then there’s that last line in each group – RunnableSignal. Now your talking speed. That one also solves a particular problem with function callback systems that they all seem to have – there is no compile time function signature checking. You have to wait until the thing runs, and then find out you are taking the wrong number of arguments, or the wrong type, etc. But, solving one problem (compile time type checking), solves the other (speed), and that brings us to SignalTyped which RunnableSignal in the test above extends (I’ll probably rename at some point).

SignalTyped is beginnings of a fast executing type safe implementation of AS3 Signals. The idea is, you extend 2 classes – SignalTyped and SlotLite. SignalTyped is effectively an abstract class – you must extend it and implement the dispatch method, and the constructor (at least for now, I’m looking for better ways to handle this). It takes a bit of boilerplate to implement this in a class that would expose signals. This example is based on the performance test from Jackson Dunstan’s CallbackTest which I borrowed (I hope that’s ok!):

[sourcecode language=”actionscript3″]
// Interface for your class that might have listeners for the SignalTyped.
// Make one of these per listener type.
interface IRunnable {
function run(): void;

// Custom Slot has a specific property for the Runnable class.
class RunnableSlot extends SlotLite
public function RunnableSlot( runnable:IRunnable ) {
this.runnable = runnable;
public var runnable:IRunnable = new EmptyRunnable;

// An empty IRunnable class for first node.
class EmptyRunnable implements IRunnable {
public function run():void {};

// You need one of these per dispatch type.
class RunnableSignal extends SignalTyped
// last and first must be set to the typed Slot.
public function RunnableSignal() {
last = first = new RunnableSlot;

// implement the dispatch method to call the runnable prop directly
// It’s easy to have it take and dispatch any type you want – with compile time type checking!
public function dispatchRunnable():void
var node:RunnableSlot = first as RunnableSlot;
while ( node = ( as RunnableSlot) ) {; // FAST!

That’s all necessary for the implementation requirements – a lot of boilerplate, I admit. Then you expose that in a class that might use it all:

[sourcecode language=”actionscript3″]
class MyDisplayObject
// could probably make this a getter..
public var signaled:RunnableSignal = new RunnableSignal;

Now for the consumer to use this, it’s just a bit more boilerplate than a normal signal:

[sourcecode language=”actionscript3″]
class MyConsumerOfSignalLite implements IRunnable // boilerplate point 1
public function MyConsumerOfSignalLite()
var dspObj:MyDisplayObject = new MyDisplayObject();
// add the signal (boilerplate point 2 – normal)
dspObj.signaled.addSlot( this );

// boilerplate 3 – normal, but more strict – naming is specific – FAST!
public function run(): void {
// do whatever when signals
// boilerplates 2 and 3 are normal for any signal, except the strictness of #3

What’s cool about this is you get compile time type checking for your method signature, and the performance improvement that comes with skipping those checks at runtime.

I’m also thinking about a slightly different signal API that would be more like the Robot Legs’ contract system – think signals by contract – I’m working on it. Since we would be implementing a defined interface per signal type, we could boil the add methods and signal nodes down to one method to add all the listeners of a single object – one add method per dispatching class, instead of one per signal on the dispatching class. This could lead to a reduction in boilerplate. We’d filter by interface type instead of using multiple signal.add nodes and methods. So – improved runtime performance, reduction in (usage) boilerplate (if not implementation) and compile time type checking. I love it!

Note – I tested none of the example in this post, and the code in github is all very early stage stuff. The performance-test class works though – give it a try!

Oh, here’s the github repo:

Author: Kevin Newman

I'm the lead developer at adcSTUDIO located in Kingston NY (in Livingston Manor NY before that). I do all kinds of things there, from robust server side work to the much more enjoyable client side development in HTML/JavaScript/Flash (RIAs, HTML5, etc.) and all the other tech-buzz-phrases of the moment. My brother came up with the idea for which was originally meant be a place to discuss and blog about whatever topics we both found interesting, from politics to technology, to art and design. Time was scarce, and I need a place to host History Keeper, and unFocus Projects - a sub focus of was born, and eventually migrated to the font page. Oh, and I'm on Twitter (@Touvan) and Google+.

7 thoughts on “Fast AS3 Signals with SignalsLite”

  1. Hey it’s cool Thanks.
    I just see your post on haXe google group too.
    A haXe port of SingnalsLite can be nice too, that’s true

    1. Yes exactly. I actually used Jackson’s research (in his “Callback Strategies” – the previous post to his TurboSignals post) to implement SignalsLite, and modified his Callback test – specifically the Runnable example – to test out some ideas.

      The difference between TurboSignals and a signals by contract paradigm that I’m working on, is TurboSignals requires you to implement a method called onSignal0, and the arguments passed to dispatch are not typed, where I’m looking to define a formal contract between the class that sends signals, and the consumer. This will allow more appropriate signal names, and compile time argument type checking. Of course, just using onSignal0 is easier (as is using SignalLite), but it’d be nicer to define the signal handlers descriptively (onMouseClicked, etc.).

      I will cop to having missed the TurboSignals post until I was pretty far down this rabbit hole though (otherwise I might have just started from that code base). My original intent was to just make a very small Signals implementation (SignalLite and SlotLite), but I couldn’t resist chasing down even greater performance wins. 🙂

  2. This example is based on the performance test from Jackson Dunstan’s CallbackTest which I borrowed (I hope that’s ok!):

    That’s more than OK! 🙂

    I’m interested to see where the strong typing takes you. The biggest downside (IMO) to TurboSignals is what others are mentioning: the need to name a public function onSignal0.

    Perhaps, as John Suggests above, a port to HaXe would help out somewhat. For example, using a “Function” type is replaced rather cleanly with a “SignalArg0,SignalArg1->Void” type (change, add, or remove types as needed) and they also support generics/templates through “type params”:

  3. Hi Jackson! I don’t know where this will end up – it takes so much boilerplate to set this all up, and then I’m not entirely sure how well the interface/contract model will be received (I may try working that into Backstage2D if I’m ever able to make any progress on that – it’s a hacky unfinished mess right now – all hobby work so far).

    I did add something like a GenericSignal test in the repo since this post that performs pretty well on AVM2 based runtimes (but not on iOS) which takes advantage of that “as” performance enhancement:

    As far as HaXe goes – I just haven’t had the time to really dig in. Some of the questions I would have revolve around what the various features actually compile down to – for example I know (think) that some features like typedef (which other features are based on) are strictly compile time features, and don’t necessarily lead to runtime performance boost – though like I said, I haven’t had the time to dive in and learn the ins and outs. Type params though seem awesome – like generics in C#. I can think of many uses for such a feature.

Leave a Reply

Your email address will not be published. Required fields are marked *