You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@flex.apache.org by lizhi <sl...@qq.com> on 2016/04/25 09:37:11 UTC

flexjs foreach very slow

i do not konw the best code,but this code fast than now

//slow code 
 var foreachiter0_target = this.ss;
  for (var foreachiter0 in foreachiter0_target) 
  {
  var s = foreachiter0_target[foreachiter0];
  {
  }}

//more fast code
  var len=this.ss.length;
  for(var i=0;i<len;i++){
	  var s = this.ss[i];
  }



--
View this message in context: http://apache-flex-development.2333347.n4.nabble.com/flexjs-foreach-very-slow-tp52571.html
Sent from the Apache Flex Development mailing list archive at Nabble.com.

Re: flexjs foreach very slow

Posted by Harbs <ha...@gmail.com>.
Please add a comment to this JIRA:
https://issues.apache.org/jira/browse/FLEX-35070

On Apr 25, 2016, at 10:37 AM, lizhi <sl...@qq.com> wrote:

> i do not konw the best code,but this code fast than now
> 
> //slow code 
> var foreachiter0_target = this.ss;
>  for (var foreachiter0 in foreachiter0_target) 
>  {
>  var s = foreachiter0_target[foreachiter0];
>  {
>  }}
> 
> //more fast code
>  var len=this.ss.length;
>  for(var i=0;i<len;i++){
> 	  var s = this.ss[i];
>  }
> 
> 
> 
> --
> View this message in context: http://apache-flex-development.2333347.n4.nabble.com/flexjs-foreach-very-slow-tp52571.html
> Sent from the Apache Flex Development mailing list archive at Nabble.com.


Re: flexjs foreach very slow

Posted by Josh Tynjala <jo...@gmail.com>.
I also commented on JIRA with information about a best practice when using
for-in in JavaScript that avoids that issue.

- Josh

On Mon, Apr 25, 2016 at 11:33 AM, Harbs <ha...@gmail.com> wrote:

> I don’t think I realized that for each can be used for normal Objects in
> ActionScript. I always assumed it was restricted to array-like objects to
> loop through the indexed values.
>
> Like I mentioned in the JIRA here:
> https://issues.apache.org/jira/browse/FLEX-35070 that for-in is not a
> good replacement for cases where Array has custom properties or methods
> added to it (which happens in Javascript).
>
> On Apr 25, 2016, at 6:22 PM, Josh Tynjala <jo...@gmail.com> wrote:
>
> > for-each loops are commonly used with string keys too, so I'm guessing
> > that's why the code gets converted to a for-in loop. That would be fully
> > compatible with both integer and string keys. I guess if the target is an
> > Array or Vector, it would be possible to use a for(;;) loop instead. It
> > could still potentially break on edge cases, though.
> >
> > - Josh
> >
> > On Mon, Apr 25, 2016 at 12:37 AM, lizhi <sl...@qq.com> wrote:
> >
> >> i do not konw the best code,but this code fast than now
> >>
> >> //slow code
> >> var foreachiter0_target = this.ss;
> >>  for (var foreachiter0 in foreachiter0_target)
> >>  {
> >>  var s = foreachiter0_target[foreachiter0];
> >>  {
> >>  }}
> >>
> >> //more fast code
> >>  var len=this.ss.length;
> >>  for(var i=0;i<len;i++){
> >>          var s = this.ss[i];
> >>  }
> >>
> >>
> >>
> >> --
> >> View this message in context:
> >>
> http://apache-flex-development.2333347.n4.nabble.com/flexjs-foreach-very-slow-tp52571.html
> >> Sent from the Apache Flex Development mailing list archive at
> Nabble.com.
> >>
>
>

Re: flexjs foreach very slow

Posted by Harbs <ha...@gmail.com>.
I don’t think I realized that for each can be used for normal Objects in ActionScript. I always assumed it was restricted to array-like objects to loop through the indexed values.

Like I mentioned in the JIRA here: https://issues.apache.org/jira/browse/FLEX-35070 that for-in is not a good replacement for cases where Array has custom properties or methods added to it (which happens in Javascript).

On Apr 25, 2016, at 6:22 PM, Josh Tynjala <jo...@gmail.com> wrote:

