PureMVC Architects Lounge

PureMVC Manifold => Bug Report => Topic started by: Tekool on February 19, 2011, 06:07:06



Title: [UNDER CONSIDERATION] Use Array.slice(0) to copy Observers instead of a loop
Post by: Tekool on February 19, 2011, 06:07:06
Here is the code used in View.notifyObservers() method to copy the Array we will loop over to notify observers :

:
public function notifyObservers( notification:INotification ) : void
{
  if( observerMap[ notification.getName() ] != null ) {

// Get a reference to the observers list for this notification name
var observers_ref:Array = observerMap[ notification.getName() ] as Array;

// Copy observers from reference array to working array,
// since the reference array may change during the notification loop
  var observers:Array = new Array();
  var observer:IObserver;
for (var i:Number = 0; i < observers_ref.length; i++)
{
   observer = observers_ref[ i ] as IObserver;
   observers.push( observer );
}

// Notify Observers from the working array                
for (i = 0; i < observers.length; i++) {
   observer = observers[ i ] as IObserver;
   observer.notifyObserver( notification );
}
}
}


This take a lot of CPU steps for nothing and as this method is heavily used, I think we can benefit a lot from changing it to:

:
public function notifyObservers( notification:INotification ) : void
{
var observers_ref:Array = observerMap[ notification.getName() ] as Array;
if( observers_ref != null )
{
// Copy observers from reference array to working array,
// since the reference array may change during the notification loop
var observers:Array = observers_ref.slice(0);

// Notify Observers from the working array                
for( var i:uint = 0; i<observers.length; i++) {
   observer = observers[ i ] as IObserver;
   observer.notifyObserver( notification );
}
}
}


Title: Re: Use Array.slice(0) to copy the Array in View.notifyObservers instead of a loop
Post by: puremvc on February 20, 2011, 01:33:31
Another place where we need to know how many cycles difference we're talking about before the 'pro' side of the argument has definite weight.

On the 'con' side is that we're trying to use the simplest possible language constructs wherever possible. If it isn't outrageously unperformant, then it is better to use constructs that are available in the widest possible number of languages. Array.slice() or (a handy analog thereof) may not be available in all languages, thus the porting developer needs to come up with a solution in their language.

-=Cliff>


Title: Re: [SUGGESSTION] Use Array.slice(0) to copy Observers instead of a loop
Post by: Tekool on February 20, 2011, 03:55:24
To have worked on near each language PureMVC was ported I saw that each developer chosen to use the more appropriate solution for the language on which PureMVC was ported. When it has no consequence on the behavior of PureMVC nor its implementation, I think this is good to benefits from possible optimization provided by a specific language. This one is really important as it adds twice the unwanted debug steps located in notifyObservers methods when working in debug mode.