Component::trigger() Performance

trigger() always uses call_user_func, while raiseEvent() in Yii 1 also used $object->$method when possible. Benchmarking with 1 million iterations (I even included Reflection) gives these results:


$object->$method: 0.25550103187561 s

Reflection:       0.45736908912659 s

call_user_func:   0.70347189903259 s

$object->$method is much faster than call_user_func. Maybe it can be changed to something like this:


			if (is_array($handler[0]) {

				list($object, $method) = $handler[0];

				if (is_string($object)) {

					call_user_func($handler[0], $event);

				} else {

					$object->$method($event);

				}

			} else {

				call_user_func($handler[0], $event);

			}



I think we don’t need to worry this performance difference. In a typical request, there should be less than 100 events triggered. Also, when you do the above checks, you add additional cost which invalidates your profiling results shown above.

Hm, do ActiveRecords still fire onAfterFind events? I remember the topic Performance Impact Of Behaviors. Basically, le_top ran into performance problems, because he instantiated lots of AR instances and all of them instantiated and attached even more behaviors.

I wouldn’t have ever expected behavior attachment could become a bottle neck, but still it happened. Same goes for events. Who knows how and in which places devs will use events?

True, I forgot about that and tested it this morning, it was still twice as fast as call_user_func.

But I agree that there likely won’t be many events, so it’s not a bottleneck.