Slide 1

Slide 1 text

namaskaaram!

Slide 2

Slide 2 text

Sameer Deshmukh github.com/v0dro @v0dro

Slide 3

Slide 3 text

City of Pune. Population: 6 million. Oxford of the East.

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Dr. Gopal Deshmukh Sameer Desmukh (not a doctor) Dr. Hemchandra Deshmukh Dr. Satish Deshmukh

Slide 6

Slide 6 text

catkamikazee.bandcamp.com Sameer

Slide 7

Slide 7 text

Pune Ruby Users Group www.punerb.org @punerb @deccanrubyconf www.deccanrubyconf.org

Slide 8

Slide 8 text

Ruby Science Foundation www.sciruby.com @sciruby @sciruby

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Daru ­ Data Analysis in RUby A Ruby gem for analysis, plotting and cleaning of data.

Slide 12

Slide 12 text

https://github.com/SciRuby/daru

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Rubex: A better way to write Ruby C extensions.

Slide 15

Slide 15 text

What is a C extension?

Slide 16

Slide 16 text

Ruby speed reliability C

Slide 17

Slide 17 text

Ruby speed reliability C Nokogiri Nokogiri::XML() fast_blank String#blank? libxml Handwritten C

Slide 18

Slide 18 text

# In test.rb require ’fast_blank’ a = ”hello” a.blank? /* In fast_blank.c: */ VALUE rb_str_blank(VALUE str) CRuby C API Interfaces C code with the CRuby runtime

Slide 19

Slide 19 text

BIG Problems ● Difficult and irritating to write. ● Debugging is time consuming. ● Tough to trace memory leaks. ● Change mindset from high level to low level language. ● Remember the CRuby C API. ● Need to care about small things.™* *Matz.

Slide 20

Slide 20 text

def addition a,b return a + b end

Slide 21

Slide 21 text

int calc_addition(int a, int b) { return (a + b); } static VALUE caddition(VALUE self, VALUE a, VALUE b) { int i = FIX2INT(a); int j = FIX2INT(b); return INT2FIX(calc_addition(i, j)); }

Slide 22

Slide 22 text

void Init_addition() { VALUE cAdd = rb_define_class("Add", rb_cObject); rb_define_method(cAdd, "addition", caddition, 2); }

Slide 23

Slide 23 text

require ‘addition.so’ a = Add.new a.addition(5, 4)

Slide 24

Slide 24 text

WTF?!

Slide 25

Slide 25 text

Hmmm should try Rubex!

Slide 26

Slide 26 text

Rubex code Rubex compiler C code CRuby runtime Knows how to interface with the CRuby interpreter. Language which looks like Ruby. Code ready to interface with Ruby. Code actually runs here.

Slide 27

Slide 27 text

# In file addition.rubex: def addition(int a, int b) return a + b end

Slide 28

Slide 28 text

# In file test.rb: require ‘addition.so’ print addition(4,5)

Slide 29

Slide 29 text

static VALUE rb_str_blank(VALUE str) { // lots of unicode handling code omitted s = RSTRING_PTR(str); e = RSTRING_END(str); while (s < e) { // cc = current character if (!rb_isspace(cc) && cc != 0) return Qfalse; } return Qtrue; }

Slide 30

Slide 30 text

def blank?(string) i32 i = 0 char *s = string i32 length = string.size while i < length do return false if s[i] != ' ' i += 1 end return true end

Slide 31

Slide 31 text

def blank?(string) i32 i = 0 char *s = string i32 length = string.size while i < length do return false if s[i] != ' ' i += 1 end return true end

Slide 32

Slide 32 text

def blank?(string) i32 i = 0 char *s = string i32 length = string.size while i < length do return false if s[i] != ' ' i += 1 end return true end

Slide 33

Slide 33 text

def blank?(string) i32 i = 0 char *s = string i32 length = string.size while i < length do return false if s[i] != ' ' i += 1 end return true end

Slide 34

Slide 34 text

Benchmarks ● Comparison : fast_blank’s String#blank? vs. blank? implemented in Rubex. ● Data : A Ruby String with 2500 spaces in the beginning and three ASCII letters at the end. Data taken so that non­trivial time will be spent on iterations to search for a white space. str = " "*2500 + "dff" ● Result: This is new stuff is good.

Slide 35

Slide 35 text

Benchmark-ips results Warming up -------------------------------------- fast_blank 3.401k i/100ms blank? 57.041k i/100ms Calculating ------------------------------------- fast_blank 35.068k (± 0.4%) i/s - 176.852k in 5.043263s blank? 671.289k (± 1.1%) i/s - 3.365M in 5.014016s Comparison: blank?: 671289.0 i/s fast_blank: 35067.6 i/s - 19.14x slower

Slide 36

Slide 36 text

Conclusion of benchmarks: ● fast_blank is not that fast for ASCII strings. ● Now anybody can write C extensions with Rubex.

Slide 37

Slide 37 text

Most important use case of Rubex ● Not simply for abstracting away C code. ● SciRuby works with many highly optimized C libraries like ATLAS, BLAS, FFTW & GSL. ● These C libraries use complex API calls that need to be interfaced with Ruby with a lot of ‘glue’ code. ● Glue code is a pain to write/debug.

Slide 38

Slide 38 text

Interfacing external C libraries ● Example : BLAS::gemm() method for multiplying two square matrices. gemm( const enum CBLAS_ORDER, const enum CBLAS_TRANSPOSE, const enum CBLAS_TRANSPOSE, const int, const int, const int, const double*, const double*, const int, const double*, const int, const double*, double*, const int )

Slide 39

Slide 39 text

Interface math.h header file with Ruby using Rubex

Slide 40

Slide 40 text

# In file maths.rubex lib math do double pow (double, double) double cos (double) end def maths(double power) double p = cos(0.5) return pow(p, power) end

Slide 41

Slide 41 text

# In file maths.rubex lib math do double pow (double, double) double cos (double) end def maths(double power) double p = cos(0.5) return pow(p, power) end

Slide 42

Slide 42 text

# In file maths.rubex lib math do double pow (double, double) double cos (double) end def maths(double power) double p = cos(0.5) return pow(p, power) end

Slide 43

Slide 43 text

# In file maths.rubex lib math do double pow (double, double) double cos (double) end def maths(double power) double p = cos(0.5) return pow(p, power) end

Slide 44

Slide 44 text

# In file maths.rubex lib math do double pow (double, double) double cos (double) end def maths(double power) double p = cos(0.5) return pow(p, power) end

Slide 45

Slide 45 text

# In file test.rb require ‘maths.so’ print maths(5.6)

Slide 46

Slide 46 text

Salient Features

Slide 47

Slide 47 text

Rubex is meant to be a super set of Ruby and is as a companion of Ruby. It does not replace Ruby.

Slide 48

Slide 48 text

Everything in Rubex is NOT an object. There can be both primitive C data types and Ruby objects co­existing in a single Rubex program.

Slide 49

Slide 49 text

You can declare Abstract C Data Types like Structs, Unions and Enums using Rubex and pass them to arbitrary C functions.

Slide 50

Slide 50 text

Future Roadmap ● Support both native C and Ruby functions. ● Ability to encapsulate methods in classes. ● Introduce advanced heuristics to convert between C and Ruby data types. ● Ability to release the Global Interpreter Lock and perform operations on native threads.

Slide 51

Slide 51 text

https://github.com/v0dro/rubex

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

I haz stickers! ^_^

Slide 54

Slide 54 text

Thank you Kochi! Thank you Ruby Conf India!