Metaprogramming in Ruby ( II )
Thinking in Lisp in Ruby in Lisp in …
http://jon.is.emotionull.com
Creating a language
Parsers from scratch Bison/yacc/lex Metaprogramming FTW!
Bow to the masters
Lisp Scheme Forth Smalltalk
Thinking again the problem
Code as data Self modifying code Dynamic adding/removing classes/functions Describe the problem, don't solve it!
Metaprogramming in the wild
Web frameworks (Ruby On Rails) A.I DSL Configuration files ...
But, hooooow???
Function pointers (C,C++) Open classes (Ruby) Metaclasses (Python) Lists (Lisp/Scheme/Clojure)
Tools of the trade for Ruby
Operators Overriding OpenClasses method_missing Blocks and eval Lambda
Speed penalty – Not always
Premature optimization Fast and smart VM (JVM) Bottleneck is somewhere else (DB) You are not smart enough. Deal with it!
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!
C in Ruby
Example
// this is a test (example.cb)
include ”stdio”
main {
printf ”Hello Mr. %s !”, ”Jon”
}
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”
}
Real usage
Prediction Modelling
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
Predictions in Ruby?
RubyVM is not very robust (think BIG > 10GB) Ruby is slow Ruby threading is difficult and bad Ruby does not SCALE!
You are right!
Thanks for watching!
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)
But ruby cannot scale!
Threading in RubyVM is bad Fibers are way better I love Jruby
Learn from the best: OpenMP
Standard for parallel processing Easy to use Very mature library
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]) ) ) ) );
....
}
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
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
Thank for watching! Questions?
Cuby available at: http://github.com/jonromero/Cuby
http://[email protected]
Top Related