# Zippy parallel vector additions in Kotlin (seems like coroutines + locking = slower)

**URL:** <https://discuss.kotlinlang.org/t/zippy-parallel-vector-additions-in-kotlin-seems-like-coroutines-locking-slower/10436>\
**Category:** Support\
**Created:** [November 20, 2018, 6:35pm UTC](https://discuss.kotlinlang.org/t/zippy-parallel-vector-additions-in-kotlin-seems-like-coroutines-locking-slower/10436 "2018-11-20T18:35:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjaminhill](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@benjaminhill](https://discuss.kotlinlang.org/u/benjaminhill)\
**Post date:** [November 20, 2018, 6:35pm UTC](https://discuss.kotlinlang.org/t/zippy-parallel-vector-additions-in-kotlin-seems-like-coroutines-locking-slower/10436/1 "2018-11-20T18:35:18Z")

</div>

_Disclaimer: this may not have a good answer._

I’m working with BufferedImages, which means slooooow code, but code that screams “you have more CPU cores, please run me in parallel!”

Sometimes I’m getting a BufferedImage pixel-\>per-pixel-hue:IntArray… which means calling a function for each pixel:

```auto
(raster.dataBuffer!! as DataBufferInt).data.asIterable().map { pixel ->
  getHue(
	  red = pixel shr 16 and 0xFF,
	  green = pixel shr 8 and 0xFF,
	  blue = pixel and 0xFF
  )
}

```

Other times I’m performing some “vector math” on the RGB values results to get running sums between frames:

```auto
(colorImage.raster.dataBuffer!! as DataBufferByte)
	.data.asIterable().chunked(if (colorImage.alphaRaster == null) 3 else 4)
	.forEachIndexed { pixelLocation, channels ->
	    // ignore alpha channel 3 if it exists
	    red[pixelLocation] += channels[2].toInt() and 0xFF
	    green[pixelLocation] += channels[1].toInt() and 0xFF
	    blue[pixelLocation] += channels[0].toInt() and 0xFF
	}

```

Kotlin is fantastic for this: nice **chunked** operators, simple map syntax. I’m very happy.

But images are big, so it is slow. I’ve got a large collection of stuff to iterate over, and I’ve tried making it go faster using [parallel maps with coroutines](https://stackoverflow.com/questions/34697828/parallel-operations-on-kotlin-collections), but it slows way down. I’m guessing this is due to locking, or memory bottlenecks, or “you can’t beat a `for(i in 0..size) { ... }`” loop.

Is there a way in Kotlin to burn up all my cores in such a way that would result in faster completion time? I’m fine with using 4x more electricity if it means the app goes 2x as fast.

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [November 20, 2018, 11:27pm UTC](https://discuss.kotlinlang.org/t/zippy-parallel-vector-additions-in-kotlin-seems-like-coroutines-locking-slower/10436/2 "2018-11-20T23:27:16Z")

</div>

You may try:

```auto
runBlocking {

	(colorImage.raster.dataBuffer!! as DataBufferByte)
		.data.asIterable()
		.chunked(if (colorImage.alphaRaster == null) 3 else 4)
		.mapIndexed { launch { pixelLocation, channels ->
		    // ignore alpha channel 3 if it exists
		    red[pixelLocation] += channels[2].toInt() and 0xFF
		    green[pixelLocation] += channels[1].toInt() and 0xFF
		    blue[pixelLocation] += channels[0].toInt() and 0xFF
		} }
		.forEach { it.join() }
}

```
