3

As of 1.9.3.0, Magento dispatches more than 300 events.

By checking the code that dispatches the event I'm pretty sure this is not memory free specially when the event data is a big object or collection:

public function dispatchEvent($eventName, $args)
{
    $eventName = strtolower($eventName);
    foreach ($this->_events as $area=>$events) {
        if (!isset($events[$eventName])) {
            $eventConfig = $this->getConfig()->getEventConfig($area, $eventName);
            if (!$eventConfig) {
                $this->_events[$area][$eventName] = false;
                continue;
            }
            $observers = array();
            foreach ($eventConfig->observers->children() as $obsName=>$obsConfig) {
                $observers[$obsName] = array(
                    'type'  => (string)$obsConfig->type,
                    'model' => $obsConfig->class ? (string)$obsConfig->class : $obsConfig->getClassName(),
                    'method'=> (string)$obsConfig->method,
                    'args'  => (array)$obsConfig->args,
                );
            }
            $events[$eventName]['observers'] = $observers;
            $this->_events[$area][$eventName]['observers'] = $observers;
        }
        if (false===$events[$eventName]) {
            continue;
        } else {
            $event = new Varien_Event($args);
            $event->setName($eventName);
            $observer = new Varien_Event_Observer();
        }

        foreach ($events[$eventName]['observers'] as $obsName=>$obs) {
            $observer->setData(array('event'=>$event));
            Varien_Profiler::start('OBSERVER: '.$obsName);
            switch ($obs['type']) {
                case 'disabled':
                    break;
                case 'object':
                case 'model':
                    $method = $obs['method'];
                    $observer->addData($args);
                    $object = Mage::getModel($obs['model']);
                    $this->_callObserverMethod($object, $method, $observer);
                    break;
                default:
                    $method = $obs['method'];
                    $observer->addData($args);
                    $object = Mage::getSingleton($obs['model']);
                    $this->_callObserverMethod($object, $method, $observer);
                    break;
            }
            Varien_Profiler::stop('OBSERVER: '.$obsName);
        }
    }
    return $this;
}

I was wondering if someone ever checked the impact on performance of dispatching events. I was thinking about implementing an event whitelist system (with all the events Magento core observes whitelisted) to avoid dispatching useless events and improve performance.

  • Well spotted! It would be interesting to have an average of observer triggered on the key pages, and it would be pretty easy to catch the effect on page load if you open up the Magento Profiler – Mayers Oct 26 '16 at 10:09
  • 1
    Objects are passed by reference. So no matter how huge an object you're passing around in the code, IMHO it is only the reference that is being passed? So event dispatches or your 'fixes' shouldn't really have a huge impact on performance either way. Unless ofcourse, the events that you take out are doing something else like db reads/writes to hurt performance. – Paras Sood Oct 26 '16 at 11:16
  • @ParasSood what about the $observers array being created ? Execution time was also in my mind – Raphael at Digital Pianism Oct 26 '16 at 11:17
  • @RaphaelatDigitalPianism in memory operation optimizations like browsing through 150 array items rather than 250 items should only have an impact of 10-20 of ms on the time to first byte. So I'd believe this effort could be better spent elsewhere. – Paras Sood Oct 26 '16 at 11:19
  • @ParasSood thanks for that I appreciate your comment, feel free to post an answer detailing your point of view. (totally forgot my PHP lessons regarding references BTW ^^) – Raphael at Digital Pianism Oct 26 '16 at 11:20
4

Objects are passed by reference. So no matter how huge an object you're passing around in the code, IMHO it is only the reference that is being passed. So event dispatches or your 'fixes' shouldn't really have a huge impact on performance either way. Unless of course, the events that you take out are doing something else like db reads/writes to hurt performance.

Also, in memory operation optimizations like browsing through 150 array items rather than 250 items(observers/events that Magento loops through) should only have an impact of 10-20 of ms on the time to first byte. So I'd believe this effort could be better spent elsewhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.