Embedding Go into Ruby applications

Marlon Henry Schweigert
Magrathea
Published in
3 min readJan 31, 2018

--

I am passionate about Ruby, but its execution time compared to other languages is extremely high, especially when we want to use more complex algorithms. In general, data structures in interpreted languages become incredibly slow compared to compiled languages. Some algorithms such as n-body and fannkuch-redux can be up to 30 times slower in Ruby than Go. This is one of the reasons I was interested in embedding Go code in a Ruby environment.

For those who do not know how shared libraries operate, they work in a similar way as DLLs in Windows. However, they have a native code with a direct interface to the C compiler.

Note Windows uses the DLL system, and in this case, this does not necessarily have to be in native code. One example is DLLs written in C#, which runs on a virtual machine. Because I do not use windows, I ended up not testing if it is possible to perform these steps on it.

You need Golang 1.5 or higher and Ruby 1.8.7 or higher.

Creating the Go library

It would be like any library, but it must have some peculiar characteristics. For signing external functions to your library, you will need to use the C language typing. For the rest of the code, you can convert to the standard Go typing.

To do this include the C library:

import “C”

Remember that this is the Achilles’ heel of Go. Try to create small interfaces where only few data is carried on. In general, data type translation is extremely slow when converting from Go to C types.

Translation of C data to Go

Function export signature

You need to tell the Go compiler which functions will be public in the library by adding the export comment above your function:

import "C"//export my_add
func my_add(a, b C.int) C.int {
return a + b
}
func main() {
// This is necessary for the compiler.
// You can add something that will be
// executed when engaging your library
// to the interpreter.
}

Note I know that the standard of nomenclatures in Go is different from what we found in this example, but I’ve created the habit of writing interfaces using the Ruby style. For this reason, the names of the functions are written using snake_case and not camelCase.

Remember that the exported functions must be in your main package. To compile, just run:

go build -o my_lib.so -buildmode=c-shared my_file.go

Writing the interface with the shared library in Ruby

This is the fastest way to embed Go code into a Ruby environment, however, remember that in this way the call time of the go interface will not be so fast.

Add to your Gemfile the following gem:

gem 'ffi'

Or manually install it: gem install ffi.

Just create a module and write your function signatures inside the Ruby package. This is like a .h file:

require 'ffi'module Foo
extend FFI::Library
ffi_lib './my_lib.so'
attach_function :my_add, [:int, :int], :int
end
puts Foo.my_add(2, 2)
# => 4

What I noticed by running native Go code in the Ruby environment?

Integrating Go and Ruby is fast and efficient for certain situations. Running the native code in Go brings several advantages, bringing you various graphical, database and efficient data structures libraries already implemented in Go and the efficiency of Go routines.

Remember that this applies when we use little data translations between Ruby and Go. This will only be advantageous if you have a huge real gain like the algorithms I quoted above, n-body and fannkuch-redux, offsetting this loss by calling a function through the Go C Library.

--

--