https://kotlinlang.org logo
#announcements
Title
# announcements
a

Andrew Gazelka

10/01/2019, 1:02 AM
is there an idiomatic way to process an infinite sequence in parallel?
g

gildor

10/01/2019, 1:29 AM
Sequences are sequential by definition, so there is no way to make them parallel. For parallel sequence of events you can use Flow
a

Andrew Gazelka

10/01/2019, 1:35 AM
No what I am saying is something along the lines of
{numbers from sequence} -> {split into subsequeces} -> {do computation in parallel on each sub sequence}
g

gildor

10/01/2019, 1:47 AM
Just split into subsequeces, run computation on own thread
a

Andrew Gazelka

10/01/2019, 1:48 AM
well I guess what I am asking is if there is an idiomatic way to split the sequence
would be cool if there were something like a grouping that could be used for an inf sequence
g

gildor

10/01/2019, 1:50 AM
You can use
Sequence.chunked
, which emits list of required size
not exactly subsequence. but also sequence is not really needed, because anyway you should buffer some amount of items to process
a

Andrew Gazelka

10/01/2019, 1:53 AM
buffering seems like it would complicate the code
I am simply trying to have integers
[2, inf)
and split and see if the number is prime concurrently
g

gildor

10/01/2019, 1:53 AM
One call of
chunked(SIZE)
doesn’t look complicated
a

Andrew Gazelka

10/01/2019, 1:53 AM
ah ok fair
g

gildor

10/01/2019, 1:55 AM
but I still think that it’s pretty limited solution, essentially, there is no way to return result, it’s fire and forget
a

Andrew Gazelka

10/01/2019, 1:56 AM
hmmm
g

gildor

10/01/2019, 1:58 AM
I believe Flow (or any other Reactive framework) is much better choice
a

Andrew Gazelka

10/01/2019, 1:58 AM
Copy code
private fun primes() {
    val threads = 6
    val infSequence = generateSequence(2) { it + 1 }
    runBlocking {
        for (i in 0 until threads) {
            launch {
                infSequence.filter { it % threads == i }
                    .filter { isPrime(it) }
                    .forEach { println(it) }
            }
        }
    }
}
eh
yeah probably
g

gildor

10/01/2019, 1:58 AM
isPrime always blocking, cannot be used in parallerl
it will block sequence
a

Andrew Gazelka

10/01/2019, 1:59 AM
it is my own implementation
even tho probably less efficient I’m guessing
g

gildor

10/01/2019, 1:59 AM
You already have coroutines, why not use flow that gives everything out of the box
and much more idiomatic
a

Andrew Gazelka

10/01/2019, 2:00 AM
how would you idiomatically do this with a flow
g

gildor

10/01/2019, 2:03 AM
just make
isPrime
suspend function that runs on particular dispatcher with target parallelism
a

Andrew Gazelka

10/01/2019, 2:04 AM
I am so confused
why would
isPrime
be suspending… it doesn’t need to suspend?…
g

gildor

10/01/2019, 2:05 AM
To run on target dispatcher
because it’s your bottleneck
a

Andrew Gazelka

10/01/2019, 2:05 AM
ah I see I think
g

gildor

10/01/2019, 2:06 AM
there are a few ways actually, one more is just blocking
isPrime
, but you split the list and use flatMapConcat
Actually, for this particular task with prime number, Java’s Fork/Join framework probably the best solution, just because it’s optimized for CPU bound tasks and have good job stealing algorithm, but to make it performant you have to batch work to threads by splitting sequence by chunks
d

Dennis Schröder

10/01/2019, 7:31 AM
Take the time to carefully read this ... https://kotlinlang.org/docs/reference/coroutines/flow.html Flow is what you need.
g

gildor

10/01/2019, 7:33 AM
Honestly not sure that pure Flow will be faster for this use case with prime number ir you launch coroutine per each item, with chunked/windowed it possible to optimise it much more, but those operators are not available in 1.3 yet
m

Matteo Mirk

10/01/2019, 8:10 AM
for deeper support try asking also on #coroutines
👍🏼 1
3 Views