> for-each loops are commonly used with string keys too, so I'm guessing
> that's why the code gets converted to a for-in loop. That would be fully
> compatible with both integer and string keys. I guess if the target is an
> Array or Vector, it would be possible to use a for(;;) loop instead. It
> could still potentially break on edge cases, though.
> 
> - Josh
> 
> On Mon, Apr 25, 2016 at 12:37 AM, lizhi <sl...@qq.com> wrote:
> 
>> i do not konw the best code,but this code fast than now
>> 
>> //slow code
>> var foreachiter0_target = this.ss;
>>  for (var foreachiter0 in foreachiter0_target)
>>  {
>>  var s = foreachiter0_target[foreachiter0];
>>  {
>>  }}
>> 
>> //more fast code
>>  var len=this.ss.length;
>>  for(var i=0;i<len;i++){
>>          var s = this.ss[i];
>>  }
>> 
>> 
>> 
>> --
>> View this message in context:
>> http://apache-flex-development.2333347.n4.nabble.com/flexjs-foreach-very-slow-tp52571.html
>> Sent from the Apache Flex Development mailing list archive at Nabble.com.
>> 


Re: flexjs foreach very slow

Posted by Josh Tynjala <jo...@gmail.com>.
for-each loops are commonly used with string keys too, so I'm guessing
that's why the code gets converted to a for-in loop. That would be fully
compatible with both integer and string keys. I guess if the target is an
Array or Vector, it would be possible to use a for(;;) loop instead. It
could still potentially break on edge cases, though.

- Josh

On Mon, Apr 25, 2016 at 12:37 AM, lizhi <sl...@qq.com> wrote:

> i do not konw the best code,but this code fast than now
>
> //slow code
>  var foreachiter0_target = this.ss;
>   for (var foreachiter0 in foreachiter0_target)
>   {
>   var s = foreachiter0_target[foreachiter0];
>   {
>   }}
>
> //more fast code
>   var len=this.ss.length;
>   for(var i=0;i<len;i++){
>           var s = this.ss[i];
>   }
>
>
>
> --
> View this message in context:
> http://apache-flex-development.2333347.n4.nabble.com/flexjs-foreach-very-slow-tp52571.html
> Sent from the Apache Flex Development mailing list archive at Nabble.com.
>

Re: flexjs foreach very slow

Posted by Josh Tynjala <jo...@gmail.com>.
Yes, exactly.

- Josh

On Tue, May 10, 2016 at 3:38 AM, Andy Dufilie <an...@gmail.com>
wrote:

> You're right, it is possible, but I think the key word there was "easily."
> :)
>
> Sent from my Android
> On May 9, 2016 23:59, "Alex Harui" <ah...@adobe.com> wrote:
>
> >
> >
> > On 5/9/16, 1:22 PM, "Josh Tynjala" <jo...@gmail.com> wrote:
> >
> > >That's a good call, Andy. You're absolutely right that a for-each loop
> > >could also have its own return statements that are meant for the
> > >surrounding function. With that in mind, I don't think there's a special
> > >case here that the compiler can easily detect to optimize.
> >
> > Hmm, maybe I'm missing something.  I'm not a compiler expert, but I've
> > heard about things like "code-flow analysis" and similar techniques that
> I
> > thought could be used for determining that there aren't planned exits
> from
> > a loop.
> >
> > -Alex
> >
> >
>

Re: flexjs foreach very slow

Posted by Andy Dufilie <an...@gmail.com>.
You're right, it is possible, but I think the key word there was "easily."
:)

Sent from my Android
On May 9, 2016 23:59, "Alex Harui" <ah...@adobe.com> wrote:

>
>
> On 5/9/16, 1:22 PM, "Josh Tynjala" <jo...@gmail.com> wrote:
>
> >That's a good call, Andy. You're absolutely right that a for-each loop
> >could also have its own return statements that are meant for the
> >surrounding function. With that in mind, I don't think there's a special
> >case here that the compiler can easily detect to optimize.
>
> Hmm, maybe I'm missing something.  I'm not a compiler expert, but I've
> heard about things like "code-flow analysis" and similar techniques that I
> thought could be used for determining that there aren't planned exits from
> a loop.
>
> -Alex
>
>

