Multiple handlers for the same event

Hi,

Sorry for another beginner question, but related to Events, would there be any (major) performance disadvantages in calling multiple handlers for the same event?

Something like this:

void Start() {
    SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(App, HandleUpdate));
    SomeMethodSomewhereElse();
}

void HandleUpdate(StringHash eventType, VariantMap& eventData) {
    // HandleUpdate instructions
}

void SomeMethodSomewhereElse() {
    SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(App, OtherHandleUpdate));
}

void OtherHandleUpdate(StringHash eventType, VariantMap& eventData) {
    // OtherHandleUpdate instructions
}

Instead of this:

void Start() {
    SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(App, HandleUpdate));
}

void HandleUpdate(StringHash eventType, VariantMap& eventData) {
    // HandleUpdate instructions
    // OtherHandleUpdate instructions
}

Thanks in advance.

I did a test with a simple app and managed a couple million separate events per second. There shouldn’t be a noticeable impact from multiple subscriptions vs a handler hub.

1 Like

Event handlers are relatively cheap, so it won’t be a problem unless you have a lot of event handlers.

As a side note, I don’t recommend to rely on the order of event evaluation.
So you may want to use single event handler if you want to explicitly order execution.

2 Likes

In my tests the bulk of CPU time for sending an event is in allocating members for the variable processed in SendEvent(). However, only one member is allocated for each separate object (event receiver), even if that object has multiple handlers, so my educated guess is calling multiple handlers for a single receiver is relatively cheap compared to the base cost of handling the event.

1 Like

@vmost and @Eugene, thank you very much for the answers, tests and explanations! I was concerned to start on the wrong foot there and end up having critical problems down the road. @Eugene thanks for reminding about the order of execution, I wasn’t even considering that. @vmost thanks for taking the time to test it, I really appreciate it. Thanks again guys!