Skip to content
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

Refactor code of WithFirstSpliterator #232

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

naftalmm
Copy link
Contributor

@naftalmm naftalmm commented Sep 5, 2020

Simplify the code of WithFirstSpliterator:

  • smaller methods
  • less overall LOC
  • replace manual thread lock management (via ReentrantLock) with more convenient synchronized blocks
  • inline accept method (as it is a one-liner) to improve readability

Tests are keep passing, perfomance is the same (even a bit higher):

Benchmark                                                  (N)   Mode  Cnt    Score    Error  Units
withFirst.WithFirstBenchmark.parallelNew                100000  thrpt   25  729,474 ± 40,193  ops/s
withFirst.WithFirstBenchmark.parallelOld                100000  thrpt   25  708,247 ± 22,885  ops/s
withFirst.WithFirstBenchmark.parallelNewShortCircuit    100000  thrpt   25  651,792 ± 19,253  ops/s
withFirst.WithFirstBenchmark.parallelOldShortCircuit    100000  thrpt   25  605,977 ±  4,831  ops/s
withFirst.WithFirstBenchmark.sequentialNew              100000  thrpt   25  845,432 ±  4,031  ops/s
withFirst.WithFirstBenchmark.sequentialOld              100000  thrpt   25  826,331 ±  3,419  ops/s
withFirst.WithFirstBenchmark.sequentialNewShortCircuit  100000  thrpt   25  432,279 ± 29,359  ops/s
withFirst.WithFirstBenchmark.sequentialOldShortCircuit  100000  thrpt   25  374,763 ± 27,017  ops/s

WithFirstSpliteratorOld class and new @Deprecated methods & benchmarks are subject for removal upon merging this PR.

add couple of deprecated methods for the sake of benchmarking;
add benchmarks to prove the absence of performance regression after refactoring;
fix javaDocs for StreamEx.withFirst methods (actually this is not directly related to refactoring, as semantics was not changed)
@coveralls
Copy link

Coverage Status

Coverage decreased (-1.5%) to 98.209% when pulling f587da1 on naftalmm:refactor_withFirst into 2be61c5 on amaembo:master.


private ReentrantLock lock;
/* package */final class WithFirstSpliterator<T, R> extends CloneableSpliterator<R, WithFirstSpliterator<T, R>> {
private static final Object lock = new Object();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your solution uses a single shared lock between all the spliterators, including totally independent that could possibly run in a separate thread pool. Sorry, but I cannot accept a pull-request that introduces a global lock.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed! My bad. Fixed this.

@@ -1764,11 +1764,6 @@ public void forPairs(BiConsumer<? super T, ? super T> action) {
* <p>
* This is a <a href="package-summary.html#StreamOps">quasi-intermediate
* operation</a>.
*
* <p>
* The size of the resulting stream is one element less than the input
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, indeed this should be changed. I forgot to update this part in version 0.6.4 when semantics of withFirst changed. However, it's better to say that the size remains the same than removing this completely. I will apply this change separately.

@loordgek
Copy link

loordgek commented Nov 4, 2023

is this good now ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants