This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Why override foreach on VectorViewTemplate.Transformed subclasses?

2 replies
Roland Kuhn
Joined: 2008-12-26,
User offline. Last seen 3 years 14 weeks ago.

Hi,

after checking the resulting byte code for VectorViewTemplate$$anon$9
and comparing with my VectorViewTemplate$$anon$10 after applying my
patch contained in https://lampsvn.epfl.ch/trac/scala/ticket/2074, I
see that leaving out the override for foreach makes the call just one
bit more direct as it is not dispatched via VectorViewTemplate$Zipped.
What was the rationale? The comment at the top mentions execution
speed, but that seems to be non-tested.

Also, if I'm zipping an Array with a List, then "foreach" on the
result uses apply() on the List, which is inefficient. In that case,
it would be preferable to use Iterable.foreach. Could this be done
using implicits? It would be nice to build one kind of Zipped for
Vectors and another for the rest...

Ciao,

Roland

Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 4 days ago.
Re: Why override foreach on VectorViewTemplate.Transformed sub

I haven't looked at your code (I'm on my phone) but if you implement
Zipped's foreach with iterators of both of the underlying collections,
you should get efficient access for both Lists and Vectors.

--j

On Monday, June 29, 2009, Roland Kuhn wrote:
> Hi,
>
> after checking the resulting byte code for VectorViewTemplate$$anon$9 and comparing with my VectorViewTemplate$$anon$10 after applying my patch contained in https://lampsvn.epfl.ch/trac/scala/ticket/2074, I see that leaving out the override for foreach makes the call just one bit more direct as it is not dispatched via VectorViewTemplate$Zipped. What was the rationale? The comment at the top mentions execution speed, but that seems to be non-tested.
>
> Also, if I'm zipping an Array with a List, then "foreach" on the result uses apply() on the List, which is inefficient. In that case, it would be preferable to use Iterable.foreach. Could this be done using implicits? It would be nice to build one kind of Zipped for Vectors and another for the rest...
>
> Ciao,
>
> Roland
>

Roland Kuhn
Joined: 2008-12-26,
User offline. Last seen 3 years 14 weeks ago.
Re: Why override foreach on VectorViewTemplate.Transformed sub

It would be nice to keep the while loop from VectorTemplate in case of
Vectors and fall back to iterators otherwise. There was a discussion
about conditional definitions based on type comparisons the other day,
in which implicits were mentioned as a possible solution, but I have
yet to understand how that was meant to play out. And then I'll have
to benchmark the difference and check whether I'm fooling myself on
this one.

Ciao,

Roland

On 2 Jul 2009, at 07:01, Jorge Ortiz wrote:

> I haven't looked at your code (I'm on my phone) but if you implement
> Zipped's foreach with iterators of both of the underlying collections,
> you should get efficient access for both Lists and Vectors.
>
> --j
>
> On Monday, June 29, 2009, Roland Kuhn wrote:
>> Hi,
>>
>> after checking the resulting byte code for VectorViewTemplate$$anon
>> $9 and comparing with my VectorViewTemplate$$anon$10 after applying
>> my patch contained in https://lampsvn.epfl.ch/trac/scala/ticket/
>> 2074, I see that leaving out the override for foreach makes the
>> call just one bit more direct as it is not dispatched via
>> VectorViewTemplate$Zipped. What was the rationale? The comment at
>> the top mentions execution speed, but that seems to be non-tested.
>>
>> Also, if I'm zipping an Array with a List, then "foreach" on the
>> result uses apply() on the List, which is inefficient. In that
>> case, it would be preferable to use Iterable.foreach. Could this be
>> done using implicits? It would be nice to build one kind of Zipped
>> for Vectors and another for the rest...
>>
>> Ciao,
>>
>> Roland
>>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland