英文:
Comparing usage of Clojure's go-blocks vs. Go's goroutines
问题
我正在尝试理解Clojure的go-blocks和Go的goroutines的实现特性以及由此产生的性能差异。
在Clojure中,>!!
和<!!
会阻塞当前线程,这意味着它们只能在显式创建线程时使用(否则只有主线程会被阻塞)。>!
和<!
是parking操作,因为go
宏管理着一个隐式状态机来处理多个"进程"。
然而,在Go中,这一切都隐藏在运行时中,goroutines扮演了这两种角色。
所以,可以说只使用parking版本的Clojure代码等效于Go吗?由于go
宏中有一个线程池,这有点类似于Go底层的goroutine线程池。不过性能会更差,因为Go在运行时处理这些,而Clojure在Clojure代码中显式管理它。
英文:
I'm trying to understand the implementation characteristics and the resulting performance of Clojure's go-blocks vs. Go's goroutines.
In Clojure >!!
and <!!
block the current thread, meaning that they can only be used when threads are created explicitly (otherwise the "only" main thread is blocked). >!
and <!
are parking because the go
macro manages an implicit state machine that juggles multiple "processes".
However, in Go this is all hidden in the runtime, where goroutines play both roles.
So is this correct to say that Clojure code would be equivalent to Go when only using the parking versions? Since there's a thread-pool in the go
macro, this is kind-of like Go's thread pool underlying goroutines. Except that performance will be worse, because Go does this in the runtime while Clojure manages it explicitly in Clojure code?
答案1
得分: 2
总的来说,是的,Clojure的go
宏的行为与使用"parking"的<!
和>
时的"goroutines"类似。
然而,任何性能影响可能高度依赖于工作负载和环境,如操作系统、核心数、堆大小等。
Clojure的go
宏将其内容编译成由许多小函数组成的状态机,这些函数通过标准的Java线程池进行调度。目前,Clojure的实现中没有实现其他调度。
Go语言有自己的内部调度器用于"goroutines",它可以根据内部运行时知识进行更复杂的调度。它还提供了对"goroutines"的运行时检查,而Clojure的go
宏则不支持这一点。
英文:
Broadly speaking, yes, the behavior of Clojure's go
macro is similar to "goroutines" when using the "parking" <!
and >!
.
However, any performance effects are likely to be highly-specific to both the workload and the environment: operating system, number of cores, heap size, etc.
Clojure's go
macro compiles its contents into a state machine composed of lots of little functions, which are dispatched through a standard Java thread pool. There isn't any other scheduling implemented in the Clojure implementation right now.
The Go language has its own internal scheduler for "goroutines," which may be able to do more sophisticated scheduling based on internal runtime knowledge. It also provides runtime inspection of "goroutines" which is not possible with Clojure's go
macro.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论