Re: flexjs foreach very slow

Posted by Alex Harui <ah...@adobe.com>.

On 5/9/16, 1:22 PM, "Josh Tynjala" <jo...@gmail.com> wrote:

>That's a good call, Andy. You're absolutely right that a for-each loop
>could also have its own return statements that are meant for the
>surrounding function. With that in mind, I don't think there's a special
>case here that the compiler can easily detect to optimize.

Hmm, maybe I'm missing something.  I'm not a compiler expert, but I've
heard about things like "code-flow analysis" and similar techniques that I
thought could be used for determining that there aren't planned exits from
a loop.

-Alex


Re: flexjs foreach very slow

Posted by Josh Tynjala <jo...@gmail.com>.
That's a good call, Andy. You're absolutely right that a for-each loop
could also have its own return statements that are meant for the
surrounding function. With that in mind, I don't think there's a special
case here that the compiler can easily detect to optimize. It sounds like
you should simply avoid for-each on Arrays in performance critical code. If
you're using for-each in existing code, and it seems too slow, you'll need
to manually optimize using one of the alternate approaches we've discovered
here.

- Josh

On Mon, May 9, 2016 at 1:02 PM, Andy Dufilie <an...@gmail.com> wrote:

> There can also be return statements within loops as well as "break to
> label" or "continue to label".  It all seems very messy to me and I still
> think it should be behind an optimization option.  The current
> implementation works perfectly and I would prefer the JS output to be
> minimally different from the AS input, which makes debugging easier.
>
> Array.isArray() is the correct way to test if something is an Array, btw.
>
> On Mon, May 9, 2016 at 1:12 PM, Alex Harui <ah...@adobe.com> wrote:
>
> >
> >
> > On 5/9/16, 9:37 AM, "Josh Tynjala" <jo...@gmail.com> wrote:
> >
> > >We might be able to figure out how to use Array.some() instead. Return
> > >false for most iterations of the loop, and then return true when a
> "break"
> > >is encountered (and return false when "continue" is encountered).
> > >Replacing break and continue could be tricky, though, since they are
> > >handled in a different sub-emitter.
> >
> > Possible.  Testing needs to be done to make sure that these patterns
> > perform better since they introduce a function call and maybe some
> scoping
> > issues.
> >
> > It may also be that some future version of the compiler generates
> > different patterns if it knows there is no early exit from the loop.
> >
> > -Alex
> >
> >
>

Re: flexjs foreach very slow

Posted by Andy Dufilie <an...@gmail.com>.
There can also be return statements within loops as well as "break to
label" or "continue to label".  It all seems very messy to me and I still
think it should be behind an optimization option.  The current
implementation works perfectly and I would prefer the JS output to be
minimally different from the AS input, which makes debugging easier.

Array.isArray() is the correct way to test if something is an Array, btw.

On Mon, May 9, 2016 at 1:12 PM, Alex Harui <ah...@adobe.com> wrote:

>
>
> On 5/9/16, 9:37 AM, "Josh Tynjala" <jo...@gmail.com> wrote:
>
> >We might be able to figure out how to use Array.some() instead. Return
> >false for most iterations of the loop, and then return true when a "break"
> >is encountered (and return false when "continue" is encountered).
> >Replacing break and continue could be tricky, though, since they are
> >handled in a different sub-emitter.
>
> Possible.  Testing needs to be done to make sure that these patterns
> perform better since they introduce a function call and maybe some scoping
> issues.
>
> It may also be that some future version of the compiler generates
> different patterns if it knows there is no early exit from the loop.
>
> -Alex
>
>

Re: flexjs foreach very slow

Posted by Alex Harui <ah...@adobe.com>.

On 5/9/16, 9:37 AM, "Josh Tynjala" <jo...@gmail.com> wrote:

>We might be able to figure out how to use Array.some() instead. Return
>false for most iterations of the loop, and then return true when a "break"
>is encountered (and return false when "continue" is encountered).
>Replacing break and continue could be tricky, though, since they are
>handled in a different sub-emitter.

