Skip to content

Improve NotificationCenter speed and usability #4414

@aleks-f

Description

@aleks-f

Is your feature request related to a problem? Please describe.
Currently, the NotificationCenter calls (N)Observer callback in the same thread where the subscribed-to event occurs. This slows down the event collection by executing the handling functionality "inline" in the event collection loop.

Additionally, notification dispatch in (N)Observer depends on dynamic_cast to determine whether to actually post the Notification to the Observer. The downside of this approach (aside from the dynamic_cast performance hit) is that it is not possible to distinguish observers by any other criteria except the compile-time notification type.

Describe the solution you'd like
Decoupling event detection from handling would allow to collect event-generating data without being slowed down by the processing workload.
Adding an additional (runtime) matching criteria would relax the current constraints and only actually interested observers could be determined prior to dynamic casting and calling the handler.

  • NotificationCenter:
    • enqueueNotification()
  • NObserver (all optional, * is only to describe what it conceptually is, could also be a unique_ptr):
    • dequeueNotification()
    • NotificationQueue*
    • Poco::Thread*
    • Dispatcher* (Runnable child)
    • (*match)(const Poco::Any&) callback

Describe alternatives you've considered
Alternatively, this could be implemented as a separate pair of classes, inheriting from the existing ones (eg. ActiveNotificationCenter and ActiveObserver)

Additional context
All this should be added as a default-off option, ie. without affecting the existing functionality.
I'm inclined to deprecate the Poco::Observer in the future, eventually renaming NObserver to Observer with perhaps notifications being std::unique_ptr which are moved into the handlers.

Metadata

Metadata

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions