Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Extending Ruby with C
Search
Paul Mucur
May 08, 2012
Programming
1
280
Extending Ruby with C
A Learning Lunch presentation about writing C extensions for Ruby.
Paul Mucur
May 08, 2012
Tweet
Share
More Decks by Paul Mucur
See All by Paul Mucur
Do you really need breakfast?
mudge
0
130
HyperLogLog in 15 minutes
mudge
0
270
Exploring #to_proc
mudge
0
210
Testing Vimscript with Vimrunner
mudge
3
210
Configuration Management with Puppet
mudge
1
230
Managing Web Application Servers with Puppet
mudge
3
140
Other Decks in Programming
See All in Programming
月刊 競技プログラミングをお仕事に役立てるには
terryu16
1
1.2k
盆栽転じて家具となる / Bonsai and Furnitures
aereal
0
1.8k
ecspresso, ecschedule, lambroll を PipeCDプラグインとして動かしてみた (プロトタイプ) / Running ecspresso, ecschedule, and lambroll as PipeCD Plugins (prototype)
tkikuc
2
1.8k
EC2からECSへ 念願のコンテナ移行と巨大レガシーPHPアプリケーションの再構築
sumiyae
3
590
為你自己學 Python
eddie
0
520
テストコードのガイドライン 〜作成から運用まで〜
riku929hr
7
1.4k
HTML/CSS超絶浅い説明
yuki0329
0
190
ISUCON14感想戦で85万点まで頑張ってみた
ponyo877
1
590
Alba: Why, How and What's So Interesting
okuramasafumi
0
210
ErdMap: Thinking about a map for Rails applications
makicamel
1
600
ある日突然あなたが管理しているサーバーにDDoSが来たらどうなるでしょう?知ってるようで何も知らなかったDDoS攻撃と対策 #phpcon.2024
akase244
2
7.7k
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
400
Featured
See All Featured
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7.1k
Speed Design
sergeychernyshev
25
740
Docker and Python
trallard
43
3.2k
Designing for humans not robots
tammielis
250
25k
Practical Orchestrator
shlominoach
186
10k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
Scaling GitHub
holman
459
140k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.8k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.4k
Git: the NoSQL Database
bkeepers
PRO
427
64k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.3k
Transcript
EXTENDING RUBY with C
Why?
PERFORMANCE
PERFORMANCE
C BINDINGS
C BINDINGS FFI
None
None
array << 1
static VALUE rb_ary_push_1(VALUE ary, VALUE item) {
long idx = RARRAY_LEN(ary); if (idx >= ARY_CAPA(ary)) { ary_double_capa(ary, idx); } RARRAY_PTR(ary)[idx] = item; ARY_SET_LEN(ary, idx + 1); return ary; }
EXT/WORLD/WORLD.C
> World.hello => "Hello, World!"
require "world" describe World do describe "#hello" do
it "greets the world" do World.hello.should == "Hello, World!" end end end
module World module_function def hello
"Hello, World!" end end
None
HELLO
return rb_str_new("Hello, World!", 13);
static VALUE world_hello(VALUE obj) { return rb_str_new("Hello, World!", 13);
}
WORLD
VALUE world_mWorld = rb_define_module("World"); rb_define_module_function(world_mWorld, "hello", world_hello,
0);
#include <ruby.h> static VALUE world_hello(VALUE obj) { return rb_str_new("Hello,
World!", 13); } void Init_world(void) { VALUE world_mWorld = rb_define_module("World"); rb_define_module_function(world_mWorld, "hello", world_hello, 0); }
COMPILATION
require "mkmf"
require "mkmf" $CFLAGS << " -‐Wall -‐Wextra" create_makefile("world")
$ ruby ext/world/extconf.rb creating Makefile
$ make compiling ext/world/world.c linking shared-‐object world.bundle
$ rspec spec/world_spec.rb . Finished in 0.00399 seconds 1
example, 0 failures
None
$ rake compile
require 'rake/extensiontask' require 'rspec/core/rake_task' Rake::ExtensionTask.new('world') RSpec::Core::RakeTask.new('test') task :test
=> :compile task :default => :test
it "greets someone by name, if given" do World.hello("Bob").should
== "Hello, Bob!" end
“If argc is -1, the function will be called as:
VALUE func(int argc, VALUE *argv, VALUE obj) where argc is the actual number of arguments, argv is the C array of the arguments, and obj is the receiver.”
def hello(name = nil) if name
"Hello, #{name}!" else "Hello, World!" end end
rb_define_module_function(world_mWorld, "hello", world_hello, -‐1);
static VALUE world_hello(int argc, VALUE *argv, VALUE obj) {
VALUE name; rb_scan_args(argc, argv, "01", &name); ... }
static VALUE world_hello(int argc, VALUE *argv, VALUE obj) {
VALUE name; rb_scan_args(argc, argv, "01", &name); if (NIL_P(name)) { return rb_str_new("Hello, World!", 13); } else { ... } }
static VALUE world_hello(int argc, VALUE *argv, VALUE obj) {
VALUE name, greeting; rb_scan_args(argc, argv, "01", &name); if (NIL_P(name)) { greeting = rb_str_new("Hello, World!", 13); } else { greeting = rb_str_new("Hello, ", 7); rb_str_cat(greeting, RSTRING_PTR(name), RSTRING_LEN(name)); rb_str_cat(greeting, "!", 1); } return greeting; }
$ rake clean test rm -‐r tmp/x86_64-‐darwin11.3.0/world/1.9.3 mkdir -‐p tmp/x86_64-‐darwin11.3.0/world/1.9.3
cd tmp/x86_64-‐darwin11.3.0/world/1.9.3 /Users/mudge/.rbenv/versions/1.9.3-‐p125/bin/ruby -‐ I. ../../../../ext/world/extconf.rb creating Makefile cd -‐ cd tmp/x86_64-‐darwin11.3.0/world/1.9.3 make compiling ../../../../ext/world/world.c linking shared-‐object world.bundle cd -‐ install -‐c tmp/x86_64-‐darwin11.3.0/world/1.9.3/world.bundle lib/ world.bundle /Users/mudge/.rbenv/versions/1.9.3-‐p125/bin/ruby -‐S rspec ./ spec/world_spec.rb .. Finished in 0.00081 seconds 2 examples, 0 failures
None
QUESTIONS?