Rust FFI

2020-02-06

Roland Brand

About Rust Basel

  • Meetup group since April 2019
  • This is the 4th Meetup
  • Talks from Stefan Braun, Reto Kaiser, Roland Brand
  • Thanks to my employer greenmatch for hosting
    • We are moving soon

next meetup will be

here

The next talk

  • about your first steps with Rust?
  • About similar languages?
    • C/C++
    • Go
    • Crystal
    • Nim
    • ...
  • About things important to the Rust ecosystem
    • Command Line
    • Web assembly
    • Networking
    • Embedded

TOC

  • Why FFI?
  • FFI in general
  • Rust C FFI
  • Other Rust FFI
  • PHP Example: expression-language

Why

FFI

use shared libs

  • use (or provide) system libraries such as databases, git, directly
    • efficient, low waste
  • long term backwards compatibility (C ABI)

interface to other high level langs

C FFI as common denominator allows for almost any combination without direct support

But really, why?

  • You (usually) shouldn't build complex programs with systems languages such as C.
    • Faster and safer development with expressive high-level OOP or FP languages
  • But performance and memory efficiency are a trade off
  • C/C++/Rust implementation of critical spots

FFI

in general

Typical

  • heavily algorithmic libraries
  • "workhorses" for platforms or libraries
    • Numpy
  • bottlenecks
    • built-in XML or JSON parsers in almost any language

API vs ABI

Direct Interface vs. FFI

  • FFI ist typically interfacing to C and relying on the stable C ABI
  • Direct interfaces exist as well (e.g Haskell and the JVM) without FFI

Examples

See Wikipedia "FFI" article

Bindgen

Rust

C FFI

Types

  • common data representation
  • std::os::raw module types are guaranteed to have the same representation as certain C types
C type std::os::raw
short c_short
int c_int
long c_long
long long c_longlong
unsigned short c_ushort
unsigned int c_uint
unsigned long c_ulong
unsigned long long c_ulonglong
char c_char
signed char c_schar
unsigned char c_uchar
float c_float
double c_double
void * *mut c_void

#[no_mangle]

  • name mangling not compatible with C
    • -> anyone jump in and explain name mangling?
  • necessary if you want to build a "C" library with rust to be called from outside, e.g. a PostgreSQL extension

pub extern "C"

cdylib

  • Dynamic Library
    • DLL, .so

Other

Rust
bindings

neon (node.js)

  • nodejs to rust interface
  • easy to use

Python

Rust bindings for the python interpreter

Classical FFI

  • CFFI
  • ctypes

PHP Example

expression-language

general idea

  • symfony expression language
  • widely used to express business rules and other customization definitions.
  • provides parser, but PHP is not the best option for performance

 

PHP FFI

  • experimental feature since PHP 7.4
  • before, you needed to install extensions (e.g php-mbstring)

Speed?

 

  • We'll see....

Pest (PEG Parser)

  • Pest (expressive) vs nom (faster)
  • comparable to Regex, Pattern matching

State

In summary,

  • Rust beats PHP's ass
    • even with an experimental, slow FFI
    • even with the slower parser option
    • even on short inputs
  • ...let's get on with it!
Made with Slides.com