FAQ

Keith Randall wrote:

Adam, if you have a particular benchmark in mind, we'd love to see it.
It's much easier to reason about these things in the concrete instead of
the abstract.
I am thinking particularly about numbers given in the discussion from 2013
here
in https://groups.google.com/d/msg/golang-nuts/RTtMsgZi88Q/ebPUKSFsF8UJ The
consensus was that the overhead is not sooo big, as long as you don't make
millions of cgo calls, which is not something that is very obvious, eg.
when using some physics engine, or bridging Go to some desktop API like Qt
or Cocoa.

Extending example from #9704 with benchmark for normal Go call I get 2.2ns
go call vs 190ns cgo call.

// run me with: go run bench.go -test.bench=.
package main

// int rand() { return 42; }
import "C"

import "testing"

func BenchmarkCgoCall(b *testing.B) {
for i := 0; i < b.N; i++ {
C.rand()
}
}

//go:noinline
func rand() int {
switch {
} // don't inline (go<=1.6)
return 42
}

func BenchmarkGoCall(b *testing.B) {
for i := 0; i < b.N; i++ {
rand()
}
}

func main() {
testing.Main(func(string, string) (bool, error) {
return true, nil
}, nil, []testing.InternalBenchmark{
{"BenchmarkCgoCall", BenchmarkCgoCall},
{"BenchmarkGoCall", BenchmarkGoCall},
}, nil)
}

Now what if the rand() is some tiny but vital function, such as vector
multiplication in some physics engine? The call overhead can be higher than
the time to execute that tiny function itself and only choice is to rewrite
parts of the engine to Go itself.

minux wrote:
The overhead of cgo calls are definitely not because of switching stacks,
because it's just switching a few registers. If switching stack is the main
bottleneck, then switching goroutinea will see similar slowness.
I think such switch isn't a really a problem when it happens once per few
milliseconds to switch coroutine, but can be a problem when occurs one
every cgo call in some tight loop of Go code calling external API.

Of course all of that is just my random ramblings on Go applications other
than high performance (web) servers.

--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 6 of 13 | next ›
Discussion Overview
groupgolang-dev @
categoriesgo
postedMar 23, '16 at 9:32p
activeMar 24, '16 at 8:34a
posts13
users7
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase