-
-
Notifications
You must be signed in to change notification settings - Fork 638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes memory leak of grouped iterator #1907
Merged
danieldietrich
merged 8 commits into
vavr-io:master
from
danieldietrich:grouped-iterator
Mar 12, 2017
+174
−38
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aa2c59b
Fixes memory leak of grouped iterator
danieldietrich c442771
Improved by throwing away unnecessary elements and by cleaning buffer…
danieldietrich cb7abb9
Used Iterator instead of AbstractIterator
danieldietrich 76b2c00
List.take/drop optimizations. See also #1910
danieldietrich 657175c
Added Iterator.sliding() benchmark
danieldietrich 17b70a9
Based benchmark Iterators of element array
danieldietrich 58413b6
Fixed a bug/uncovered case
danieldietrich 5f84292
low-level optimization
danieldietrich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
import javaslang.Tuple3; | ||
import javaslang.collection.IteratorModule.ConcatIterator; | ||
import javaslang.collection.IteratorModule.DistinctIterator; | ||
import javaslang.collection.IteratorModule.GroupedIterator; | ||
import javaslang.control.Option; | ||
|
||
import java.math.BigDecimal; | ||
|
@@ -20,6 +21,7 @@ | |
import static java.lang.Double.NEGATIVE_INFINITY; | ||
import static java.lang.Double.POSITIVE_INFINITY; | ||
import static java.math.RoundingMode.HALF_UP; | ||
import static javaslang.API.*; | ||
import static javaslang.collection.IteratorModule.BigDecimalHelper.areEqual; | ||
import static javaslang.collection.IteratorModule.BigDecimalHelper.asDecimal; | ||
import static javaslang.collection.IteratorModule.EmptyIterator; | ||
|
@@ -1535,9 +1537,9 @@ default <C> Map<C, Iterator<T>> groupBy(Function<? super T, ? extends C> classif | |
|
||
@Override | ||
default Iterator<Seq<T>> grouped(int size) { | ||
return sliding(size, size); | ||
return new GroupedIterator<>(this, size, size); | ||
} | ||
|
||
@Override | ||
default boolean hasDefiniteSize() { | ||
return false; | ||
|
@@ -1827,37 +1829,9 @@ default Iterator<Seq<T>> sliding(int size) { | |
|
||
@Override | ||
default Iterator<Seq<T>> sliding(int size, int step) { | ||
if (size <= 0 || step <= 0) { | ||
throw new IllegalArgumentException("size: " + size + " or step: " + step + " not positive"); | ||
} | ||
if (!hasNext()) { | ||
return empty(); | ||
} else { | ||
final Stream<T> source = Stream.ofAll(this); | ||
return new AbstractIterator<Seq<T>>() { | ||
private Stream<T> that = source; | ||
private IndexedSeq<T> next = null; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
while (next == null && !that.isEmpty()) { | ||
final Tuple2<Stream<T>, Stream<T>> split = that.splitAt(size); | ||
next = split._1.toVector(); | ||
that = split._2.isEmpty() ? Stream.empty() : that.drop(step); | ||
} | ||
return next != null; | ||
} | ||
|
||
@Override | ||
public IndexedSeq<T> getNext() { | ||
final IndexedSeq<T> result = next; | ||
next = null; | ||
return result; | ||
} | ||
}; | ||
} | ||
return new GroupedIterator<>(this, size, step); | ||
} | ||
|
||
@Override | ||
default Tuple2<Iterator<T>, Iterator<T>> span(Predicate<? super T> predicate) { | ||
Objects.requireNonNull(predicate, "predicate is null"); | ||
|
@@ -2089,6 +2063,35 @@ public String toString() { | |
} | ||
} | ||
|
||
final class GroupedIterator<T> extends AbstractIterator<Seq<T>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @paplorinc I simplified it... |
||
|
||
private final Iterator<T> that; | ||
private final int step; | ||
|
||
private Seq<T> buffer; | ||
|
||
GroupedIterator(Iterator<T> that, int size, int step) { | ||
if (size < 1 || step < 1) { | ||
throw new IllegalArgumentException("size (" + size + ") and step (" + step + ") must both be positive"); | ||
} | ||
this.that = that; | ||
this.step = step; | ||
this.buffer = Vector.ofAll(that.take(size)); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return !buffer.isEmpty(); | ||
} | ||
|
||
@Override | ||
protected Seq<T> getNext() { | ||
final Seq<T> result = buffer; | ||
buffer = that.hasNext() ? buffer.appendAll(that.take(step)).drop(step) : buffer.take(0); | ||
return result; | ||
} | ||
} | ||
|
||
final class BigDecimalHelper { | ||
|
||
@GwtIncompatible("Math::nextDown is not implemented") | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scala's grouped() and sliding() functions return a
GroupedIterator<Seq<T>>
. That GroupedIterator has the ability of padding and skipping partial results (both not implemented in our version, yet).It would be nice if we could provide the same functionality. Returning a
GroupedIterator<Seq<T>>
instead ofIterator<Seq<T>>
will not be backward compatible. I will target that change for 3.0.0There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I internally based the buffer on our persistent Vector, Scala users a mutable ArrayBuffer. Our version is blazing fast (a little slower than the mutable version). Congrats @paplorinc !!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice, could you share some benchmarks :)?