Possible.  Testing needs to be done to make sure that these patterns
perform better since they introduce a function call and maybe some scoping
issues.

It may also be that some future version of the compiler generates
different patterns if it knows there is no early exit from the loop.

-Alex


Re: flexjs foreach very slow

Posted by Josh Tynjala <jo...@gmail.com>.
We might be able to figure out how to use Array.some() instead. Return
false for most iterations of the loop, and then return true when a "break"
is encountered (and return false when "continue" is encountered).
Replacing break and continue could be tricky, though, since they are
handled in a different sub-emitter.

- Josh

On Sun, May 8, 2016 at 11:49 PM, Harbs <ha...@gmail.com> wrote:

> Rats. Good point…
>
> On May 9, 2016, at 8:12 AM, Alex Harui <ah...@adobe.com> wrote:
>
> > Isn't tan issue with Array.forEach() that you can't use 'break' to stop
> > the loop?
> >
> > -Alex
> >
> > On 5/8/16, 3:41 AM, "Harbs" <ha...@gmail.com> wrote:
> >
> >> So would this be a workable solution?
> >>
> >> This:
> >>
> >> for each(item in object){
> >>      item.doSomething();
> >> }
> >>
> >> Would become:
> >>
> >> if (!!object.forEach){
> >>      object.forEach(function(item){
> >>              item.doSomething();
> >>      });
> >> } else {
> >>      var foreachiter0_target = object;
> >>      for (var foreachiter0 in foreachiter0_target)
> >>      {
> >>              var item = foreachiter0_target[foreachiter0];
> >>              item.doSomething();
> >>      }
> >> }
> >>
> >> I could add a forEach method to XMLList objects and then we would not
> >> need to do compile time checks for XML. (at least for cases of for each)
> >>
> >> On May 8, 2016, at 3:16 AM, Josh Tynjala <jo...@gmail.com> wrote:
> >>
> >>> The array forEach() seems like an acceptable alternative. Looking at
> MDN
> >>> [1], forEach is widely supported in browsers. Including IE 9.
> >>>
> >>>
> >>>
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_
> >>> Objects/Array/forEach
> >>>
> >>> - Josh
> >>>
> >>> On Sat, May 7, 2016 at 4:32 PM, lizhi <sl...@qq.com> wrote:
> >>>
> >>>> 10x slow.
> >>>> maybe use the arr.forEach.
> >>>> pls run this code
> >>>>
> >>>> https://gist.github.com/matrix3d/a9765b94ade3d626ad64d16f28deccae
> >>>>
> >>>>
> >>>>
> >>>> --
> >>>> View this message in context:
> >>>>
> >>>>
> http://apache-flex-development.2333347.n4.nabble.com/flexjs-foreach-very
> >>>> -slow-tp52571p52880.html
> >>>> Sent from the Apache Flex Development mailing list archive at
> >>>> Nabble.com.
> >>>>
> >>
> >
>
>

Re: flexjs foreach very slow

Posted by Harbs <ha...@gmail.com>.
Rats. Good point…

On May 9, 2016, at 8:12 AM, Alex Harui <ah...@adobe.com> wrote:

