Sunday, July 4, 2010

CJSignals - My New Observer Framework



CJSignals project homepage

You might be able to tell from the project name that this project is inspired by Robert Penner's AS3 Signals. Yes, this is the case, indeed.

I've studied C# before, and played with Qt in the last semester. I felt the same thing Robert felt about the Event System in C# and the Signals/Slots System in Qt. It makes much more sense to extend an observer framework with composition rather than inheritance, and it makes code cleaner and more comprehensible if an object dispatches events of just one type, which is not the case for the ActionScript 3.0 native event dispatchers, for they can dispatch multiple types of events.

With CJSignals, each type of event corresponds to a Signal object "owned", a term for composition, by the subject. For instance, you can write code like this.



The parameter types of the listener matches those specified in the signal constructor. For information on the actual usage, you may check out the tutorial wiki page.

I built CJSignals from the ground up, using my own implementation of signals to boost performance, mainly performance concerning listener priority management and listener removal. I used the fast-splicing array technique I mentioned in this post, which is also employed by Rusher for active component priority management. And the priority sorting is only executed upon signal dispatch and if necessary.

Here's a comparison chart of the performance for the native event system, CJSignals, and AS3 Signals. Although CJSignals places second or third in some tests, the actual numerical results do not pose as bottleneck. What is more important is that CJSignals has the best worst-case performance: the worst test result for CJSignals is merely 13ms, whereas that for the native event system is 681ms, and AS3 Signals 5717ms.

10 comments:

aladdin said...

1. as3-signal is not a project worthing paying attention at the final. You can see the article why robert start this project, and what he had missed in the many edits that he added lately. After all, you can see the signal system is of no practical use: can't reduce code, can't write man-understandable code, and can't be efficient.

2. the event system in c# is very unreachable at first, that's why c# 2.0 has modified the whole structure. even now, I still hitted by others' code that extends the EventArgs class in the wrong way, or use a event handler with a non-void return (Mike chambers' ExternalInterfaceProxy included in the IntrovertIM sample made this mistake).

You still do some good job by writing out a mini-framework, but you can think this structure again.

aladdin said...

And another comment: yes, when you really need something like signal, we still have pureMVC at our bags. You will need message-like system at some greater application scale.

CJ Cat said...

That's weird. Blogger seems to fail to publish the comments I've approved.

CJ Cat said...

Weird. Blogger seems to fail to publish comments I've approved :(

Robert Penner said...

It's strange that I see 2 comments from you in this comment submission form, but only one of them on the blog page.

CJ Cat said...

I have no idea. The Blogger comment system seems to malfunction lately. I've seen lots of posts about the same problem on the Blooger forum. Some comments I've approved are lost forever :(

Robert Penner said...

Nice optimizations, CJ. I should point out that your tests all use DeluxeSignal. I didn't know DeluxeSignal.add() and remove() were so slow. I am more concerned about dispatch speed than add, because dispatch will generally occur more frequently. But DeluxeSignal should be better optimized; it's a bit ridiculous.

However, if priority isn't needed, then my simpler Signal adds and removes listeners as fast as CJSignals.

CJ Cat said...

Yeah, I used DeluxeSignals because I wanted to test priorities. I should have used the Signal class in the "ADD without priority" test. I'll update the test project ASAP.

Rex said...

Hi, it's great to the hear improvement on event system of actionscript. Btw... Are there any differences on performance comparing to as3-signal without priority?

Devu said...

consider this solution http://flaemo.com/blog/?p=450