Performance

C Ruby? C Ruby Go! Go Ruby Go!

C Ruby? C Ruby Go! Go Ruby Go!

by Loren Segal

In the talk titled "C Ruby? C Ruby Go! Go Ruby Go!" at RubyConf 2016, Loren Segal discusses the integration of Ruby and Go through a tool called Gorb, which generates native Ruby extension wrappers for Go programs. Segal, a developer with experience in various languages, introduces the strengths and weaknesses of both Ruby and Go.

  • Introduction of Go and Ruby: Ruby is often viewed as a slower and less efficient programming language, yet it remains beloved for its beauty and simplicity. In contrast, Go is noted for its speed and ease of use, along with features like structural typing and fast garbage collection.
  • Overview of Gorb: Gorb is designed to simplify the process of writing performance-sensitive code in Go while integrating it within the Ruby environment. This tool helps Ruby developers who are not comfortable with C, as it generates the necessary wrappers without requiring them to write C code directly.
  • How Gorb Works: Segal explains how Gorb analyzes data structures and creates corresponding Ruby classes, allowing Ruby code to leverage the performance benefits of Go. He provides a demonstration with a Fibonacci implementation, showing how easy it is to generate native extensions using Gorb commands like 'gorp gen.' This allows Ruby developers to seamlessly call Go functionality right from their Ruby code without deep dives into boilerplate code or extensive setup.
  • Limitations of Gorb: Despite its advantages, Gorb has limitations, such as restricted concurrency support due to Ruby's Global Interpreter Lock (GIL), and it does not support some advanced features found in Go.
  • Concluding thoughts: Segal emphasizes the value of experimenting with Go from a Ruby perspective as a way to expand one's programming skills. He encourages the audience to explore new languages together to uncover their capabilities.

The session concludes with Segal inviting questions from the audience, reinforcing the collaborative spirit of learning about the integration between these two programming languages.

00:00:14.750 So I'm gonna get started. Welcome everyone to RubyConf! Hope you guys are having a good time so far. I am going to be talking about Ruby and Go, and how you can use both languages together inside the Ruby VM to write fast Ruby code.
00:00:20.610 But first, I'm going to tell you about who I am once I get my thing working here. All right, cool. So, who am I? My name is Loren Segal. I have a Twitter account and a blog that I occasionally update. You can follow me and check out what I write about random things.
00:00:38.100 I've been working on a bunch of different tools for Ruby, tools for the cloud, and I was working on games for a little bit. Most recently, I am working on music tooling at Splice. This means I've been working with many different languages and technologies, including Ruby, JavaScript, C#, Go, Objective-C, and TypeScript. So, I figured I would take this opportunity to share with you some of my experience using my two favorite languages: Ruby and Go.
00:01:16.530 Let's start with Ruby. Ruby is, I guess, for the purposes of this talk, slow and inefficient, but it's a beautiful language. I like it a lot; it is my favorite language. However, it is definitely slower than Go. I could definitely talk more about Ruby, tell you all the awesome stuff and the bad stuff, but we're here to hopefully talk about Go, which many of you may not know much about.
00:01:42.200 So, Go is fast and simple. We know it as a simple language that's also as fast as C, but that's just the beginning. Go has many features: it has structural typing, a really fast garbage collector, interoperability with C, a good concurrency story, fast performance, and a very simple grammar. The key thing you should understand about Go, in my opinion, is its structural typing. You can think of Go as a language with two major features: the structs and data types that define data, and the functions that define behavior for that data. This makes it easier to understand how code is set up when you start reading it.
00:02:28.880 You might ask, doesn't every language separate code and data? That's true, but in Go, the separation is much more explicit. For example, in an object-oriented language like Ruby, when we define a class, we bundle methods and attributes together inside it. In Go, we define structs that represent just the data, independent from how it behaves. Functions are defined separately. Understanding these two components is key to getting acquainted with Go, as there aren't many other abstractions in the language.
00:03:12.300 Now, let's get back to the tool I mentioned earlier, called Gorb. Its purpose is to generate native Ruby extension wrappers for your Go programs easily. Gorb looks at the structure definitions we create, producing simple wrappers that we can use in our Ruby code. It leverages native extensions in Ruby, which allow us to interface with machine-level code by loading dynamic libraries into the Ruby VM.
00:04:12.300 C Ruby, which is the API for Ruby written in C, drives the Ruby implementation. Writing C code can be challenging since many programmers aren't comfortable with it. Gorb aims to simplify things by allowing Ruby developers to harness Go's performance without needing to code in C directly. Instead, you write your Go code like you normally would, and Gorb generates the necessary wrappers for you. This lets you combine the benefits of both languages with less hassle.
00:04:54.740 Now, let's jump into a demo where I can show you how this all works. I have Gorb running inside a Docker image. If you pull down the repo, you'll find a Docker file you can use to build and run everything without worrying about setting up Go or Ruby on your machine. The command I want to demonstrate is gorp gen, which generates the wrapper files for the Go packages you provide. Once generated, you can compile the code as a native extension in Ruby. Let me show you an example of a Fibonacci implementation.
00:06:44.350 Here’s the Fibonacci example that we use for benchmarking. We've created a struct that Gorb converts into a class and attached a method performing the necessary computations. In Ruby, we simply load the generated library as if it were regular Ruby code, creating a new class instance and invoking the method. This demonstrates how Gorb seamlessly integrates Go code with Ruby.
00:08:45.240 We can generate the wrapper file using gorp gen, pointing to our Fibonacci package, and it'll create and build the native extension automatically—no need for any manual make process. Once built, we require the generated file in Ruby and can use it just like any other module. This holistic view simplifies the whole process, allowing you to focus on writing your actual Go code without worrying about the boilerplate.
00:10:39.220 Moreover, Gorb automatically generates the required boilerplate code, saving time and effort. It's capable of handling various methods, even those that produce complex scenarios, seamlessly integrating them into Ruby. In this manner, you can tackle different programming tasks, whether they involve manipulating HTTP servers or arrays, applying Go functionalities directly within your Ruby code. The beauty of this tool is that it allows Ruby developers to leverage Go's extensive capabilities without venturing deeply into the language itself.
00:21:03.830 While Gorb is a powerful tool, it does have limitations. For example, it currently has limited concurrency support due to Ruby's Global Interpreter Lock (GIL). Although Go allows concurrency, Ruby's threading model restricts how it can interact with Ruby code. Additionally, Gorb does not support certain advanced features like storing and managing Ruby blocks or fully exporting complex data structures from Go without additional handling.
00:23:54.080 Despite these challenges, Gorb remains a project that encapsulates the essence of exploring Go from a Ruby perspective. It serves as a playful experiment for developers interested in combining both languages for swift implementations. I encourage you all to think of it as practice rather than a definitive solution. Engaging in this hands-on experience can provide valuable insights and reinforce your craft, whether through automation, writing libraries, or creating personal projects. It's all about exploring new territories and appreciating the joy of learning.
00:30:56.340 I hope this talk has sparked your interest in potentially using Go and Ruby together through tools like Gorb. There's a wealth of possibilities when exploring different languages and how they can interact. Thank you all for listening, and now I'd like to open the floor for any questions.
00:39:04.690 If there are any queries about the tool or its implementations, feel free to ask!