Skip to content

Instantly share code, notes, and snippets.

View fluidsonic's full-sized avatar

Marc Knaup fluidsonic

View GitHub Profile
@file:OptIn(ExperimentalTime::class)
import kotlinx.coroutines.*
import kotlin.time.*
suspend fun main() {
coroutineScope {
println("Launching tasks…")
@fluidsonic
fluidsonic / ParallelExecutor.kt
Last active January 24, 2023 05:51
Parallel coroutine execution with dynamic concurrency in Kotlin
// Dependencies:
// implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9")
// implementation("org.jetbrains.kotlinx:atomicfu:0.14.4")
import kotlinx.atomicfu.*
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.selects.*
import java.io.*
import kotlin.coroutines.*
@fluidsonic
fluidsonic / FlowShareIn.kt
Created October 11, 2020 15:36
Rudimentary Flow.shareIn with WhenSubscribed behavior until the official operator is launched
fun <T> Flow<T>.shareIn(scope: CoroutineScope): Flow<T> {
val upstream = this
var broadcastChannel: BroadcastChannel<T>? = null
val mutex = Mutex()
var subscriberCount = 0
return flow {
val activeChannel = mutex.withLock {
subscriberCount += 1
broadcastChannel ?: upstream.broadcastIn(scope).also {
@fluidsonic
fluidsonic / FlowCombineLatest.kt
Last active July 2, 2024 07:28
Fast flow combination
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
private val undefined = Any()
internal fun <T> Iterable<Flow<T>>.combineLatest(): Flow<List<T>> =
combineLatest { it }
@fluidsonic
fluidsonic / CIO.kt
Created October 12, 2020 10:46
CIO fails under high load
import io.ktor.application.*
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
@fluidsonic
fluidsonic / KtorClientBenchmark.kt
Created October 12, 2020 14:35
Testing how fast Ktor client engines are in comparison
import io.ktor.application.*
import io.ktor.client.*
import io.ktor.client.engine.*
import io.ktor.client.engine.apache.*
import io.ktor.client.engine.cio.*
import io.ktor.client.engine.jetty.*
import io.ktor.client.engine.okhttp.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
@fluidsonic
fluidsonic / CacheFlow.kt
Last active October 14, 2020 17:12
Creates a cache that uses "child" MutableStateFlows that won't get a value emitted for its own changes in order to avoid cycles.
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
/* Output:
cache collected: initial
flow1 (collector 1) collected: initial
flow1 (collector 2) collected: initial
flow2 (collector 1) collected: initial
/* Output
Collecting 2 elements…
upstream -> hot
emit: 0
resumed
collect: 0
Expensive work on 0 (isPaused = false)…
Expensive work on 0 (isPaused = false)…
Expensive work on 0 (isPaused = false)…
@fluidsonic
fluidsonic / Example.kt
Created November 4, 2020 03:10
Resource/asset hashing using Webpack in Kotlin React project
@JsName("require")
external fun asset(path: String): String
// in a React builder
img(src = asset("image.png")) {}
@fluidsonic
fluidsonic / StringBuilder.kt
Created November 24, 2020 23:28
Array.push/join-based StringBuilder for Kotlin/JS
public external interface StringBuilder
public inline fun StringBuilder.append(value: Any) {
append("$value")
}
public inline fun StringBuilder.append(string: String) {
if (string != "")