This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
Re: Support Iterable in for-each
- From: Jamison Hope <jrh at theptrgroup dot com>
- To: "kawa at sourceware dot org list" <kawa at sourceware dot org>
- Date: Fri, 10 Jan 2014 16:26:58 -0500
- Subject: Re: Support Iterable in for-each
- Authentication-results: sourceware.org; auth=none
- References: <CAOTvmokU5J0Gy58iCa_UgU7FhvYLT6Ba7Z8C6W1yA7DBA44G_w at mail dot gmail dot com> <52D048D4 dot 2080803 at bothner dot com> <596354A4-C4B3-40B6-BCED-2FCF1CB1B9C5 at theptrgroup dot com> <52D06419 dot 4080302 at bothner dot com>
On Jan 10, 2014, at 4:20 PM, Per Bothner <per@bothner.com> wrote:
> On 01/10/2014 01:04 PM, Jamison Hope wrote:
>> On Jan 10, 2014, at 2:24 PM, Per Bothner <per@bothner.com> wrote:
>
>>> * It is possible to come up with some iteration protocol that
>>> at least avoids object allocation for Kawa lists and vectors, and
>>> perhaps falls back to Iterator in the generic case. However, I haven't
>>> figured out a way to do so without 3 method calls per iteration step.
>>> (The problem is for a list the "state" variable is an object (a pair);
>>> for a vector it is an index. To avoid heap allocation the state
>>> needs to be one or more local variables. Thus for each step you may need
>>> to update an object and/or an int, plus you need to extract the next
>>> value.)
>>
>> I count two: Iterator#hasNext() and Iterator#next(). What's the third
>> method call? Just the invocation of the passed procedure?
>
> You missed the requirement "some iteration protocol that at least avoids
> object allocation for Kawa lists and vectors". Using Iterator#hasNext(
> and Iterator#next() implies that you have allocated an Iterator. It's not
> per-step, but it is per-iteration. Perhaps I should worry less about it.
I was assuming that the existing for-each and vector-for-each would remain,
so Kawa lists and vectors would avoid this entirely. The path that treats
the sequence as an Iterable would be the last resort.
--
Jamison Hope
The PTR Group
www.theptrgroup.com