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
IntIterator and LongIterator implement OfInt,OfLong #591
base: master
Are you sure you want to change the base?
IntIterator and LongIterator implement OfInt,OfLong #591
Conversation
This makes it easier to use in standard library situations that expect and OfInt or OfLong. The downside is that while practicaly compatible due to autoboxing existing users of the next() method might be slowed down if they do not migrate to nextLong().
Relates with #461 I may require the same API change, hence package change. |
Is that true? Certainly, that may happen if 'scalarisation' fails, but I am interested in examples where the iterator would be such that scalarisation would not happen. Could someone volunteer to benchmark this? |
My naive expectation is that if you take an integer, you box it, return the box, and then immediately unbox it to an integer... then the JIT will just optimize away the boxing. At least this ought to be true for 'some' versions of Java. This presentation makes me hopeful : https://www.youtube.com/watch?v=p1MhRBYS-k0 In any case, it is an empirical issue. We can test it out. |
@lemire I assumed as much in most cases. But the comment on IntIterator suggests that there are cases where scalarization used to fail and
With basically identical text in the IntIterator. |
Yes, I am sure that 'on some tests' this was true. We should not assume that it is still true today. Again, I am calling for folks to benchmark this. It may very well be that your PR has no practical performance impact. In other words, I am very supportive of this PR. My assumption is that it is all good. (Of course, we should test it out.) |
Well I ran into an issue :( when adapting the existing
this might lead to more issues downstream than I expected. |
Why would you need to adapt it? |
Oh to see the difference of running the test with |
@JervenBolleman Right but I think care must be needed not to extend it to |
Yes. That's an example where this PR would break existing code. :-( |
Here comes a very large table with benchmark results from my desktop with JDK1.8 on a cheap (AMD Ryzen 3 3200G).
Which on a quick tired eyeballing seems that
Next up JDk 17.0.4 |
JDK 8 is old at this point. |
Here comes the JDK-17 batch. Which I find surprising and very interesting.
Just leaving the results here now that they are ready. Analysis to follow. |
/** | ||
* @return whether there is another value | ||
*/ | ||
boolean hasNext(); |
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.
What about keeping these methods, adding .nextInt with a default implementation? it would enable 0.X compatibility.
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.
Ok. Both interface has a conflict over .next() (int or Integer). This was discussed in
-> is the need motivating by this PR covered by wrapping getIntIterator()
in a RoaringOfInt
as done in
I am marking this PR as 'work needed'. Before we adopt this, we need some more analysis. |
This makes it easier to use in standard library situations that expect and OfInt or OfLong. The downside is that while practicaly compatible due to autoboxing existing users of the next() method might be slowed down if they do not migrate to nextLong().
SUMMARY
Since java 8 there has been an interface OfInt and OfLong that is similar to the IntIterator and LongIterator in ideas.
However instead of next returning the primitive, next returns an autoboxed one, with new methods nextInt/Long to return a primitive.
This pull request changes uses the two primitive Iterators to OfInt and OfLong. This makes it easier to interoperate with other code that expects these two interfaces. But with a significant downside that existing of
next()
may have a significant regression in performance.I personally don't think this should be merged before the 0.10.x release series but wanted to open this pull request for discussion if and when such a patch should be merged.
Procedure to make this patch is trivial. Use rename re-factoring of the existing next to nextInt/Long methods then add the OfInt/Long to the existing Int/LongInterface.
Automated Checks
./gradlew test
and made sure that my PR does not break any unit test../gradlew checkstyleMain
or the equivalent and corrected the formatting warnings reported.