Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Going Native With FFI - Euruko 2021

Going Native With FFI - Euruko 2021

Ruby is a flexible language that allows us to write expressive and maintainable code. However, sometimes it could be necessary to work with a low-level language, looking for better performance. In this talk, I’m going to show you how to use Ruby to create an interface for compiled languages like C.

In this talk I’m going to talk about how ruby can work jointly with languages like C, creating a simple communication interface that allows us to use all the benefits of the low-level languages in our favorite high-level language.
Furthermore, I will present the alternatives that ruby provides to communicate our application with low-level libraries, and we will explore the pros and cons of these approaches.
Finally, we will analyze a practical case about how to create a ruby extension using ruby-ffi.

This talk is aimed at people interested in performance issues as well as interested in including libraries made in another language within their project without the need to migrate outside of Ruby.

The talk is designed for an intermediate-level audience, although the fundamental concepts are explained along with it, so a beginner could find it interesting

More Decks by Juan Carlos Ruiz González

Other Decks in Programming

Transcript

  1. ❏ Sr. Full Stack developer @ EasyBroker ❏ Remote work

    advocate ❏ I ❤ Open source @JuanCrg90
  2. @JuanCrg90 mkmf.rb is used by Ruby C extensions to generate

    a Makefile which will correctly compile and link the C extension to Ruby and a third-party library.
  3. Pros: - We can use C functions in our Ruby

    Code - We take advantage of the existing ruby.h library to create the extension Cons: - We need to add extra C code to create the bindings - This solution only works in MRI @JuanCrg90
  4. A foreign function interface (FFI) is a mechanism by which

    a program written in one programming language can call routines or make use of services written in another. @JuanCrg90
  5. Pros: - We don’t need to add additional C code

    - An FFI extension is multi-platform and multi-implementation Cons: - Complex macros are difficult to maintain - Same for callback functions @JuanCrg90