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

Ruby FFI with Rust

Ruby FFI with Rust

We can use Ruby FFI to interface with other programming languages. This talk shares why we should consider Rust, and how we can use Ruby FFI to leverage Rust's high performance.

Michael Galero

November 15, 2019
Tweet

More Decks by Michael Galero

Other Decks in Programming

Transcript

  1. About me • Software engineer • Previously from Engine Yard

    (~ 5 years) • About to join the team at TradeGecko • A non-prolific open source contributor for Ruby and Rust projects • Co-created Mina deployment tool
  2. FFI Foreign Function Interface A Foreign Function Interface (FFI) is

    a means for a programming language to interface with libraries written in other programming languages. Source: From Chapter 3 of The ECL Manual by Garcia-Ripoll, Kochmański
  3. Performance Rust is blazingly fast and memory-efficient: with no runtime

    or garbage collector, it can power performance-critical services, run on embedded devices, and easily integrate with other languages. Source: www.rust-lang.org
  4. An old performance comparison for a “hello world” HTTP server

    Source: https://aturon.github.io/blog/2016/08/11/futures/
  5. Iterating a HashMap in Rust use std::collections::HashMap; let mut scores

    = HashMap::new(); scores.insert("Blue".to_string(), 10); scores.insert("Yellow".to_string(), 50); for (key, value) in &scores { println!("{}: {}", key, value); }
  6. When to use Rust? • CLI tools • Network services

    • WASM • Embedded systems • Asynchronous applications • Game development
  7. When to use Rust? For Ruby web developers • CLI

    tools • Network services • Server agents • Background jobs • Speeding up slow parts of our application using FFI
  8. Using FFI in Ruby require 'ffi' module Main extend FFI::Library

    ffi_lib 'target/debug/librubyffi.' + FFI::Platform::LIBSUFFIX attach_function :add, [:int, :int], :int end
  9. Using FFI in Ruby LIBSUFFIX = case OS when /darwin/

    'dylib' when /linux|bsd|solaris/ 'so' when /windows|cygwin|msys/ 'dll' else # Punt and just assume a sane unix (i.e. anything but AIX) 'so' end
  10. Using FFI in Ruby require 'ffi' module Main extend FFI::Library

    ffi_lib 'target/debug/librubyffi.' + FFI::Platform::LIBSUFFIX attach_function :add, [:int, :int], :int end sum = Main.add(2, 2)