Code Monkey home page Code Monkey logo

Comments (20)

mdekstrand avatar mdekstrand commented on August 26, 2024 4

Interesting idea.

I think this should not be the default behavior of iterator(); if desired, it should be on a separate method, like fastIterator() is; possibly recycledIterator().

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 iterator() contract would be a very bad thing.

from fastutil.

botismarius avatar botismarius commented on August 26, 2024 2

from fastutil.

vigna avatar vigna commented on August 26, 2024 1

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...

from fastutil.

pjulien avatar pjulien commented on August 26, 2024

I think this should not be the default behavior of iterator()

If you make another method, the standard for loop won't be able to call it. It might warrant another set of generated classes

from fastutil.

jonatino avatar jonatino commented on August 26, 2024

This feature would be awesome. But @mdekstrand is correct. It should not be the default behavior of iterator() due to the unforseen amount of multithreading errors

from fastutil.

guidomedina avatar guidomedina commented on August 26, 2024

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.

from fastutil.

guidomedina avatar guidomedina commented on August 26, 2024

Or another option is to generate recycled iterator variants of each collection if adding a boolean is so bad.

from fastutil.

vigna avatar vigna commented on August 26, 2024

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).

from fastutil.

guidomedina avatar guidomedina commented on August 26, 2024

@vigna yes that's a much better option, basically you reuse the iterator rather than breaking the JCF specs.

from fastutil.

kno10 avatar kno10 commented on August 26, 2024

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).

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.

from fastutil.

vigna avatar vigna commented on August 26, 2024

from fastutil.

kno10 avatar kno10 commented on August 26, 2024

Since fastutil already adds skip(int), is it really necessary to add a RewindableIterator interface? Why not just add rewind() to drv/Iterator.drv, with a default implementation throwing an exception like remove()?

from fastutil.

vigna avatar vigna commented on August 26, 2024

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...

from fastutil.

kno10 avatar kno10 commented on August 26, 2024

To make it usable, we need the return value to be a RewindableIterator, and at the same time a primitive-specific iterator. So this would mean introducing RewindableFloatIterator etc.; and as far as I can tell we will probably not have a non-rewindable float iterator anywhere in fastutil.

from fastutil.

jheusser avatar jheusser commented on August 26, 2024

Just as an example of how it could look like, Agrona always returns the same iterator instance on its collections and calls reset() when getting the iterator (example here). This allows garbage-free iteration.

from fastutil.

vigna avatar vigna commented on August 26, 2024

That violates the Map contract.

from fastutil.

kno10 avatar kno10 commented on August 26, 2024

@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 rewind(), but iterator recycling needs to be done by the user (you could try to use finalizer/reference magic, but that will likely be more expensive than new iterators!).

Having the collection rewind() an iterator that may still be in use will cause horrible side effects!

With the rewind() discussed above, one could do

IntIterator i1 = col.iterator(), i2 = col.iterator();
while(i1.hasNext()) {
  int first = i1.nextInt();
  i2.rewind();
  while(i2.hasNext()) {
    int second = i2.nextInt();
    // Do something on (frist, second)
  }
}

Note that in this example, I need the iterator to have both rewind() and nextInt().

from fastutil.

jheusser avatar jheusser commented on August 26, 2024

@kno10 @vigna just highlighting how another high-performance library does it. Clearly, Agrona is not meant as drop-in replacement for standard collections but for specific use cases.

I do like the explicit rewind() approach as well.

from fastutil.

kno10 avatar kno10 commented on August 26, 2024

Why was this closed?
Is there an option to reuse/rewind an iterator now?

from fastutil.

mdproctor avatar mdproctor commented on August 26, 2024

+1 for explicit rewind() / reset()

from fastutil.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.