> Isn't tan issue with Array.forEach() that you can't use 'break' to stop
> the loop?
> 
> -Alex
> 
> On 5/8/16, 3:41 AM, "Harbs" <ha...@gmail.com> wrote:
> 
>> So would this be a workable solution?
>> 
>> This:
>> 
>> for each(item in object){
>> 	item.doSomething();
>> }
>> 
>> Would become:
>> 
>> if (!!object.forEach){
>> 	object.forEach(function(item){
>> 		item.doSomething();	
>> 	});
>> } else {
>> 	var foreachiter0_target = object;
>> 	for (var foreachiter0 in foreachiter0_target)
>> 	{
>> 		var item = foreachiter0_target[foreachiter0];
>> 		item.doSomething();
>> 	}
>> }
>> 
>> I could add a forEach method to XMLList objects and then we would not
>> need to do compile time checks for XML. (at least for cases of for each)
>> 
>> On May 8, 2016, at 3:16 AM, Josh Tynjala <jo...@gmail.com> wrote:
>> 
>>> The array forEach() seems like an acceptable alternative. Looking at MDN
>>> [1], forEach is widely supported in browsers. Including IE 9.
>>> 
>>> 
>>> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_
>>> Objects/Array/forEach
>>> 
>>> - Josh
>>> 
>>> On Sat, May 7, 2016 at 4:32 PM, lizhi <sl...@qq.com> wrote:
>>> 
>>>> 10x slow.
>>>> maybe use the arr.forEach.
>>>> pls run this code
>>>> 
>>>> https://gist.github.com/matrix3d/a9765b94ade3d626ad64d16f28deccae
>>>> 
>>>> 
>>>> 
>>>> --
>>>> View this message in context:
>>>> 
>>>> http://apache-flex-development.2333347.n4.nabble.com/flexjs-foreach-very
>>>> -slow-tp52571p52880.html
>>>> Sent from the Apache Flex Development mailing list archive at
>>>> Nabble.com.
>>>> 
>> 
> 


Re: flexjs foreach very slow

Posted by Alex Harui <ah...@adobe.com>.
Isn't tan issue with Array.forEach() that you can't use 'break' to stop
the loop?

-Alex

On 5/8/16, 3:41 AM, "Harbs" <ha...@gmail.com> wrote:

>So would this be a workable solution?
>
>This:
>
>for each(item in object){
>	item.doSomething();
>}
>
>Would become:
>
>if (!!object.forEach){
>	object.forEach(function(item){
>		item.doSomething();	
>	});
>} else {
>	var foreachiter0_target = object;
>	for (var foreachiter0 in foreachiter0_target)
>	{
>		var item = foreachiter0_target[foreachiter0];
>		item.doSomething();
>	}
>}
>
>I could add a forEach method to XMLList objects and then we would not
>need to do compile time checks for XML. (at least for cases of for each)
>
>On May 8, 2016, at 3:16 AM, Josh Tynjala <jo...@gmail.com> wrote:
>
>> The array forEach() seems like an acceptable alternative. Looking at MDN
>> [1], forEach is widely supported in browsers. Including IE 9.
>> 
>> 
>>https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_
>>Objects/Array/forEach
>> 
>> - Josh
>> 
>> On Sat, May 7, 2016 at 4:32 PM, lizhi <sl...@qq.com> wrote:
>> 
>>> 10x slow.
>>> maybe use the arr.forEach.
>>> pls run this code
>>> 
>>> https://gist.github.com/matrix3d/a9765b94ade3d626ad64d16f28deccae
>>> 
>>> 
>>> 
>>> --
>>> View this message in context:
>>> 
>>>http://apache-flex-development.2333347.n4.nabble.com/flexjs-foreach-very
>>>-slow-tp52571p52880.html
>>> Sent from the Apache Flex Development mailing list archive at
>>>Nabble.com.
>>> 
>


Re: flexjs foreach very slow

Posted by Harbs <ha...@gmail.com>.
So would this be a workable solution?

This:

for each(item in object){
	item.doSomething();
}

Would become:

if (!!object.forEach){
	object.forEach(function(item){
		item.doSomething();	
	});
} else {
	var foreachiter0_target = object;
	for (var foreachiter0 in foreachiter0_target)
	{
		var item = foreachiter0_target[foreachiter0];
		item.doSomething();
	}
}

I could add a forEach method to XMLList objects and then we would not need to do compile time checks for XML. (at least for cases of for each)

On May 8, 2016, at 3:16 AM, Josh Tynjala <jo...@gmail.com> wrote:

> The array forEach() seems like an acceptable alternative. Looking at MDN
> [1], forEach is widely supported in browsers. Including IE 9.
> 
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
> 
> - Josh
> 
> On Sat, May 7, 2016 at 4:32 PM, lizhi <sl...@qq.com> wrote:
> 
>> 10x slow.
>> maybe use the arr.forEach.
>> pls run this code
>> 
>> https://gist.github.com/matrix3d/a9765b94ade3d626ad64d16f28deccae
>> 
>> 
>> 
>> --
>> View this message in context:
>> http://apache-flex-development.2333347.n4.nabble.com/flexjs-foreach-very-slow-tp52571p52880.html
>> Sent from the Apache Flex Development mailing list archive at Nabble.com.
>> 


