user1095108
user1095108

Reputation: 14603

event system optimization

If I have these declarations and definitions:

enum Events
{
  INIT,
  RENDER
};

struct EventBase
{
  typedef void (EventBase::*event_callback_type)();

  ~EventBase() {}

  virtual void init() { assert(0); }
  virtual void render() { assert(0); }
};

template <enum Events>
struct EventTraits
{
  static EventBase::event_callback_type const event_callback;
};

// in a .cpp file
template <>
EventBase::event_callback_type const
EventTraits<INIT>::event_callback(
  &EventBase::init);

template <>
EventBase::event_callback_type const
EventTraits<RENDER>::event_callback(
  &EventBase::render);

// in another class *i are pointers to objects that inherit EventBase
template <enum Events event>
inline void EventNotifier::notify()
{
  for (events_type::const_iterator i(event_handlers[event].begin());
    i != event_handlers[event].begin() + num_event_handlers[event];
    ++i)
  {
    ((*i)->*EventTraits<event>::event_callback)();

    if ((*i)->num_event_handlers[event])
    {
      (*i)->notify<event>();
    }
    // else do nothing
  }
}

Say, that the event RENDER needs fastest possible handling, do you think it is worthwhile to do a member template specialization:

template <>
inline void EventNotifier::notify<RENDER>()
{
  for (events_type::const_iterator i(event_handlers[RENDER].begin());
    i != event_handlers[RENDER].begin() + num_event_handlers[RENDER];
    ++i)
  {
    (*i)->render();

    if ((*i)->num_event_handlers[RENDER])
    {
      (*i)->notify<RENDER>();
    }
    // else do nothing
  }
}

This would not require the fetching of a static pointer to a member function. Or perhaps I should do this:

template <enum Events>
struct EventTraits
{
  static EventBase::event_callback_type event_callback();
};

And specialize the struct template?

Upvotes: 2

Views: 219

Answers (2)

Charles Beattie
Charles Beattie

Reputation: 5949

The member specialisation won't make any difference in it's current form as the code you have written in identical to the code the compiler will generate for you.

Here's a small improvement:

template <enum Events event>
inline void EventNotifier::notify()
{
  for (events_type::const_iterator i(event_handlers[event].begin()),
       end (event_handlers[event].begin() + num_event_handlers[event]);
       i != end; ++i)
  {
    ((*i)->*EventTraits<event>::event_callback)();

    if ((*i)->num_event_handlers[event])
    {
      (*i)->notify<event>();
    }
    // else do nothing
  }
}

Upvotes: 1

Sergey K.
Sergey K.

Reputation: 25386

Just additional 5 cents. EventNotifier::notify() looks completely thread-unsafe. Futhermore, if any event handler generates new event bad things can happen. I suggest doing notification this way (C++ 11, just don't know all of your types):

template <> inline void EventNotifier::notify<RENDER>()
{
  decltype(event_handlers[RENDER]) local;
  decltype(num_event_handlers[RENDER]) local_num;

  {
    std::lock_guard<std::mutex> guard(my_mutex);
    local = event_handlers[RENDER];
    local_num = num_event_handlers[RENDER];
  }

  for (events_type::const_iterator i(local.begin()); i != local.begin() + local_num; ++i)
  {
    (*i)->render();

    if ((*i)->num_event_handlers[RENDER]) (*i)->notify<RENDER>();
  }
}

Upvotes: 2

Related Questions