Slide 1

Slide 1 text

Need for Speed Boost Ruby with FFI @johnlinvc

Slide 2

Slide 2 text

Me • John Lin ( ྛ⇉ᠳʣ • @johnlinvc • Head of Algorithm @ Spoonrocket

Slide 3

Slide 3 text

What’s SpoonRocket?

Slide 4

Slide 4 text

The Problem

Slide 5

Slide 5 text

heroku/router: at=error code=H12 
 desc="Request timeout"

Slide 6

Slide 6 text

Real World® Problem

Slide 7

Slide 7 text

We Need Speed

Slide 8

Slide 8 text

Change Language • Switch to Go / Scala / Java / C++

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Go Native • C extension • FFI

Slide 11

Slide 11 text

C Extension • Write the critical part using Ruby API in C • Need compiler on production environment • Not portable between Platforms

Slide 12

Slide 12 text

Foreign Function Interface • Mounting C function as Ruby method • Call functions using C Library libffi • Doesn’t need compiler • Portable between platforms

Slide 13

Slide 13 text

C-ext vs FFI

Slide 14

Slide 14 text

How Libffi works in C

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

C Library Structure(ELF) 9/8/2015 https://upload.wikimedia.org/wikipedia/commons/7/77/Elf-layout--en.svg ... .data .rodata .text Program header table ELF header Section header table { { https://en.wikipedia.org/wiki/Executable_and_Linkable_Format

Slide 17

Slide 17 text

Load Library void* dlopen(const char* path, int mode); • load a shared library file at path & relocate the address

Slide 18

Slide 18 text

Function Address λ ~/ nm /usr/lib/libruby.dylib 0000000000027a75 T _InitVM_Enumerator 00000000000088de T _Init_Array 000000000012a65e T _Init_BareVM 00000000000164e2 T _Init_Bignum 0000000000037829 T _Init_Binding 000000000001b08e T _Init_Comparable 000000000001be5c T _Init_Complex 000000000013c869 T _Init_Cont 0000000000020721 T _Init_Dir 00000000000032ec T _Init_Encoding 00000000000239a6 T _Init_Enumerable 000000000002906a T _Init_Enumerator 000000000002caa7 T _Init_Exception

Slide 19

Slide 19 text

Find Function Address void* dlsym(void* handle, const char* symbol); • get address of a symbol from a library

Slide 20

Slide 20 text

Function call with cdecl

Slide 21

Slide 21 text

Call Function With Address In C typedef int func(void); func * f = (func*) 0x12345566; int res = f();

Slide 22

Slide 22 text

Define Function Signature ffi_status ffi_prep_cif( ffi_cif *cif, ffi_abi abi, unsigned int nargs, ffi_type *rtype, ffi_type **atypes); Define the signature of a function. cif stands for Call InterFace

Slide 23

Slide 23 text

Invoke Function void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue); • ffi_call(): call the actual function by address, with return address and input parameters

Slide 24

Slide 24 text

dlopen() dlsym() ffi_prep_cif() ffi_call()

Slide 25

Slide 25 text

Ruby-FFI • Wrapper of C Library libffi • First appeared in Rubinius • Maintained by JRuby team • Available in MRI, JRuby & Rubinius(same interface)

Slide 26

Slide 26 text

Installing • gem install ffi

Slide 27

Slide 27 text

Hello FFI #!/usr/bin/env ruby require 'ffi' module LIBC extend FFI::Library ffi_lib FFI::Library::LIBC attach_function :printf, [:string], :int end LIBC.printf("hello FFI\n")

Slide 28

Slide 28 text

Loading Library • Loading Multiple Libs
 ffi_lib "foo", "bar" • Loading all possible names of a Lib
 ffi_lib [“foo", “foo-1.2”]

Slide 29

Slide 29 text

Attach Function • Function rename
 attach_function :c_print, :printf, [:string], :int • Function with variable length
 attach_function :printf, [:string, :varargs], :int


Slide 30

Slide 30 text

Supported C Types • Integer : singed/unsigned of various length • Floating Point: float / double • String: NULL terminated char array. • Pointer: pointer

Slide 31

Slide 31 text

Define C Structs class Timeval < FFI::Struct layout :tv_sec => :ulong, :tv_usec => :ulong end struct timeval { unsinged long tv_sec; /* seconds since 1/1/1970 */ unsinged long tv_usec;/* and microseconds */ }; C Ruby

Slide 32

Slide 32 text

Accessing C Struct module LibC extend FFI::Library ffi_lib FFI::Library::LIBC attach_function :gettimeofday, [ :pointer, :pointer ], :int end t = Timeval.new LibC.gettimeofday(t.pointer, nil) puts "t.tv_sec=#{t[:tv_sec]} t.tv_usec=#{t[:tv_usec]}" int gettimeofday(struct timeval * tp, void * tzp); C signature Ruby

Slide 33

Slide 33 text

Define Callback module LibC extend FFI::Library ffi_lib FFI::Library::LIBC callback :qsort_cmp, [ :pointer, :pointer ], :int attach_function :qsort, [ :pointer, :ulong, :ulong, :qsort_cmp ], :void end void qsort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *)); C signature Ruby

Slide 34

Slide 34 text

Using Callback pointer = FFI::MemoryPointer.new(:int, 2) ary = [ 2, 1 ] pointer.write_array_of_int32(ary) size_of_int = 4 LibC.qsort(pointer, ary.count, size_of_int) do |p1, p2| i1 = p1.read_int32 i2 = p2.read_int32 i1 < i2 ? -1 : i1 > i2 ? 1 : 0 end p pointer.get_array_of_int32(0, ary.count)

Slide 35

Slide 35 text

Features • Load C library • Invoke C function • Auto convert between Ruby types & C types • Define/Access C Struct • Using block/Proc as callback

Slide 36

Slide 36 text

Benchmark • Find Eigenvalue & Eigenvector of a matrix • Benchmark the runtime of • Ruby Matrix in Stdlib • C Extension with LAPACK • FFI with LAPACK

Slide 37

Slide 37 text

Performance N=1000 N=10000 N=100000 Ruby 0.3 2.87 30.3 FFI 0.02 0.19 2.1 C-Ext 0.01 0.1 0.99

Slide 38

Slide 38 text

Take aways • Don’t give up ruby if you need speed. • How FFI works under the hood • FFI core features

Slide 39

Slide 39 text

Q&A

Slide 40

Slide 40 text

References • Linker & Loaders • Libffi • ruby-ffi Gem • Race car Image • x86 call convention