Re: flexjs foreach very slow

Posted by Josh Tynjala <jo...@gmail.com>.
The array forEach() seems like an acceptable alternative. Looking at MDN
[1], forEach is widely supported in browsers. Including IE 9.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

- Josh

On Sat, May 7, 2016 at 4:32 PM, lizhi <sl...@qq.com> wrote:

> 10x slow.
> maybe use the arr.forEach.
> pls run this code
>
> https://gist.github.com/matrix3d/a9765b94ade3d626ad64d16f28deccae
>
>
>
> --
> View this message in context:
> http://apache-flex-development.2333347.n4.nabble.com/flexjs-foreach-very-slow-tp52571p52880.html
> Sent from the Apache Flex Development mailing list archive at Nabble.com.
>

Re: flexjs foreach very slow

Posted by lizhi <sl...@qq.com>.
10x slow.
maybe use the arr.forEach.
pls run this code

https://gist.github.com/matrix3d/a9765b94ade3d626ad64d16f28deccae



--
View this message in context: http://apache-flex-development.2333347.n4.nabble.com/flexjs-foreach-very-slow-tp52571p52880.html
Sent from the Apache Flex Development mailing list archive at Nabble.com.

Re: flexjs foreach very slow

Posted by Josh Tynjala <jo...@gmail.com>.
I agree with Andy. The default behavior should remain syntax sugar on top
of for-in.

- Josh

On Sat, May 7, 2016 at 11:38 AM, Andy Dufilie <an...@gmail.com>
wrote:

> If you do make this change it should be an optional compiler argument,
> because this changes the behavior of the code. The following loop should
> print 1 and 2, not undefined:
>
> var array = [];
> array[2] = 1;
> array[4] = 2;
> for each (var x in array)
>     trace(x);
>
>
> On May 7, 2016 10:18 AM, "Alex Harui" <ah...@adobe.com> wrote:
>
> >
> >
> > On 5/7/16, 1:32 AM, "lizhi" <sl...@qq.com> wrote:
> >
> > >must ded the type.
> > >if(Array){
> > >   for(var i=0;i<len;i++)
> > >}else{
> > > for in
> > >}
> >
> > Should be doable.  How much faster is it?
> >
> > Please file a pull request or JIRA so we don't forget this issue.
> >
> > Thanks,
> > -Alex
> >
> >
>

Re: flexjs foreach very slow

Posted by Andy Dufilie <an...@gmail.com>.
If you do make this change it should be an optional compiler argument,
because this changes the behavior of the code. The following loop should
print 1 and 2, not undefined:

var array = [];
array[2] = 1;
array[4] = 2;
for each (var x in array)
    trace(x);


On May 7, 2016 10:18 AM, "Alex Harui" <ah...@adobe.com> wrote:

>
>
> On 5/7/16, 1:32 AM, "lizhi" <sl...@qq.com> wrote:
>
> >must ded the type.
> >if(Array){
> >   for(var i=0;i<len;i++)
> >}else{
> > for in
> >}
>
> Should be doable.  How much faster is it?
>
> Please file a pull request or JIRA so we don't forget this issue.
>
> Thanks,
> -Alex
>
>

Re: flexjs foreach very slow

Posted by Alex Harui <ah...@adobe.com>.

On 5/7/16, 1:32 AM, "lizhi" <sl...@qq.com> wrote:

>must ded the type.
>if(Array){
>   for(var i=0;i<len;i++)
>}else{
> for in
>}

Should be doable.  How much faster is it?

Please file a pull request or JIRA so we don't forget this issue.

Thanks,
-Alex


Re: flexjs foreach very slow

Posted by lizhi <sl...@qq.com>.
must ded the type.
if(Array){
   for(var i=0;i<len;i++)
}else{
 for in
}



--
View this message in context: http://apache-flex-development.2333347.n4.nabble.com/flexjs-foreach-very-slow-tp52571p52864.html
Sent from the Apache Flex Development mailing list archive at Nabble.com.