`

Introducing TurboSignals

阅读更多
http://jacksondunstan.com/articles/585

My last article on Callback Strategies highlighted some pretty severe performance differences between my Runnable strategy, as3signals by Robert Penner, and Flash’s native Event system. My simple Runnable technique had an artificial advantage though: it was not a proper library but instead a little bit of code built right into the test app. Today I’m introducing TurboSignals as an AS3 library making use of the Runnable technique.


BASICS
TurboSignals is a simple library. It includes signal classes for up to ten parameters (tt>Signal0, Signal1, ..., Signal10) as well as a class for var args (SignalN). These are paired with slot interfaces (Slot0, Slot1, ..., Slot10 and SlotN) that you implement in order to receive the signal's callback. The purpose of an explicit slot type is to avoid using a Function variable, which is very slow. This was shown in my article on Runnables along with how Runnables are a good strategy for avoiding that slowdown. Some helper classes (FunctionSlot0, FunctionSlot1, ..., FunctionSlot10 and FunctionSlotN), however slow, are provided for when you really want to provide an arbitrary function.

ADVANCED FEATURES
One nicety of TurboSignals is that the dispatch operation is "safe" insomuch as that calls to addSlot, removeSlot, and removeAllSlots will not affect which slots are called. One drawback though is that parameters to dispatch are all untyped (*) and therefore there is no compile-time checking of the parameters and the possibility exists that there will be type errors at runtime. This is true too in the Event/EventDispatcher system as well as as3signals, only the latter explicitly checks for this problem at runtime to give more informative errors.

USAGE EXAMPLE (FASTER)
This example runs at maximum speed, which is probably not needed for simple button clicks.

import com.jacksondunstan.signals.*;
public class Button extends Sprite
{
    public var clicked:Signal0;
    public function Button()
    {
        addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    }
    private function onMouseDown(ev:MouseEvent): void
    {
        this.clicked.dispatch();
    }
}
public class MainMenu implements Slot0
{
    public function MainMenu(button:Button)
    {
        button.clicked.addSlot(this);
    }
    public function onSignal0(): void
    {
        trace("button was clicked");
    }
}
USAGE EXAMPLE (SLOWER)
This example allows you to make your callback private and name it as you wish, courtesy of the FunctionSlot0 adapter class.

import com.jacksondunstan.signals.*;
public class Button extends Sprite
{
    public var clicked:Signal0;
    public function Button()
    {
        addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    }
    private function onMouseDown(ev:MouseEvent): void
    {
        this.clicked.dispatch();
    }
}
public class MainMenu
{
    public function MainMenu(button:Button)
    {
        button.clicked.addSlot(new FunctionSlot0(onButtonClicked));
    }
    private function onButtonClicked(): void
    {
        trace("button was clicked");
    }
}
USAGE EXAMPLE (COMPLEX)
This example runs at maximum speed with multiple buttons.

import com.jacksondunstan.signals.*;
public class Button extends Sprite
{
    public var clicked:Signal1;
    public function Button()
    {
        addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    }
    private function onMouseDown(ev:MouseEvent): void
    {
        this.clicked.dispatch(this);
    }
}
public class MainMenu implements Slot1
{
    private var __button1:Button;
    private var __button2:Button;
    public function MainMenu(button1:Button, button2:Button)
    {
        __button1 = button1;
        __button2 = button2;
        button1.clicked.addSlot(this);
        button2.clicked.addSlot(this);
    }
    public function onSignal1(target:Button): void
    {
        if (target == __button1)
        {
            trace("button 1 was clicked");
        }
        else if (target == __button2)
        {
            trace("button 2 was clicked");
        }
    }
}
PARAMETER PASSING STRATEGY
As you can see from above, functionality from alternative systems can be emulated in TurboSignals by simply adding more event parameters. The Event/EventDispatcher system's Event.target is attained by simply passing a reference to this as a parameter to dispatch. Likewise, the type field can easily be passed. You may also choose to pass objects like Event that include these too, which may help with speed as multiple arguments slow down the dispatch operation.

PERFORMANCE DATA
The TurboSignals distribution includes a suite of performance tests for TurboSignals as well as as3signals and Event/EventDispatcher. Here are the results for the first version:

TurboSignals - 1 Listener (1000000 dispatches)

ENVIRONMENT 0 1 2 3 4 5 6 7 8 9 10 N
2.2 Ghz Intel Core 2 Duo, 2GB, Mac OS X 10.6 56 91 98 97 101 90 94 107 104 116 119 1917
TurboSignals - 10 Listeners (1000000 dispatches)

ENVIRONMENT 0 1 2 3 4 5 6 7 8 9 10 N
2.2 Ghz Intel Core 2 Duo, 2GB, Mac OS X 10.6 317 600 570 585 567 606 582 562 580 614 628 7679
TurboSignals - 1 Function Listener (1000000 dispatches)

ENVIRONMENT 0 1 2 3 4 5 6 7 8 9 10 N
2.2 Ghz Intel Core 2 Duo, 2GB, Mac OS X 10.6 317 600 570 585 567 606 582 562 580 614 628 7679
TurboSignals - 10 Function Listeners (1000000 dispatches)

ENVIRONMENT 0 1 2 3 4 5 6 7 8 9 10 N
2.2 Ghz Intel Core 2 Duo, 2GB, Mac OS X 10.6 2985 3359 3449 3433 3551 3564 3534 3597 3624 3731 3862 22184
as3signals - 1 Function Listener (1000000 dispatches)

ENVIRONMENT 0 1 2 3 4 5 6 7 8 9 10 N
2.2 Ghz Intel Core 2 Duo, 2GB, Mac OS X 10.6 954 1158 1326 1418 1534 1696 1818 1951 2055 2467 2740 n/a
as3signals - 10 Function Listeners (1000000 dispatches)

ENVIRONMENT 0 1 2 3 4 5 6 7 8 9 10 N
2.2 Ghz Intel Core 2 Duo, 2GB, Mac OS X 10.6 2568 2908 3394 3656 3918 4249 4535 4805 5354 6288 6912 n/a
Event/EventDispatcher - 1 Function Listener (1000000 dispatches)

ENVIRONMENT 0 1 2 3 4 5 6 7 8 9 10 N
2.2 Ghz Intel Core 2 Duo, 2GB, Mac OS X 10.6 n/a 4886 n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a
Event/EventDispatcher - 10 Function Listeners (1000000 dispatches)

ENVIRONMENT 0 1 2 3 4 5 6 7 8 9 10 N
2.2 Ghz Intel Core 2 Duo, 2GB, Mac OS X 10.6 n/a 33755 n/a n/a n/a n/a n/a n/a n/a n/a n/a n/a
PERFORMANCE GRAPHS
















PERFORMANCE ANALYSIS
The latest version of as3signals (as of today, 2/15/2010) goes a long way to improve performance of the Event/EventDispatcher system, especially on Mac OS X. TurboSignals goes a lot further though and nearly matches the speed of its inspiration: the simple list of Runnables. TurboSignals manages to dispatch events about 17 times faster than as3signals when implementing the slot directly and about 3 times faster than as3signals when using a Function variable to allow for a the callback to be private, named, or anonymous. That said, as3signals is itself 4-13x faster than the Event/EventDispatcher system. So if you are planning on dispatching frequently or to many listeners, you should definitely take a look at TurboSignals.

Tags: as3signals, events, runnables, turbosignals
  • 大小: 19.5 KB
  • 大小: 20.9 KB
  • 大小: 20.5 KB
  • 大小: 21.7 KB
分享到:
评论

相关推荐

    Introducing Windows 7 for Developers (pdf)

    为了帮助开发者更好地理解并利用这一新平台的功能,《Introducing Windows 7 for Developers》一书应运而生。本书由Yochay Kiriaty、Laurence Moroney、Sasha Goldshtein 和 Alon Fliess 共同编写,并由Microsoft ...

    erlang编程 Introducing Erlang

    **Erlang编程:Introducing Erlang** Erlang是一种函数式编程语言,由爱立信在1986年开发,主要用于构建高可用性、容错性和并发性的分布式系统。"Introducing Erlang"是Simon St. Laurent撰写的一本入门级教程,...

    Introducing Erlang Getting Started in Functional Programming(2nd) epub

    Introducing Erlang Getting Started in Functional Programming(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Introducing.Python英文文字版

    《Introducing Python》是一本广泛认可的Python编程入门书籍,英文版提供了全面而深入的Python语言教程。这本书适合初学者,也对有一定经验的程序员有很高的参考价值。它覆盖了Python的基础语法、核心概念以及一些...

    Introducing.JavaFX.8.Programming.0071842551.epub

    Introducing JavaFX 8 Programming provides a fast-paced, practical introduction to JavaFX, Java’s next-generation GUI programming framework. In this easy-to-read guide, best-selling author Herb ...

    introducing-java-8.pdf

    Java 8 是Java编程语言的一个重要更新版本,由Raoul-Gabriel Urma所著的《Introducing Java 8》是一本介绍Java 8新特性的指南书。本书提供了Java 8中新增的Lambda表达式和流(Streams)的快速入门,旨在帮助程序员...

    Introducing Visual C# 2010随书代码

    《Introducing Visual C# 2010》是面向初学者和中级程序员的一本经典教程,旨在引导读者深入了解Visual C# 2010编程语言及其相关开发工具。随书附带的源代码是为了帮助读者更好地理解书中的示例和实践项目,以便于将...

    Introducing Bootstrap 4 无水印pdf

    Introducing Bootstrap 4 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或...

    Introducing Visual C# 2010

    《Introducing Visual C# 2010》是关于C#编程语言的一份详细指南,主要针对Visual Studio 2010版本。这本书深入浅出地介绍了C#的基础概念、语法以及在实际开发中的应用。C#是微软公司推出的面向对象的编程语言,广泛...

    Apress.Introducing.Spring.Framework.A.Primer.

    This book is an introduction to the well-known Spring Framework that offers an inversion of control container for the Java platform. The Spring Framework is an open source application framework that ...

    python英文原版教程《Introducing_Python》

    1. Python教程《Introducing Python》是一本面向初学者及高级学者的Python学习资源。这意味着这本书适合不同经验水平的学习者,从完全的编程新手到有一定编程背景的读者都能够从中获益。 2. 书籍作者为Bill ...

    Introducing Starling Building GPU Accelerated Applications epub

    Introducing Starling Building GPU Accelerated Applications 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    introducing_eventstorming.pdf

    这本书《Introducing EventStorming》由Alberto Brandolini撰写,介绍了事件风暴技术的具体应用方法和案例,适合于对领域驱动设计(DDD)和大型系统建模感兴趣的读者。 事件风暴工作坊通常包括以下几个阶段: 1. ...

Global site tag (gtag.js) - Google Analytics