SlideShare ist ein Scribd-Unternehmen logo
1 von 65
Downloaden Sie, um offline zu lesen
Вівторок, 19 березня 13 р.
Inside-Out Ruby: Using
MRI as a C library
Вівторок, 19 березня 13 р.
About me
Arthur Pirogovski
arthur@flyintealeaf.com
Вівторок, 19 березня 13 р.
About me
Coding for , mostly Ruby.
Вівторок, 19 березня 13 р.
About me
And C.
segfault(87698) malloc: *** error for object
0x7fff5dbad6c0:
pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
Вівторок, 19 березня 13 р.
About Ruby
Вівторок, 19 березня 13 р.
About Ruby
very readable
Вівторок, 19 березня 13 р.
About Ruby
“#{@str[:mostly]}”
very readable
Вівторок, 19 березня 13 р.
About Ruby
very bad concurrency.
Вівторок, 19 березня 13 р.
About Ruby
Mediocre speed
Вівторок, 19 березня 13 р.
About C
Вівторок, 19 березня 13 р.
About C
very unreadable
void (*signal(int, void (*fp)(int)))(int);
Вівторок, 19 березня 13 р.
About C
concurrency is as good
as your programming skills are
Вівторок, 19 березня 13 р.
About C
conc is urrencyaods go
amings your program ls askilre
Вівторок, 19 березня 13 р.
About C
only FORTRAN is faster
Вівторок, 19 березня 13 р.
About C
Actually, not
Вівторок, 19 березня 13 р.
Native Extensions
Вівторок, 19 березня 13 р.
Native Extensions
• great speedup
•
•
Вівторок, 19 березня 13 р.
Native Extensions
• great speedup
• portability problems #ifdef
•
Вівторок, 19 березня 13 р.
Native Extensions
• great speedup
• portability problems #ifdef
• secret concurrency (don’t tell Ruby)
Вівторок, 19 березня 13 р.
What if we turn it
inside out?
Вівторок, 19 березня 13 р.
Strings
“much”*10 easier in Ruby
than in char ** C
Вівторок, 19 березня 13 р.
DSL
Turing-complete out of the box
Вівторок, 19 березня 13 р.
DSL
Turing-complete out of the box
Pretty like drugs
Вівторок, 19 березня 13 р.
Calling Ruby code
- Hey, I’m C!
(awkward silence)
- Hey, I’m Ruby!
Вівторок, 19 березня 13 р.
Garbage Collector
Very bad one, but still better than none
Вівторок, 19 березня 13 р.
Well-known APIs
VALUE hash = rb_hash_new();
VALUE key = rb_string_new2(“key”);
VALUE value = INT2FIX(42);
rb_hash_aset(hash, key, value);
rb_p(hash);
Вівторок, 19 березня 13 р.
MVP
#ifdef __APPLE__
#define HAVE_STRUCT_TIMESPEC 1
#endif
Вівторок, 19 березня 13 р.
MVP
#ifdef __APPLE__
#define HAVE_STRUCT_TIMESPEC 1
#endif
#include <ruby.h>
Вівторок, 19 березня 13 р.
MVP
#ifdef __APPLE__
#define HAVE_STRUCT_TIMESPEC 1
#endif
#include <ruby.h>
int main() {
ruby_init();
return 0;
}
Вівторок, 19 березня 13 р.
MVP
#ifdef __APPLE__
#define HAVE_STRUCT_TIMESPEC 1
#endif
#include <ruby.h>
int main() {
ruby_init();
ruby_init_loadpath();
return 0;
}
Вівторок, 19 березня 13 р.
MVP
#ifdef __APPLE__
#define HAVE_STRUCT_TIMESPEC 1
#endif
#include <ruby.h>
int main() {
ruby_init();
ruby_init_loadpath();
ruby_script("c.rb");
return 0;
}
Вівторок, 19 березня 13 р.
MVP
#ifdef __APPLE__
#define HAVE_STRUCT_TIMESPEC 1
#endif
#include <ruby.h>
int main() {
ruby_init();
ruby_init_loadpath();
ruby_script("c.rb");
ruby_finalize();
return 0;
}
Вівторок, 19 березня 13 р.
MVP
#ifdef __APPLE__
#define HAVE_STRUCT_TIMESPEC 1
#endif
#include <ruby.h>
int main() {
ruby_init();
ruby_init_loadpath();
ruby_script("c.rb");
VALUE ruby_string = rb_str_new2("Hi,I’m Ruby!");
rb_p(ruby_string);
ruby_finalize();
return 0;
}
Вівторок, 19 березня 13 р.
MVP
$ gcc minimal_embedded_ruby.c 
-o meruby -lruby 
-I ${MY_RUBY_HOME}/include/ruby-1.9.1
-I ${MY_RUBY_HOME}/include/
ruby-1.9.1/x86_64-linux 
-L $MY_RUBY_HOME/lib
$ ./meruby
"Hi from Ruby!"
Note: $MY_RUBY_HOME is RVM-specific
Вівторок, 19 березня 13 р.
Warning #1
Ruby can only be initialized
from the main thread
Вівторок, 19 березня 13 р.
Warning #2
DO NOT call ruby_run()
It intercepts control flow and never returns.
Вівторок, 19 березня 13 р.
Memory
management
Вівторок, 19 березня 13 р.
Memory
management
not fun.
Вівторок, 19 березня 13 р.
This will SEGFAULT
int main() {
ruby_init();
ruby_init_loadpath();
ruby_script("c.rb");
VALUE ruby_string = rb_str_new2("Hi,I’m
Ruby!");
rb_p(ruby_string);
rb_gc();
rb_p(ruby_string); /* Right here */
ruby_finalize();
return 0;
}
Вівторок, 19 березня 13 р.
Registering vars
#include <ruby.h>
static VALUE ruby_objects;
void lo(VALUE ruby_object) {
rb_hash_aset(ruby_objects,
rb_obj_id(ruby_object),
ruby_object);
}
void uo(VALUE ruby_object) {
rb_hash_delete(ruby_objects,
rb_obj_id(ruby_object));
}
Вівторок, 19 березня 13 р.
Registering vars
int main() {
ruby_init();
...
ruby_objects = rb_hash_new();
rb_global_variable(&ruby_objects);
}
Вівторок, 19 березня 13 р.
Registering vars
int main() {
ruby_init();
...
ruby_objects = rb_hash_new();
rb_global_variable(&ruby_objects);
VALUE ruby_string = rb_str_new2("Hi
from Ruby!");
}
Вівторок, 19 березня 13 р.
Registering vars
int main() {
ruby_init();
...
ruby_objects = rb_hash_new();
rb_global_variable(&ruby_objects);
VALUE ruby_string = rb_str_new2("Hi from
Ruby!");
lo(ruby_string);
rb_p(ruby_string);
rb_gc();
}
Вівторок, 19 березня 13 р.
Registering vars
int main() {
ruby_init();
...
ruby_objects = rb_hash_new();
rb_global_variable(&ruby_objects);
VALUE ruby_string = rb_str_new2("Hi from Ruby!");
lo(ruby_string);
rb_p(ruby_string);
rb_gc();
rb_p(ruby_string); /* No SEGFAULT this time */
uo(ruby_string);
...
}
Вівторок, 19 березня 13 р.
GC tips
call rb_gc() to collect
can also be invoked by Ruby itself
Вівторок, 19 березня 13 р.
GC tips
always register variables
rb_gc_register_address() is slow,
so use some data structure
Вівторок, 19 березня 13 р.
GC tips
run rb_gc() often to detect problems early
automatic GC can be disabled (dangerous!)
Вівторок, 19 березня 13 р.
Exceptions
Вівторок, 19 березня 13 р.
Exceptions
are evil.
Вівторок, 19 березня 13 р.
Exceptions
rb_raise()
rb_rescue()
rb_ensure()
Вівторок, 19 березня 13 р.
Exceptions
rb_raise() is a longjmp()
longjmp() skips your free()’s
Вівторок, 19 березня 13 р.
Exceptions
rb_protect() is a good catch-all wrapper
Вівторок, 19 березня 13 р.
Exceptions
int state = 0;
rb_protect(
RUBY_METHOD_FUNC(rb_require),
(VALUE) “./code.rb”,
&state);
/* We couldn't execute rb_require */
if (state) {
rb_p(rb_gv_get("$!"));
exit(1);
}
Вівторок, 19 березня 13 р.
Functions:
calling from C
VALUE new_object =
rb_funcall(some_class,
rb_intern("new"), 1, arg1);
Вівторок, 19 березня 13 р.
Functions:
exporting to Ruby
rb_define_singleton_method(
some_class, “method_name”,
method_name_in_c, -1)
Вівторок, 19 березня 13 р.
Functions:
exporting to Ruby
static VALUE
method_name_in_c(int argc,
VALUE * argv, VALUE self)
Вівторок, 19 березня 13 р.
Functions:
exporting to Ruby
rb_scan_args(argc, argv,
“11”, &arg1, &arg2);
Вівторок, 19 березня 13 р.
Speed
For simple tasks,
C-based loader is up to 4x faster
Вівторок, 19 березня 13 р.
Speed
But for complex tasks,
speed gain is only about 15%
Вівторок, 19 березня 13 р.
Speed
Pure C code is up to 15x times faster
Вівторок, 19 березня 13 р.
Docs
README.EXT
Vim’s if_ruby.c
Ruby source code
Вівторок, 19 березня 13 р.
Examples
https://github.com/arp/ruby_from_c
Вівторок, 19 березня 13 р.
Thanks
Questions?
Вівторок, 19 березня 13 р.
Bonus
FastCGI + Ruby + C
Вівторок, 19 березня 13 р.

Weitere ähnliche Inhalte

Mehr von Ruby Meditation

Use cases for Serverless Technologies - Ruslan Tolstov (RUS) | Ruby Meditatio...
Use cases for Serverless Technologies - Ruslan Tolstov (RUS) | Ruby Meditatio...Use cases for Serverless Technologies - Ruslan Tolstov (RUS) | Ruby Meditatio...
Use cases for Serverless Technologies - Ruslan Tolstov (RUS) | Ruby Meditatio...Ruby Meditation
 
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...Ruby Meditation
 
What/How to do with GraphQL? - Valentyn Ostakh (ENG) | Ruby Meditation 27
What/How to do with GraphQL? - Valentyn Ostakh (ENG) | Ruby Meditation 27What/How to do with GraphQL? - Valentyn Ostakh (ENG) | Ruby Meditation 27
What/How to do with GraphQL? - Valentyn Ostakh (ENG) | Ruby Meditation 27Ruby Meditation
 
New features in Rails 6 - Nihad Abbasov (RUS) | Ruby Meditation 26
New features in Rails 6 -  Nihad Abbasov (RUS) | Ruby Meditation 26New features in Rails 6 -  Nihad Abbasov (RUS) | Ruby Meditation 26
New features in Rails 6 - Nihad Abbasov (RUS) | Ruby Meditation 26Ruby Meditation
 
Security Scanning Overview - Tetiana Chupryna (RUS) | Ruby Meditation 26
Security Scanning Overview - Tetiana Chupryna (RUS) | Ruby Meditation 26Security Scanning Overview - Tetiana Chupryna (RUS) | Ruby Meditation 26
Security Scanning Overview - Tetiana Chupryna (RUS) | Ruby Meditation 26Ruby Meditation
 
Teach your application eloquence. Logs, metrics, traces - Dmytro Shapovalov (...
Teach your application eloquence. Logs, metrics, traces - Dmytro Shapovalov (...Teach your application eloquence. Logs, metrics, traces - Dmytro Shapovalov (...
Teach your application eloquence. Logs, metrics, traces - Dmytro Shapovalov (...Ruby Meditation
 
Best practices. Exploring - Ike Kurghinyan (RUS) | Ruby Meditation 26
Best practices. Exploring - Ike Kurghinyan (RUS) | Ruby Meditation 26Best practices. Exploring - Ike Kurghinyan (RUS) | Ruby Meditation 26
Best practices. Exploring - Ike Kurghinyan (RUS) | Ruby Meditation 26Ruby Meditation
 
Road to A/B testing - Alexey Vasiliev (ENG) | Ruby Meditation 25
Road to A/B testing - Alexey Vasiliev (ENG) | Ruby Meditation 25Road to A/B testing - Alexey Vasiliev (ENG) | Ruby Meditation 25
Road to A/B testing - Alexey Vasiliev (ENG) | Ruby Meditation 25Ruby Meditation
 
Concurrency in production. Real life example - Dmytro Herasymuk | Ruby Medita...
Concurrency in production. Real life example - Dmytro Herasymuk | Ruby Medita...Concurrency in production. Real life example - Dmytro Herasymuk | Ruby Medita...
Concurrency in production. Real life example - Dmytro Herasymuk | Ruby Medita...Ruby Meditation
 
Data encryption for Ruby web applications - Dmytro Shapovalov (RUS) | Ruby Me...
Data encryption for Ruby web applications - Dmytro Shapovalov (RUS) | Ruby Me...Data encryption for Ruby web applications - Dmytro Shapovalov (RUS) | Ruby Me...
Data encryption for Ruby web applications - Dmytro Shapovalov (RUS) | Ruby Me...Ruby Meditation
 
Rails App performance at the limit - Bogdan Gusiev
Rails App performance at the limit - Bogdan GusievRails App performance at the limit - Bogdan Gusiev
Rails App performance at the limit - Bogdan GusievRuby Meditation
 
GDPR. Next Y2K in 2018? - Anton Tkachov | Ruby Meditation #23
GDPR. Next Y2K in 2018? - Anton Tkachov | Ruby Meditation #23GDPR. Next Y2K in 2018? - Anton Tkachov | Ruby Meditation #23
GDPR. Next Y2K in 2018? - Anton Tkachov | Ruby Meditation #23Ruby Meditation
 
Postgres vs Elasticsearch while enriching data - Vlad Somov | Ruby Meditaiton...
Postgres vs Elasticsearch while enriching data - Vlad Somov | Ruby Meditaiton...Postgres vs Elasticsearch while enriching data - Vlad Somov | Ruby Meditaiton...
Postgres vs Elasticsearch while enriching data - Vlad Somov | Ruby Meditaiton...Ruby Meditation
 
Database Sharding in Rails Applications – Vitalik Danchenko | Ruby Meditatio...
 Database Sharding in Rails Applications – Vitalik Danchenko | Ruby Meditatio... Database Sharding in Rails Applications – Vitalik Danchenko | Ruby Meditatio...
Database Sharding in Rails Applications – Vitalik Danchenko | Ruby Meditatio...Ruby Meditation
 
Building MVP from business owner’s perspective – Piotr Latoszek | Ruby Medita...
Building MVP from business owner’s perspective – Piotr Latoszek | Ruby Medita...Building MVP from business owner’s perspective – Piotr Latoszek | Ruby Medita...
Building MVP from business owner’s perspective – Piotr Latoszek | Ruby Medita...Ruby Meditation
 
Growing Rails Apps - Dmitry Zhlobo | Ruby Meditation #23
Growing Rails Apps - Dmitry Zhlobo | Ruby Meditation #23Growing Rails Apps - Dmitry Zhlobo | Ruby Meditation #23
Growing Rails Apps - Dmitry Zhlobo | Ruby Meditation #23Ruby Meditation
 
Residence permit EU - Gennadii Miroshnychenko
Residence permit EU - Gennadii MiroshnychenkoResidence permit EU - Gennadii Miroshnychenko
Residence permit EU - Gennadii MiroshnychenkoRuby Meditation
 
Practical SOLID with Rails - Andrii Savchenko
Practical SOLID with Rails - Andrii SavchenkoPractical SOLID with Rails - Andrii Savchenko
Practical SOLID with Rails - Andrii SavchenkoRuby Meditation
 
Open education - Artem Suchov
Open education - Artem SuchovOpen education - Artem Suchov
Open education - Artem SuchovRuby Meditation
 
Getting new devs up to speed - Ivan Zarea
Getting new devs up to speed - Ivan ZareaGetting new devs up to speed - Ivan Zarea
Getting new devs up to speed - Ivan ZareaRuby Meditation
 

Mehr von Ruby Meditation (20)

Use cases for Serverless Technologies - Ruslan Tolstov (RUS) | Ruby Meditatio...
Use cases for Serverless Technologies - Ruslan Tolstov (RUS) | Ruby Meditatio...Use cases for Serverless Technologies - Ruslan Tolstov (RUS) | Ruby Meditatio...
Use cases for Serverless Technologies - Ruslan Tolstov (RUS) | Ruby Meditatio...
 
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...
 
What/How to do with GraphQL? - Valentyn Ostakh (ENG) | Ruby Meditation 27
What/How to do with GraphQL? - Valentyn Ostakh (ENG) | Ruby Meditation 27What/How to do with GraphQL? - Valentyn Ostakh (ENG) | Ruby Meditation 27
What/How to do with GraphQL? - Valentyn Ostakh (ENG) | Ruby Meditation 27
 
New features in Rails 6 - Nihad Abbasov (RUS) | Ruby Meditation 26
New features in Rails 6 -  Nihad Abbasov (RUS) | Ruby Meditation 26New features in Rails 6 -  Nihad Abbasov (RUS) | Ruby Meditation 26
New features in Rails 6 - Nihad Abbasov (RUS) | Ruby Meditation 26
 
Security Scanning Overview - Tetiana Chupryna (RUS) | Ruby Meditation 26
Security Scanning Overview - Tetiana Chupryna (RUS) | Ruby Meditation 26Security Scanning Overview - Tetiana Chupryna (RUS) | Ruby Meditation 26
Security Scanning Overview - Tetiana Chupryna (RUS) | Ruby Meditation 26
 
Teach your application eloquence. Logs, metrics, traces - Dmytro Shapovalov (...
Teach your application eloquence. Logs, metrics, traces - Dmytro Shapovalov (...Teach your application eloquence. Logs, metrics, traces - Dmytro Shapovalov (...
Teach your application eloquence. Logs, metrics, traces - Dmytro Shapovalov (...
 
Best practices. Exploring - Ike Kurghinyan (RUS) | Ruby Meditation 26
Best practices. Exploring - Ike Kurghinyan (RUS) | Ruby Meditation 26Best practices. Exploring - Ike Kurghinyan (RUS) | Ruby Meditation 26
Best practices. Exploring - Ike Kurghinyan (RUS) | Ruby Meditation 26
 
Road to A/B testing - Alexey Vasiliev (ENG) | Ruby Meditation 25
Road to A/B testing - Alexey Vasiliev (ENG) | Ruby Meditation 25Road to A/B testing - Alexey Vasiliev (ENG) | Ruby Meditation 25
Road to A/B testing - Alexey Vasiliev (ENG) | Ruby Meditation 25
 
Concurrency in production. Real life example - Dmytro Herasymuk | Ruby Medita...
Concurrency in production. Real life example - Dmytro Herasymuk | Ruby Medita...Concurrency in production. Real life example - Dmytro Herasymuk | Ruby Medita...
Concurrency in production. Real life example - Dmytro Herasymuk | Ruby Medita...
 
Data encryption for Ruby web applications - Dmytro Shapovalov (RUS) | Ruby Me...
Data encryption for Ruby web applications - Dmytro Shapovalov (RUS) | Ruby Me...Data encryption for Ruby web applications - Dmytro Shapovalov (RUS) | Ruby Me...
Data encryption for Ruby web applications - Dmytro Shapovalov (RUS) | Ruby Me...
 
Rails App performance at the limit - Bogdan Gusiev
Rails App performance at the limit - Bogdan GusievRails App performance at the limit - Bogdan Gusiev
Rails App performance at the limit - Bogdan Gusiev
 
GDPR. Next Y2K in 2018? - Anton Tkachov | Ruby Meditation #23
GDPR. Next Y2K in 2018? - Anton Tkachov | Ruby Meditation #23GDPR. Next Y2K in 2018? - Anton Tkachov | Ruby Meditation #23
GDPR. Next Y2K in 2018? - Anton Tkachov | Ruby Meditation #23
 
Postgres vs Elasticsearch while enriching data - Vlad Somov | Ruby Meditaiton...
Postgres vs Elasticsearch while enriching data - Vlad Somov | Ruby Meditaiton...Postgres vs Elasticsearch while enriching data - Vlad Somov | Ruby Meditaiton...
Postgres vs Elasticsearch while enriching data - Vlad Somov | Ruby Meditaiton...
 
Database Sharding in Rails Applications – Vitalik Danchenko | Ruby Meditatio...
 Database Sharding in Rails Applications – Vitalik Danchenko | Ruby Meditatio... Database Sharding in Rails Applications – Vitalik Danchenko | Ruby Meditatio...
Database Sharding in Rails Applications – Vitalik Danchenko | Ruby Meditatio...
 
Building MVP from business owner’s perspective – Piotr Latoszek | Ruby Medita...
Building MVP from business owner’s perspective – Piotr Latoszek | Ruby Medita...Building MVP from business owner’s perspective – Piotr Latoszek | Ruby Medita...
Building MVP from business owner’s perspective – Piotr Latoszek | Ruby Medita...
 
Growing Rails Apps - Dmitry Zhlobo | Ruby Meditation #23
Growing Rails Apps - Dmitry Zhlobo | Ruby Meditation #23Growing Rails Apps - Dmitry Zhlobo | Ruby Meditation #23
Growing Rails Apps - Dmitry Zhlobo | Ruby Meditation #23
 
Residence permit EU - Gennadii Miroshnychenko
Residence permit EU - Gennadii MiroshnychenkoResidence permit EU - Gennadii Miroshnychenko
Residence permit EU - Gennadii Miroshnychenko
 
Practical SOLID with Rails - Andrii Savchenko
Practical SOLID with Rails - Andrii SavchenkoPractical SOLID with Rails - Andrii Savchenko
Practical SOLID with Rails - Andrii Savchenko
 
Open education - Artem Suchov
Open education - Artem SuchovOpen education - Artem Suchov
Open education - Artem Suchov
 
Getting new devs up to speed - Ivan Zarea
Getting new devs up to speed - Ivan ZareaGetting new devs up to speed - Ivan Zarea
Getting new devs up to speed - Ivan Zarea
 

Inside Out Ruby: Using MRI as a C library - Artur Pyrogovskyi