arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2211.04986cs.DScs.DC
收藏

Kotlin协程中的快速和可扩展通道

Fast and Scalable Channels in Kotlin Coroutines

Nikita Koval, Dan Alistarh, Roman Elizarov

摘要:

在过去的十年中,异步编程已经变得非常流行:许多流行的语言都可以通过库和本地语言实现来支持这种编程模式,通常是以协程或Awatch/await结构的形式。与通过共享内存编程不同,这个概念假定通过消息传递进行隐式同步。实现这种通信的关键数据结构是会合信道。粗略地说,会合通道是一个大小为零的阻塞队列,所以send(e)和receive()操作都在等待对方,当它们相遇时执行会合。为了优化消息传递模式,通道通常配备有固定大小的缓冲区,因此send(e)-s不会挂起并将元素放入缓冲区,直到其容量被超过。这种原语称为缓冲通道。本文提出了一种快速和可扩展的算法,交会和缓冲通道。与现代队列类似,我们的解决方案基于一个无限数组,其中有两个用于send(e)和receive()操作的位置计数器,利用无条件Fetch-And-Add指令来更新它们。然而,该算法需要对这个经典模式进行重要的修改,以便支持完整的通道语义,例如缓冲和取消等待请求。我们将我们的解决方案的性能与Kotlin实现的性能以及其他学术建议进行了比较,显示出高达9.8倍的加速。为了展示其表现力和性能,我们还将所提出的算法集成到标准Kotlin Coroutines库中,取代了之前的通道实现。

英文摘要:

Asynchronous programming has gained significant popularity over the last decade: support for this programming pattern is available in many popular languages via libraries and native language implementations, typically in the form of coroutines or the async/await construct. Instead of programming via shared memory, this concept assumes implicit synchronization through message passing. The key data structure enabling such communication is the rendezvous channel. Roughly, a rendezvous channel is a blocking queue of size zero, so both send(e) and receive() operations wait for each other, performing a rendezvous when they meet. To optimize the message passing pattern, channels are usually equipped with a fixed-size buffer, so send(e)-s do not suspend and put elements into the buffer until its capacity is exceeded. This primitive is known as a buffered channel. This paper presents a fast and scalable algorithm for both rendezvous and buffered channels. Similarly to modern queues, our solution is based on an infinite array with two positional counters for send(e) and receive() operations, leveraging the unconditional Fetch-And-Add instruction to update them. Yet, the algorithm requires non-trivial modifications of this classic pattern, in order to support the full channel semantics, such as buffering and cancellation of waiting requests. We compare the performance of our solution to that of the Kotlin implementation, as well as against other academic proposals, showing up to 9.8x speedup. To showcase its expressiveness and performance, we also integrated the proposed algorithm into the standard Kotlin Coroutines library, replacing the previous channel implementations.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