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
mruby hacking guide
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
ksss
April 19, 2014
Programming
7
2k
mruby hacking guide
大江戸Ruby会議04 Ninja Talks
http://regional.rubykaigi.org/oedo04/
ksss
April 19, 2014
Tweet
Share
More Decks by ksss
See All by ksss
RaaP
ksss
0
700
Railsの型ファイル自動生成における課題と解決
ksss
4
5.1k
RBS generation framework using Rack architecture
ksss
1
7.7k
mrubyでruby/specを走らせてみた結果www
ksss
1
2.5k
Rubyに型があると便利か
ksss
4
4.7k
A New Testing Framework Rgot
ksss
0
1.3k
Other Decks in Programming
See All in Programming
そのAIレビュー、レビューしてますか? / Are you reviewing those AI reviews?
rkaga
6
4.5k
AWS re:Invent 2025参加 直前 Seattle-Tacoma Airport(SEA)におけるハードウェア紛失インシデントLT
tetutetu214
2
110
ThorVG Viewer In VS Code
nors
0
770
Automatic Grammar Agreementと Markdown Extended Attributes について
kishikawakatsumi
0
180
Oxlintはいいぞ
yug1224
5
1.3k
Implementation Patterns
denyspoltorak
0
280
CSC307 Lecture 06
javiergs
PRO
0
680
AI Agent Tool のためのバックエンドアーキテクチャを考える #encraft
izumin5210
6
1.8k
20260127_試行錯誤の結晶を1冊に。著者が解説 先輩データサイエンティストからの指南書 / author's_commentary_ds_instructions_guide
nash_efp
0
930
CSC307 Lecture 03
javiergs
PRO
1
490
登壇資料を作る時に意識していること #登壇資料_findy
konifar
4
1k
KIKI_MBSD Cybersecurity Challenges 2025
ikema
0
1.3k
Featured
See All Featured
Practical Orchestrator
shlominoach
191
11k
Writing Fast Ruby
sferik
630
62k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
180
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
160
The Cult of Friendly URLs
andyhume
79
6.8k
Unsuck your backbone
ammeep
671
58k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
So, you think you're a good person
axbom
PRO
2
1.9k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
640
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
90
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
7.9k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
3.9k
Transcript
mruby hacking guide 第256回 Asakusa.rb 2014/04/19 大江戸Ruby会議04
IUUQJMPWFSVCZOFUKBSIHCPPL w .JOFSP"PLJ w 3VCZJTUͷόΠϒϧ w ճಡ͏ʂʂ
about me { name: "Yuki Kurihara", twitter: "@_ksss_", github: "ksss",
commit: "mruby(59)", since: "Feb 26, 2014" }
生活発表会
3VCZͱͷग़ձ͍ ・phpに危機感 ・そうだ、外へ出よう ・RubyKaigi2013に行ってみる
3VCZͱͷग़ձ͍ ・@_zzakのスピーチに感動 ・完全にRubyにハマる ・ゴミみたいなGemを量産
"TBLVTBSCͱͷग़ձ͍ ・今のおまえに足りないものがある ・危機感だ
"TBLVTBSCͱͷग़ձ͍ ・そうだ、Asakusa.rbへ行こう
"TBLVTBSCͱͷग़ձ͍ ・なんか無言でパチパチしてる
"TBLVTBSCͱͷग़ձ͍ ・みんな好き勝手やっているだけ
"TBLVTBSCͱͷग़ձ͍ ・なんだ、好き勝手やっていいんだ
Gemを量産して わかったこと
Rubyの実装は宝の山
None
Ruby is 21 years old Ruby is 21 years old
Ruby is red ocean Ruby is red ocean
None
mruby is 2 years old mruby is 2 years old
mruby is blue ocean mruby is blue ocean
mruby ・コードが短い ・シンプルで読みやすい ・Matzがcommitしてる
✔ github ✖ mail ✖ redmine IUUQTHJUIVCDPNNSVCZNSVCZ
Let's Hack mruby
VM Object lib Build GC parse.y
“Bad programmers worry about the code. Good programmers worry about
data structures and their relationships.” Linus Torvalds
ʮඞཁͳσʔλߏମʹʯ mrubyの方針 ʮγεςϜʹԿ͔Λڧ੍͠ͳ͍ʯ
「データ構造重要」
VM Object lib Build GC parse.y
RString
RString ptr len capa lNSVCZz 5 5
Hack
RString ptr len capa lNSVCZz 5 5
RString ptr len capa void * void * void *
RVALUE
RString lNSVCZz ptr len capa
RString ptr len capa “ m r u b y
” as.ary[]
RString struct RString { MRB_OBJECT_HEADER; mrb_int len; union { mrb_int
capa; struct mrb_shared_string *shared; } aux; char *ptr; };
RString struct RString { MRB_OBJECT_HEADER; union { struct { mrb_int
len; union { mrb_int capa; struct mrb_shared_string *shared; } aux; char *ptr; } heap; char ary[RSTRING_EMBED_LEN_MAX + 1]; } as; };
Embed-String ・less -10% memory allocs ! でもこれって…… https://github.com/mruby/mruby/pull/1820
CRubyから パクった
͜͜Ͱ͘Β͍
Enumerator
昔のHash#each #call-seq: # hsh.each {|k, v| block} -> hsh #
hsh.each -> an_enumerator def each(&block) self.keys.each{|k| block.call([k, self[k]]) } self end
!?
昔のHash#each #call-seq: # hsh.each {|k, v| block} -> hsh #
hsh.each -> an_enumerator def each(&block) self.keys.each{|k| block.call([k, self[k]]) } self end
Hack
NSVCZFOVNFSBUPS IUUQTHJUIVCDPNLTTTNSVCZFOVNFSBUPS
NHFN IUUQTHJUIVCDPNNSVCZNHFNMJTU IUUQTHJUIVCDPNCPWJNHFN NHFNMJTU
Send Pull Request to mruby/mgem-list
IUUQTHJUIVCDPNNSVCZNHFNMJTUQVMM %JEZPVNJTUBLFSFQPTJUPSZ GPSTFOEJOHQVMMSFRVFTU IUUQTHJUIVCDPNNSVCZNSVCZ
/src/* /mrblib/* /mrbgems/* mgem ISO mgem mgem Ruby互換 Freedom
Send Pull Request to mruby/mruby
Merged IUUQTHJUIVCDPNNSVCZNSVCZQVMM
CRubyから パクった
khash
khash ✔IBTIςʔϒϧ ✔$3VCZͰݴ͏TU@UBCMF ✔IU NU JW OT TU ✔γϯϓϧগϝϞϦ ✔։൪๏
✔
khash n_buckets size n_occupied mask inc upper_bound keys vals ed_flags
kh_*
khash keys vals ed_flags malloc( )
khash keys vals ed_flags n_buckets=2
khash keys vals ed_flags empty del khkey_t khval_t n_buckets=1 1
0 1 0 1 0 1 0
hash[a]=1 a 1 0 1 0 1 0 1 0
hash function empty del
hash[a]=1 a 1 hash function a 1 0 0 0
1 0 1 0 empty del 1
hash[b]=2 b 1 hash function a b 1 0 0
0 0 0 1 0 1 2 empty del
hash[b]=2 b 1 hash function del b 1 0 0
1 0 0 1 0 - 2 empty del
Hack
khash n_buckets size n_occupied mask inc upper_bound keys vals ed_flags
kh_* n_buckets-1 n_buckets/2-1 n_buckets*3/4
khash n_buckets size n_occupied mask inc upper_bound keys vals ed_flags
kh_* n_buckets-1 n_buckets/2-1 n_buckets*3/4
kh_get delを無視 kh_put delを上書き khash
khash hashいくつ? 空だった? うん 3 hashいくつ? 3 空だった? うん そこ入れといて
kh_get kh_put
khash hashいくつ? 空だった? うん 3 hashいくつ? 3 空だった? うん そこ入れといて
khash hashいくつ? 空だった? うん 3 そこ入れといて おっけー kh_put
PRしようとしたら matzに先を越された IUUQTHJUIVCDPNNSVCZNSVCZDPNNJU BFDEBGCDFFBFCEDCECGCDDDFCFE
͜Ε͔ΒͬͯΈ͍ͨ͜ͱ ɾIBTIΛ࿈๏Ͱ࣮ͯ͠ൺֱ ɾWN ($ΛಡΜͰվળ ɾͻͨ͢Βόάফ͠
FOE
Hack