Why use the FastDelegate library for Events?

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Why use the FastDelegate library for Events?

      Hi,

      Firstly, great book (I've read both the 3rd and 4th editions cover to cover).

      I have one question however regarding the Event Management System and its use of the Fast Delegate library.

      It appears that you're using the Fast Delegate library as a way of calling member functions on objects.

      If so then would it not be better to just use std::bind and std::function to do the same thing?

      For example you could do the following:

      typedef std::function<void (IEventDataPtr)> EventListenerDelegate;

      Then in your EventManager you'd define the list as before:

      typedef std::list<EventListenerDelegate> EventListenerList;
      ... and so on ...

      Anything that calls VAddListener would construct the delegate parameter using std::bind, for example this:

      EventListenerDelegate delegateFunc = MakeDelegate(this, &RoleSystem:: DestroyActorDelegate);

      would become this:

      EventListenerDelete delegateFunc = std::bind(&RoleSystem:: DestroyActorDelegate, this, _1);

      I'm guessing that you didn't do this because this wasn't (Officially) supported until C++11?

      Now that C++ does support this is there any reason why we should still use the FastDelegate library over the C++11 method?

      Thanks
      Ben
      P.S. I'm guessing that with the 80+ hour weeks you're working at the moment that there's not much chance of a Fifth Edition of the book any time soon?

      The post was edited 2 times, last by BenS1 ().

    • I can't speak for Mike and Rez in terms of the book, however you are right that about the support. Visual studio 2010's support of C++11 features are pretty slim, the most it supports is a partial coverage of the features when it was C++0x.

      Now, I can speak for how well std::function links in with various other parts of the C++11 standard (some of the things you can do with it are really awesome), but as for the performance vs the FastDelegate library I am unsure. Generally STL stuff is known to trade off performance for generalization. I haven't seen any benchmarks but I bet FastDelegate is very fast in comparison.
      PC - Custom Built
      CPU: 3rd Gen. Intel i7 3770 3.4Ghz
      GPU: ATI Radeon HD 7959 3GB
      RAM: 16GB

      Laptop - Alienware M17x
      CPU: 3rd Gen. Intel i7 - Ivy Bridge
      GPU: NVIDIA GeForce GTX 660M - 2GB GDDR5
      RAM: 8GB Dual Channel DDR3 @ 1600mhz
    • For what it's worth, I also use std::function. Just because I like limiting my use of third party libraries unless there is a very convincing reason to include them.

      I believe the reference in the book to the fast delegate library has information on why it is necessary. I, uhhh, skipped that part. I'm sure its a very interesting read though!
    • Thanks! I'm glad you enjoyed the book!

      Yes, I cover why I chose to use fastdelegate in the beginning of the Event chapter. I didn't use std::function because it wasn't standard at the time.

      If I were to do it over, I would certainly evaluate the use of std::function. fastdelegate is nice because of the very small performance overhead. I don't know what the overhead of std::function is, nor do I know if it fully meets my requirements. If the performance and behavior are comparable, I would probably ditch fastdelegate in favor of std::function if for no other reason that to remove the third party dependency.

      Still, as third party dependencies go, it's pretty light. There's just a single header file rather than a lib you have to link in.

      We currently don't have any plans for a fifth edition. If we decide to ride that velociraptor again, this will be the first place we post about it. Once the 4th edition was decided upon, we took a poll here to see what topics people wanted us to cover. I imagine we'd do the same thing.

      -Rez
    • Thanks all for your replies,

      column_vector, thanks for the code... looks like what I was thinking.
      Note that you can effectively compare 2 std:functions for equality using the std::function::target. For more info see here:
      std::function::target

      With regards to performance, I would have assumed that std::function would have been at least as fast as the FastDelegate library as the FastDelegate library was trying (And succeeding) in implementing delegate functionality without direct compiler support, whereas std::function and std::bind are using new compiler level functionality to implement the same (Similar) functionality.

      Still, I wouldn't be comfortable to bet money on which one is faster in reality. If I get time I might do a quick benchmark comparison.

      Thanks again
      Ben
    • Originally posted by BenS1
      With regards to performance, I would have assumed that std::function would have been at least as fast as the FastDelegate library as the FastDelegate library was trying (And succeeding) in implementing delegate functionality without direct compiler support, whereas std::function and std::bind are using new compiler level functionality to implement the same (Similar) functionality.

      Still, I wouldn't be comfortable to bet money on which one is faster in reality. If I get time I might do a quick benchmark comparison.

      This is not a safe assumption. The STL needs to solve the most common concerns, which aren't always performance. Look at new and delete. They are great, general-purpose allocators but they're not optimized for performance. At Maxis, we have our own internal version of the STL called EASTL, which you can find here:
      open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html

      This was written with performance in mind and outperforms Microsoft's STL in pretty much every way. By contrast, fastdelegate was written from the ground up with perfomance in mind while std::function probably wasn't.

      Still, I haven't done any tests so I could be completely wrong here. :) If you end up doing some bench tests, you should post your results here. I'd be very curious to see what you find out.

      -Rez
    • FastDelegates definetly is FASTER than std::function, at least in VS2013 with november CTP 2013 and is also fastest compared to VS2012. Main reason is std::function performs several heap allocations. I did several tests but can not find them right now. You can google a bit, I'm sure you will find good posts in stackoverflow

      If you don not need to use lambdas which store states, definetly use fastdelegates...

      If you are not concerned with speed gainings and do not feel comfortable using fastdelegates, then use std::function.