https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
m

Mattias Flodin

06/14/2022, 1:47 PM
In a product of ours we have had many performance issues connected to the network latency between application and database server. There are many trivial SQL queries executed in sequence and for each query there's a network round trip. Critically the database only permits a single query at a time on a given connection / transaction so I can't hide the latency by running the queries in parallel. Instead of "real" parallel execution I had an idea to develop a coroutine dispatcher that allows each coroutine to issue a query, but it queues them up and sends them in a single batch (one call to Statement.execute() with multiple statements or using executeBatch()). Each query is associated with the continuation that will process its results, so once the result sets come in I can dispatch them all through multiple resumeWith() calls. My problem is, how do I know when to stop waiting for more queries to be queued up and actually make the JDBC call? Can the coroutine dispatcher somehow detect when await() is called (or there is an implicit wait) on any of its queued continuations? Basically I want to continue queuing queries up until the originating control flow enters a waiting state.
a

Adam Powell

06/14/2022, 2:42 PM
The dispatcher is the wrong unit to try to manipulate; you can do what you're describing through your suspending query function
m

Mattias Flodin

06/14/2022, 2:45 PM
So the suspending query function... queues up the query, right? But then remains the question, how to know when to send the batch?
Do I yield and then signal the share queue that it should execute the batch?
a

Adam Powell

06/14/2022, 2:47 PM
how does a caller signal to you that they've enqueued the full batch?
and does it matter?
how long are you willing to wait to form a batch vs. sending queries serially?
m

Mattias Flodin

06/14/2022, 2:53 PM
I'm imagining that I have a block of code (a scope in a method) that fires up multiple coroutines. Eventually execution enters a point when all coroutines that were started, including the top coroutine, are all suspended, all waiting for some query to finish. That's when I want to fire off the batch to and subsequently release all the waiting continuations when the results come back. I'm willing to wait as long as any coroutine is still running and able to queue up more queries, but only within this particular scope of execution (say, a REST query being serviced).
There will likely be internally dependent queries so once you resume the initial continuations there will be a bunch of new ones being queued up.
a

Adam Powell

06/14/2022, 3:05 PM
the coroutines machinery works in layers where lower layers by design don't have knowledge of the semantics of layers above. Dispatchers/ContinuationInterceptors only know how to modify the way that continuations resume; they know nothing about why a coroutine suspended or when/why one will resume. Jobs sort of know about vague structural dependencies between coroutines but again, no concept of why, and there aren't useful intermediate non-terminal states that you can use to represent, "I am still running but I am waiting for a very specific kind of result"
from either of those layers you can't know whether something is suspended waiting for a query vs suspended waiting on a
delay
or similar vs. suspended waiting on some aggregated result of several queries running in different coroutines
m

Mattias Flodin

06/14/2022, 3:11 PM
Ok. So you don't see any way of using a coroutine based abstraction to hide the details of the batching needed to remove latency then?
a

Adam Powell

06/14/2022, 3:12 PM
I didn't say that, I said that you don't have enough info to be able to do it from a dispatcher 🙂
or from other notions of local "idleness" because they suffer from the same limitations
so either you need to accept that batching is going to be its own thing that determines when a batch is ready to go on its own without trying to monitor idleness of related operations, or you'll need to have the client give an explicit signal of some sort. That explicit signal might come from something like a dsl-scoped block of code reaching the end, but it's still "explicit" from the standpoint of client layering
m

Mattias Flodin

06/14/2022, 3:19 PM
Yeah the problem is that it's hard to explicitly say when it's time to run the batch since there are multiple levels in the call chain and each level can have queries that are not dependent on the other's results (and can hence run in the same batch). The model I have in my head is that of a build system such as GNU Make: I feed it a dependency graph, and from that it implicitly figures out what it can run in parallel (i.e. in a batch) and what needs to wait because it depends on results from the previous tasks. And I was hoping that the coroutine machinery would have access to such a dependency tree for jobs based on which coroutine waits on what coroutine. But I guess I could make a DSL with lambda functions for something more akin to a build system instead.
a

Adam Powell

06/14/2022, 3:22 PM
it has no such dependency tree and really, it probably shouldn't. Relying on such a thing is always going to be fragile since it's so easy to construct a scenario where 3rd party code can suspend in such a way that there's a semantic dependency that isn't represented structurally within the system. Expanding the structure to be able to model all possible use cases would make the whole system unwieldy and possibly perform badly
m

Mattias Flodin

06/14/2022, 3:24 PM
I see. Thanks for taking the time to explain.
a

Adam Powell

06/14/2022, 3:26 PM
I think that as you work through this you might find that creating a precise dependency tracking setup doesn't perform any better than accumulating queries from a channel and then sending the whole batch after some short time delay
m

Mattias Flodin

06/14/2022, 3:28 PM
Perhaps. Or I could issue the first query right away and while waiting for the results of that I keep queuing up subsequent queries.
👍 3
a

Adam Powell

06/14/2022, 3:28 PM
it may even end up performing worse depending on how subsequent/otherwise unrelated queries get stacked up
yeah, that's another idea too
play with it and profile
j

Joffrey

06/15/2022, 9:18 AM
@Mattias Flodin The last thing you suggested is what is sometimes referred to as "natural batching" - not time-based, not size-based. When the "actor" (the DB) is ready, it takes all available elements from the queue and that's your new batch. While the actor is working on a batch, all new queries are enqueued, waiting for it to be ready. That might work well enough for you
👍 3
m

Mattias Flodin

06/15/2022, 9:45 AM
Ah yes, I've had to deal with that issue with SharedFlow previously and solved it in a pretty roundabout way by keeping a separate event history that is checked simultaneously as the flow is polled for an event. Probably not a good solution for the general case.
m

Marc Knaup

09/04/2022, 11:49 PM
Hey there, I just found this thread as I’m trying to solve basically the same problem. I’m trying to build Facebook’s DataLoader in Kotlin using coroutines. DataLoader is made for Node.js and basically uses the end of the current event loop cycle to dispatch batch of “load” events. The goal is to use it in a GraphQL project to batch database queries that are executed in parallel when resolving fields. Since coroutines aren’t event loop-based this poses quite a challenge. The only idea I have so far that will likely work is to dispatch a batch after a certain delay plus optionally using a manual trigger. That certainly adds a performance penalty of at least 1ms per batch – maybe less if I implement an alternative to
delay
that supports sub-millisecond delays. @Mattias Flodin basically said that would be my ideal scenario too:
Eventually execution enters a point when all coroutines that were started, including the top coroutine, are all suspended, all waiting for some query to finish. That’s when I want to fire off the batch to and subsequently release all the waiting continuations when the results come back.
Calling
DataLoader.load(…)
makes it explicit that we’re waiting for something and once a batch is dispatched all loads are combined into one query. @Adam Powell I don’t fully understand your point how layering makes this impractical or impossible. What exactly is a layer? Are lower layers opaque to higher layers? Is there a good source to read about the architecture? Is it not possible to wait for all execution within that scope to be suspended, then execute some logic (dispatch a batch, which adds another suspended execution), and only then allow all executions to resume again? It doesn’t matter if a suspension is a query, delay, or anything else. “wait for all execution within that scope to be suspended” would just be the equivalent to Node.js’ end of currently event loop cycle and allowing the execution to resume equivalent to resuming the event loop.