mozilla/shumway — pull request #438 by tschneidereit (2013-07-21 20:24:03+00:00)
Title: Optimize events

## TL;DR:
- Event broadcasting is *heavily* optimized, reducing the number of handler invocations by ~99,5%(!)
- constructFrame handling in MovieClip is optimized, reducing the number of constructFrame handler (and thus `MovieClip::_gotoFrame`) invocations by something between 40% and 60%

## Longer version:
The first of the attached patches does two things:
- it changes the SystemDomain's broadcaster from a "broadcast everything, filter in the handlers" setup to a "register handlers for and broadcast named events" one. As we're broadcasting 4 to 5 events during each rendering loop, this reduces the number of handler invocations by anywhere between 20% and 80%
- it changes MovieClip to only listen to broadcasted events if it has listeners for those events, itself. This is the killer change that removes almost all events: `frameConstructed` and `exitFrame` are almost never used, `enterFrame` is comparatively rare

The second patch deals with `constructFrame`, which each playing MovieClip listens to. It reduces the number of listening MovieClips by making 1-frame clips not listen. Those clips that still listen only mark themselves as dirty if advancing to the next frame actually changes anything.

## Numbers
I tested with the Mushroomer game's intro, taking numbers for the loading and initialization process and then for everything up until the end of the intro:

Load Mushroomer, let it run into "click anywhere to start" has blinked 10 times
old:
```
callback.notify: ~1,450,000
instances: ~30,100
constructFrame: ~16,450
```
new:
```
callback(constructFrame).notify1: 6,366
callback(enterFrame).notify1: 647
callbacks combined: ~7,000 (-99,5%)
instances: ~20,500 (-32%)
constructFrame: ~6,600 (-60%)
```

click on Mushroomer stage, let it run until the intro has finished and all leaves have vanished
old: (average of two runs)
```
callback.notify: ~5,800,000
instances: ~138,000
constructFrame: ~75,000
```

new:
```
callback(constructFrame).notify1: ~31,500
callback(enterFrame).notify1: ~10,000
callbacks combined: ~41,500 (-99,3%)
instances: ~90,000 (-35%)
constructFrame: ~31,500 (-42%)
```