Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: [UNDER CONSIDERATION] Use Array.slice(0) to copy Observers instead of a loop  (Read 7448 times)
Tekool
Sr. Member
****
Posts: 192


View Profile WWW Email
« 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 );
}
}
}
« Last Edit: February 21, 2011, 08:53:36 by puremvc » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 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>
Logged
Tekool
Sr. Member
****
Posts: 192


View Profile WWW Email
« Reply #2 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.
Logged
Pages: [1]
Print