Git Product home page Git Product logo

kotlin-parallel-operations's Introduction

Parallel Operations for Kotlin

Parallel map, reduce, and various indexed and in-place operations on collections for Kotlin Multiplatform using coroutines.

The parallel map implementation is called .mapParallel(). It is implemented like this.

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.

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.

Installation

The library is published in the Maven Central repository. Include this line in your module build.gradle file.

dependencies {
    implementation("io.github.cvb941:kotlin-parallel-operations:2.0.0")
}

Future

In the future, I would like other transformation functions to be implemented.

kotlin-parallel-operations's People

Contributors

cvb941 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

kotlin-parallel-operations's Issues

Multiplatform artifacts

It would be great if this library would also be usable in multiplatform projects by adding artifacts for iosArm64, iosSimulator and iosX64 and the likes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.