-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
84 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Parallel Operations for Kotlin | ||
Parallel *map*, *reduce*, and various indexed and in-place operations on collections for Kotlin using coroutines. | ||
|
||
The parallel *map* implementation is called *.mapParallel()*. It is implemented like this. | ||
```kotlin | ||
suspend fun <T, R> Iterable<T>.mapParallel(transform: (T) -> R): List<R> = coroutineScope { | ||
map { async { transform(it) } }.map { it.await() } | ||
} | ||
``` | ||
|
||
Example of using the parallel *map* operation. | ||
```kotlin | ||
fun showCase() { | ||
val list = listOf(1,2,3) | ||
runBlocking(Dispatchers.Default) { | ||
var mappedList = list.mapParallel { it * 2 } // Results in [2,4,6] | ||
} | ||
} | ||
``` | ||
|
||
There is also the parallel *reduce* operation with chunked variations, which can be used to perform **associative** operations on a collection, like *sum*. | ||
|
||
**Note:** If you want to achieve multithreading, make sure to run the coroutine with the Default dispatcher. | ||
|
||
## Chunked operations | ||
Chunked operations improve performance since they split the collection into just a couple of segments, | ||
which are processed each by a single thread. That benefits from data locality and lesser thread management. | ||
It is particularly useful (pretty much needed for operations like sum) in the reduce operation when using multithreading, | ||
since each thread takes one chunk that it reduces on its own. After all coroutines finish, their results are then reduced again to the final result. | ||
|
||
## Gradle | ||
The library is published in the Maven Central repository. | ||
Include this line in your module build.gradle file. | ||
```gradle | ||
dependencies { | ||
implementation("io.github.cvb941:kotlin-parallel-operations:2.0.0") | ||
} | ||
``` | ||
|
||
## Future | ||
In the future, I would like other transformation functions to be implemented. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import com.vanniktech.maven.publish.SonatypeHost | ||
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl | ||
|
||
plugins { | ||
kotlin("multiplatform") version "1.9.23" | ||
id("org.jetbrains.kotlinx.benchmark") version "0.4.10" | ||
id("org.jetbrains.kotlin.plugin.allopen") version "1.9.23" | ||
id("maven-publish") | ||
id("com.vanniktech.maven.publish") version "0.28.0" | ||
} | ||
|
||
group = "net.kusik" | ||
group = "io.github.cvb941" | ||
version = "2.0.0" | ||
|
||
repositories { | ||
|
@@ -90,15 +91,34 @@ allOpen { | |
annotation("org.openjdk.jmh.annotations.State") | ||
} | ||
|
||
publishing { | ||
repositories { | ||
maven { | ||
name = "GitHubPackages" | ||
url = uri("https://maven.pkg.github.com/cvb941/kotlin-parallel-operations") | ||
credentials { | ||
username = project.findProperty("gpr.user")?.toString() ?: System.getenv("GITHUB_ACTOR") | ||
password = project.findProperty("gpr.key")?.toString() ?: System.getenv("GITHUB_TOKEN") | ||
mavenPublishing { | ||
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL, automaticRelease = true) | ||
|
||
signAllPublications() | ||
|
||
pom { | ||
name.set("Parallel Operations") | ||
description.set("Parallel map, reduce and more using coroutines in Kotlin.") | ||
inceptionYear.set("2019") | ||
url.set("https://github.com/cvb941/kotlin-parallel-operations/") | ||
licenses { | ||
license { | ||
name.set("MIT License") | ||
url.set("https://opensource.org/licenses/MIT") | ||
distribution.set("repo") | ||
} | ||
} | ||
developers { | ||
developer { | ||
id.set("cvb941") | ||
name.set("Lukas Kusik") | ||
url.set("https://github.com/cvb941/") | ||
} | ||
} | ||
scm { | ||
url.set("https://github.com/cvb941/kotlin-parallel-operations/") | ||
connection.set("scm:git:git://github.com/cvb941/kotlin-parallel-operations.git") | ||
developerConnection.set("scm:git:ssh://[email protected]/cvb941/kotlin-parallel-operations.git") | ||
} | ||
} | ||
} |