Cgo experiments
Hybrid meetup #35 took place 2023-04-18 19:00 at Basislager Leipzig and was all about Cgo.
Cgo allows to bridge C and Go. From C, we can connect to other languages, like C++, too. Use cases are legacy code or performance. Implementing a pseudo-random walk (using an LCG) in both Go and C shows that C can be up to 4x faster for this particular task.
C 1 -1 29.553µs
GO 1 -1 84.303µs
C 1000 -12 41.799µs
GO 1000 26 107.295µs
C 1000000 -1810 22.837387ms
GO 1000000 -2 83.182797ms
C 1000000000 15894 6.25975724s
GO 1000000000 -44632 25.620427056s
One example was concerned with wrapping ggml, a lightweight tensor library for machine learning - xcgoggml. A threading mystery remains.
Experiment details can be found here: https://github.com/miku/cgosamples
Zig cross-compilation
The zig compiler (LLVM) can build real staticly linked binaries from a Go project with C dependencies, such as sqlite3. Example repo: https://github.com/klingtnet/cross-compile-with-zigcc.
Misc
- Cgo is a not Go, many guarantees are given up, maintenance of mixed C and Go code is much more difficult - rather than use CGO, maybe a grpc or other interface may keep C and Go world better separated
- To wrap C++, we need to create a C façade
- Go has a nice ast
- GRPC performance in Go may suffer, if interfaces are used (due to overhead of reflection)
- oapi-codegen is an openapi code generator for Go (and an alternative to the swagger tool)
- Go’s type system can be limiting, when working with Web APIs or SDKs
- Simple Markup Language
- Parsec, Participle
- YAML/HELL
- Using symbols straight from a dynamic library via dlopen
- A rundown of a benchmark across Python, C, and BLAS, for a HPC and competetive programming blog: https://en.algorithmica.org/hpc/complexity/languages/
- Unbuffered terminal output in Go is slow. Try to wrap
os.Stdout
in abufio.Writer
if you need to print a lot of text.
We also briefly discussed LLMs, as we ran alpaca model ggml-alpaca-7b-q4.bin in the background, which wrote some simple SQL and thought of a haiku about the Go programming language:
Did you find a nice haiku?
Join our meetup and let us know!