Metaprogramming in Ruby

22
 Metaprogramming in Ruby ( II ) Thinking in Lisp in Ruby in Lisp in … http://jon.is.emotionull.com

Transcript of Metaprogramming in Ruby

Page 1: Metaprogramming in Ruby

   

Metaprogramming in Ruby ( II )

Thinking in Lisp in Ruby in Lisp in …

http://jon.is.emotionull.com

Page 2: Metaprogramming in Ruby

   

Creating a language

Parsers from scratch Bison/yacc/lex  Metaprogramming FTW!

Page 3: Metaprogramming in Ruby

   

Bow to the masters

Lisp Scheme Forth Smalltalk 

Page 4: Metaprogramming in Ruby

   

Thinking again the problem

Code as data Self modifying code Dynamic adding/removing classes/functions Describe the problem, don't solve it!

Page 5: Metaprogramming in Ruby

   

Metaprogramming in the wild

Web frameworks (Ruby On Rails) A.I DSL Configuration files ...

Page 6: Metaprogramming in Ruby

   

But, hooooow???

Function pointers (C,C++) Open classes (Ruby) Metaclasses (Python) Lists (Lisp/Scheme/Clojure)

Page 7: Metaprogramming in Ruby

   

Tools of the trade for Ruby

Operators Overriding OpenClasses method_missing Blocks and eval Lambda

Page 8: Metaprogramming in Ruby

   

Speed penalty – Not always

Premature optimization Fast and smart VM (JVM) Bottleneck is somewhere else (DB) You are not smart enough. Deal with it!

Page 9: Metaprogramming in Ruby

   

Cuby

Implemented in ~ 10 minutes Looks like C Feels like C FULLY extensible Trivial to add new libraries You can use available Ruby libs VM ~ 25 lines!

Page 10: Metaprogramming in Ruby

   

C in Ruby

Example

// this is a test (example.cb)

include ”stdio”

main {

    printf ”Hello Mr. %s !”, ”Jon”

}     

Page 11: Metaprogramming in Ruby

   

C in Ruby : Functions

Example

include ”stdio”

plea_for_merci_to name {

  shout ”Please don't kill me” + name + ”!”

}

main {

    plea_for_merci_to ”Jon”

}     

Page 12: Metaprogramming in Ruby

   

Real usage

Prediction Modelling

Page 13: Metaprogramming in Ruby

   

Prediction Modelling

Lots of data (sometimes more than 60GB) Long live C, Fortran, Lisp Do you know OpenMP? If not, find another job Speed, speed, speed

Page 14: Metaprogramming in Ruby

   

Predictions in Ruby?

RubyVM is not very robust (think BIG > 10GB) Ruby is slow Ruby threading is difficult and bad Ruby does not SCALE!

Page 15: Metaprogramming in Ruby

   

You are right!

Thanks for watching!

Page 16: Metaprogramming in Ruby

   

But ruby is slooooow

1.9 is waaay better than 1.8 Jruby is the King of the Hill  The bottleneck is not really here (hint: DB)

Page 17: Metaprogramming in Ruby

   

But ruby cannot scale!

Threading in RubyVM is bad Fibers are way better  I love Jruby

 

Page 18: Metaprogramming in Ruby

   

Learn from the best: OpenMP

Standard for parallel processing Easy to use Very mature library

Page 19: Metaprogramming in Ruby

   

OpenMP example

Example

#pragma omp parallel for#pragma omp parallel for

 for (i = 0; i < arraySize; i++){  

      ....

      y[i] = sin( exp( cos( ­ exp( sin(x[i]) ) ) ) );

     ....

   }

Page 20: Metaprogramming in Ruby

   

Can we do this in Ruby?

Example

for j in 1..30

    puts "Hello from iteration " + j.to_s

      for i in 0..700000

        c += a[i] * b[­i]

      end

      puts "I have just finished!"

 end 

Page 21: Metaprogramming in Ruby

   

In Cuby we trust

Example

for j in 1..30

    puts "Hello from iteration " + j.to_s

      parallel {

      for i in 0..700000

        c += a[i] * b[­i]

      end

      puts "I have just finished!"

      }

 end 

Page 22: Metaprogramming in Ruby

   

 

Thank for watching! Questions?

Cuby available at: http://github.com/jonromero/Cuby

http://[email protected]