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.

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集