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
Recyclable Iterators #45
Comments
Interesting idea. I think this should not be the default behavior of Even in single-threaded applications, multiple iterators can be active over the same object, e.g. in nested loops processing pairs of items in a list or set. Quietly breaking such code by violating the |
If you make another method, the standard |
How about somehow providing the iterator from outside? This way, it
would be possible to reuse the same iterator for iterating multiple
collections.
|
This feature would be awesome. But @mdekstrand is correct. It should not be the default behavior of |
You could add a new constructor which will either use the default iterator or recycled iterator by default with the warning that once such collection is created one behavior or the other applies for all iterators of such instance. Edit: Memory wise is just one boolen per collection instance which will default to the default iterator behavior. |
Or another option is to generate recycled iterator variants of each collection if adding a boolean is so bad. |
I think a better option would be to have a reset() (or something) method that resets an iterator. Returning always the same object is too big a break from the current practice (and from the specification). |
@vigna yes that's a much better option, basically you reuse the iterator rather than breaking the JCF specs. |
I have had this on my wishlist for some time, but I don't think this should be handled by the collection. Instead, iterators should simply have a Then you could simply get the iterator once, and P.S. thanks for the upgrades to Java 8. I appreciate this, and I hope there isn't a performance regression because of this (the use of lambdas sometimes seems to cause unexpected performance regressions, but the hotspot VM also improves). I would appreciate a modularized fastutil, with a fastutil-mini that only has the most important parts, fastutil-extra that has the less common primitives, and fastutil-big that has all the big collections. A meta package "fastutil" depending on all three could provide backwards compatibility. In many cases, the -mini should be enough and reduce the footprint. |
Maybe "rewind()", and we could have a RewindableIterator interface.
…On June 24, 2017 12:19:11 AM GMT+02:00, Erich Schubert ***@***.***> wrote:
I have had this on my wishlist for some time, but I don't think this
should be handled by the collection.
Instead, iterators should simply have a `reset()` function. I'd assume
this is fairly easy to implement for all iterators on collections.
Then you could simply get the iterator once, and `reset()` it before
each use. With multiple threads, you can get a separate iterator for
each thread, or even handle an "iterator pool" of iterators if
necessary (similar to how people have been doing database connection
pools).
--
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
#45 (comment)
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
|
Since fastutil already adds |
Well, an interface has the advantage that people can easily check for the feature. What you propose implies that to check one has to try and check the exception with a very complicated logic... |
To make it usable, we need the return value to be a |
Just as an example of how it could look like, Agrona always returns the same iterator instance on its collections and calls |
That violates the Map contract. |
@jheusser I think that is a really bad idea. What if I need a nested loop, to compute pairwise similarities? I need to be able to get two different iterators... I like the idea of Having the collection With the
Note that in this example, I need the iterator to have both |
Why was this closed? |
Nope, but this was from 5 years ago and no one proposed a PR. I have to clean up issues once in a while or I lost track of what's urgent... |
+1 for explicit rewind() / reset() |
I would like to introduce the idea that fastutil should support recyclable iterators. That is, when calling the
iterator()
method, the same iterator object should be returned but reset to the start.When trying to avoid allocations, it's clear that escape analysis can remove these allocations, but that means nothing when trying to profile the application, all I can see is these iterators because escape analysis is turned off by the profiler so I just fallback to using
elements()
where I can.As long as the collections are not being paired with a lock, it seems like this is pretty straightforward. For the synchronized collections, this might be complicated. Although a lock must be held for the duration of the iteration, the iterator itself might be retained beyond the scope of the lock, and then be used again when the lock is re-acquired
The text was updated successfully, but these errors were encountered: