TimurMahammadov / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

Iterators.concat(a, b) minor memory issue #102

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

Is implemented using Arrays.asList (in turn using new ArrayList()).

Which means you're hanging on to a after you've moved on to b.
Not terribly important unless you've got a lot of generators (producing 
results with little or no backing collections)

An Iterators.of(T... a) which nulls references to returned items might be 
handy.

Original issue reported on code.google.com by jvdne...@gmail.com on 29 Oct 2008 at 9:20

GoogleCodeExporter commented 9 years ago
Arrays.asList do *not* use new (java.util.)ArrayList()

Original comment by jim.andreou on 29 Oct 2008 at 9:24

GoogleCodeExporter commented 9 years ago

fair enough
but it's still a 'backing collection' which retains references to objects 
already 
iterated over.

Original comment by jvdne...@gmail.com on 29 Oct 2008 at 9:46

GoogleCodeExporter commented 9 years ago
Thanks for pointing this out.

Since iterators are usually short-lived and require small amounts of memory, 
I'm not
sure whether this issue is worth fixing.

Original comment by jared.l....@gmail.com on 29 Oct 2008 at 6:28

GoogleCodeExporter commented 9 years ago

I need it since I use *very* wide and deep trees of iterators/iterables, and 
memory 
usage peaks too high. This helps. Code pasted below.

    @SuppressWarnings("unchecked")
    public static <T> Iterator<T> concat2(final Iterator<? extends T> a, final 
Iterator<? extends T> b) {
        final Iterator[] items = new Iterator[] { a, b };
        return new UnmodifiableIterator<T>() {
            private Iterator<? extends T> current = items[0];
            private int index = 0;
            public boolean hasNext() {
                while (!this.current.hasNext() && (this.index < items.length)) {
                    this.current = items[this.index];
                    items[this.index++] = null;
                }
                return this.current.hasNext();
            }
            public T next() {
                if (hasNext()) {
                    return this.current.next();
                }
                return null;
            }
        };
    }

Feel free to use should you decide to fix it anyway. (same holds for 
concat(Iterator<? extends T>... inputs))

Original comment by jvdne...@gmail.com on 3 Nov 2008 at 11:47

GoogleCodeExporter commented 9 years ago

Original comment by kevin...@gmail.com on 17 Sep 2009 at 5:58

GoogleCodeExporter commented 9 years ago
This issue has been moved to the Guava project (keeping the same id number). 
Simply replace 'google-collections' with 'guava-libraries' in your address 
bar and it should take you there.

Original comment by kevinb@google.com on 5 Jan 2010 at 11:09