Recently we’ve been working on a project for a limited device and have been concentrating on the performance aspects of the application. We use signals quite extensively in the project application (to good effect!), but it does intrigue me when you come to any stage of application development, that there are certain things you know about the application and what it does and when. When looking at Signals specifically these things are:
- You know when something needs to listen to a Signal
- You should know how many times your registering to said Signal
I’m more interested in number 2 for now. As the application is ironed out over time, it makes me wonder why we’ve got this extra functionality of allowing multiple listeners to registered to a Signal, when only one suffices. I’ve never been a fan of hooking on to direct callbacks, mainly for the reason that you don’t know what is coming through as the arguments and the added bonus of reading the documentation every time. I’m more interested in self documenting code, not that I’m against documentation! I just want to be able at a glance to understand what the code does (where possible of course).
I had thought about implementing a SingleSignal. The whole purpose of this is to provide away to have a one listener at a time callback. The SingleSignal had to satisfy the following:
- Only register one listener at a time
- Notify the developer if they tried to register more than one listener
- Be faster than the default Signal, as we’ve not got the over head of the linked list and Dictionary
- Use less memory than the default Signal
It didn’t take me long to implement the functionality of the SingleSignal, as I already had the excellent work of Robert Penner and Joa Ebert to start from. The current implementation is in my github repo. I’ve also created some unit tests to help improve the robustness and prevent errors slipping through.
The last thing I did was verify the performance using the existing performance testing in the repo. Here are the results from a release Flash Player (10.1r85.3) on a mac running 10.6.2:
AddOncePerformance
Note: All tests min time was 0ms for all.
| DeluxeSignal | Signal | SingleSignal |
|---|
| Frames per second (fps): | 49fps | 60fps | 60fps |
| Memory used: | 6mb | 6mb | 4mb |
| Max time: | 8ms | 7ms | 4ms |
RemovePerformance
Note: All tests ran at 60fps and the min time was 0ms for all.
| DeluxeSignal | Signal | SingleSignal |
|---|
| Memory used: | 6mb | 6mb | 4mb |
| Max time: | 6ms | 6ms | 4ms |
There you have it. It’s up to 33% faster, which won’t be that noticiable, depends on how many addOnce or add and dispatches you’re doing. It also uses 2mb less, which is a good improvement overall. So if you know what the restrictions are of your application and you know you’re going to add one listener and you want to use less memory then a SingleSignal might be for you…
EDIT: slight refractor to make the article more concise.