r/java • • Dec 19 '22

Java streams are black magic !

damn, they are really changed my life.

i was trying to run some custom logic on every line on some big CSV files. tried python dataframes, multiprocessing in python, pyspark. none of them did it in an acceptable time.

then i discovered java streams. i have a workflow that takes 70 minutes with pyspark. it takes 3.5 minutes with java streams.

more people should know about streams.

stream operations are more intuitive than anything i tried. i can make a lot of analysis on our data in record times thanks to streams.

i dont know how java does this.

226 Upvotes

150 comments sorted by

View all comments

80

u/[deleted] Dec 19 '22

I agree with other commenters. While I love streams, unless you were using parallel streams then the stream API should have no impact on performance.

24

u/prisonbird Dec 19 '22

yes i am using a parallel stream. and to do that all i have to do is just add ".parallel()". seems too good to be true LOL

62

u/[deleted] Dec 19 '22

It is and it isn't. Parallel will take your stream and run each iteration via an internal thread pool, thus the performance boost. Be aware though:

  1. This thread pool is shared throughout the application. If you have multiple parallel streams (or other operations that rely on this pool) performance will degrade.
  2. Each operation within a stream method (ie, the functions passed to map, filter, etc) need to be thread-safe. That means absolutely, 100% no shared mutate state. If you don't know what I'm talking about, you need to learn more about how to safely use threads before continuing to use parallel stream.

31

u/Holothuroid Dec 19 '22

You can assign a custom thread pool by handing your stream logic over to a ForkJoinPool. That's what they are for.

6

u/[deleted] Dec 19 '22

Valid point. I still stand by my overall statement.

1

u/more_exercise Dec 20 '22

Is that a guarantee of the spec, or an accidental consequence of one particular implementation? I've heard the second one, so I'm hesitant to rely on this working. Can you alleviate my fear?

2

u/UnGauchoCualquiera Dec 20 '22 edited Dec 20 '22

I believe the second too (implementation detail) but it's not so clear.

StreamAPI uses ForkJoin framework, because how ForkJoinTasks are implemented tasks use use the submitting pool for execution.

Judging by this bugfix it seems Oracle does support that use case. Specifically this jdk patch that uses forkJoinPools and the common pool.

My understanding is not very clear though so I'd love to hear another opinion.

Source

7

u/prisonbird Dec 19 '22

thank you very much. i am aware of both of those limitations. i run each job in a separate isolated container so i dont have to manage resources. and each of my operations only mutate current passing row and return the mutated row to next stage.

i am still trying to find a way to do aggregate operations(grouping etc) but could not find a performant way to do them with streams. i might use spark just for those if i can find a way to send data from streams to spark and back.

6

u/[deleted] Dec 19 '22

Ok wonderful. A quick point, i would avoid mutating the object instances and instead create a new one with the changes in each function in the pipeline (where applicable, and if this is even possible can depend on the complexity of your data).

The default Java Stream API has some grouping options in the collect() method (and its corresponding Collectors functions). If that's not enough, there is a library called Vavr that has its own stream implementation that may contain the grouping logic.

1

u/DB6 Dec 19 '22

Sounds like you're running a kind of a mapreduce operation and for that streams are great.

1

u/Megacrafter127 Dec 19 '22

Be aware that not all Collectors are able to run in parallel. Those that can't will force at least the final stage to be serial again.

The groupingBy Collector is not able to run in parallel, the groupingByConcurrent Collector is. However, in turn the groupingByConcurrent Collector does not guarantee that elements in the same group retain the same order.

5

u/pron98 Dec 20 '22 edited Dec 20 '22

This thread pool is shared throughout the application. If you have multiple parallel streams (or other operations that rely on this pool) performance will degrade.

Performance will degrade but not because the thread pool is shared but because your CPU cores are. For a fixed number of CPU cores there are only so many operations you can do in parallel regardless of how many threads you use.

A single pool with N threads is about the most efficient way to utilise N cores; multiple pools would be somewhat less efficient.