1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Please Use Event Hints

Discussion in 'XenForo Development Discussions' started by Snog, Sep 8, 2014.

  1. Snog

    Snog Well-Known Member

    I just spent a good portion of a day going through add-ons that are being run on a low power server to see if I could speed things up a bit.

    What I found was almost scary. :)

    I found a good portion of the add-ons using code something like this in their listeners...

    public static function loadClassModel($class, array &$extend)
      if ($class == 'XenForo_Model_Conversation')
      $extend[] = 'XXXX_Model_Conversation';
      else if ($class == 'XenForo_Model_Like')
      $extend[] = 'XXXX_Model_Like';
      else if ($class == 'XenForo_Model_AddOn')
      $extend[] = 'XXXX_Model_AddOn';
    The way that's coded and entered into the listeners, that code runs on every single page load.

    That really isn't bad if the only add-on running on the server is that one. But, when you multiply that by 30 to 40 add-ons it really starts accumulating.

    If there were separate listeners for each one with the event hint filled in, they would only run when needed.

    I was able to trim almost a full point off the server load by putting event hints in a good portion of the add-ons. But I wasn't able to hit them all.

    Now I'm just as guilty as the next guy and sometimes I fail to use the event hint. But, I think we should all be thinking of them when we code. ;)
    NixFifty, Aayush, Xon and 2 others like this.
  2. Adam K M

    Adam K M Active Member

    I agree with this, although I do wish that multiple event hints were allowed per class. For me the issue of using event hints is not so much as forgetting them, but deciding against making multiple listener classes (I guess I might just be lazy). This might be slightly redundant to the purpose of optimizing things, but whatever.

    Sounds like you're running a pretty specialized forum, my forum doesn't even have more than 6 active addons at one time. haha
    Xon likes this.
  3. Snog

    Snog Well-Known Member

    I've got all of my own add-ons active on my site.

    But, it's not my site that I was working on. It's a server I manage for a client. He has 30 or 40 add-ons installed.
  4. HWS

    HWS Well-Known Member

    Since event hints are a rather new feature of XenForo all add-ons started some time ago and not refurbished until now won't use them.
    Aayush and Jeremy P like this.
  5. Xon

    Xon Well-Known Member

    You can make multiple events hints hit the same class, and even the same method. The real cost of event hooks is the function call, not the class name check.

    But for the love of all that is holy, use a switch statement; not a if-else forest.
    NixFifty likes this.

Share This Page