Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top