droc60607 2017-10-21 15:01
浏览 504
已采纳

哪些协程(goroutines和kotlin协程)比较快? [关闭]

Kotlin corutines is sugar for finite state machine and some task runner (for example, default ForkJoinPool). https://github.com/Kotlin/kotlin-coroutines/blob/master/kotlin-coroutines-informal.md#implementation-details

In other words, there is no runtime coroutines in java/kotlin runtime yet (but this can change with http://cr.openjdk.java.net/~rpressler/loom/Loom-Proposal.html ). Kotlin coroutine is just sequential of tasks, which are executed one by one. Each task can be executed in any thread from thread pool.

Go runtime supports "coroutines". But goroutines is not the real coroutines. Goroutines does not allow to set yield points in program. Also, Go does not allow to set custom thread pool. You can to set only size of threads in default pool.

First difference between kotlin coroutines and goroutines is Go runtime manages which coroutine is running at this moment. When goroutine are blocked at some IO operation (or synchronization primitives), Go choices next Job to execute it. In JVM there is no intellectual job switching in such terms.

Because of this, Go can cheaply change currently running job. Go has only to change few registries https://groups.google.com/forum/#!msg/golang-nuts/j51G7ieoKh4/wxNaKkFEfvcJ. But some people say, that JVM can use stack of threads instead of using registers. So there is no saving and loading of registers at all.

The second difference between kotlin coroutines and goroutines is type of coroutines. Kotlin coroutines is stackless coroutines. Goroutines are stackful coroutines. All state of Kotlin coroutines are stored in Kotlin context, which is stored in heap. Goroutines state is stored in registers and thread stack.

I want to know, which coroutines (goroutines and kotlin coroutines) are faster in IO bound tasks? CPU bound tasks? What about memory consumption?

  • 写回答

1条回答 默认 最新

  • doujiang1993 2017-10-21 16:09
    关注

    Coroutines in Kotlin are implemented in a different way than goroutines in Go, so which one is "faster" depends on the problem that you are solving and the kind of code you are writing.

    In general, it is very hard to tell in advance which one is going to work better for a problem you have at hand. You have to run benchmarks for your particular workloads to figure it out. However, here is a general summary of key differences that should give you some guidance.

    • Kotlin coroutines require less memory per simple instance than Go goroutines. A simple coroutine in Kotlin occupies only a few dozen bytes of heap memory, while a Go goroutine starts with 4KiB of stack space. It means, that if you are planning to have literally millions of coroutines, then coroutines in Kotlin might give you an edge versus Go. It also makes Kotlin coroutines better suited for very short-lived and small tasks like generators and lazy sequences.

    • Kotlin coroutines can go to any stack depth, however each invocation of suspending function allocates object in heap for its stack. An invocation stack in Kotlin coroutines is currently implemented as a linked list of heap objects. In contrast, goroutines in Go use linear stack space. This makes suspension on deep stacks more efficient in Go. So, if the code you are writing suspends very deep down the stack, you may find that goroutines are more efficient for you.

    • Efficient asynchronous IO is a very multidimensional design problem. An approach that is efficient for one kind of application may not give the best performance to another one. All IO operations in Kotlin coroutines are implemented by libraries written in Kotlin or Java. There is a huge variety of IO libraries available to Kotlin code. In Go asynchronous IO is implemented by Go runtime using primitives that are not available to general Go code. If Go approach to implementing IO operations is well suited to your application, then you might find that its tight integration with Go runtime gives you an advantage. On the other side, in Kotlin you can find a library or write one yourself that implements asynchronous IO in a way that is best suited to your application.

    • Go runtime takes complete control of scheduling goroutines execution on the physical OS threads. The advantage of this approach is that you don't have to think about it all. With Kotlin coroutines you have fine-grained control on the execution environment of your coroutines. This is error-prone (e.g. you may simply create too many different thread-pools and waste your CPU time on context switching between them). However, it gives you ability to fine-tune your thread allocation and context switches for your application. For example, in Kotlin it is easy to execute your whole application or a subset of its code in a single OS thread (or thread pool) to completely avoid switching contexts between OS threads just by writing an appropriate code for that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?