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
ksss
April 19, 2014
Programming
7
1.9k
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
630
Railsの型ファイル自動生成における課題と解決
ksss
4
4.7k
RBS generation framework using Rack architecture
ksss
1
7.3k
mrubyでruby/specを走らせてみた結果www
ksss
1
2.5k
Rubyに型があると便利か
ksss
4
4.6k
A New Testing Framework Rgot
ksss
0
1.3k
Other Decks in Programming
See All in Programming
LT 2025-06-30: プロダクトエンジニアの役割
yamamotok
0
760
システム成長を止めない!本番無停止テーブル移行の全貌
sakawe_ee
1
200
PHPで始める振る舞い駆動開発(Behaviour-Driven Development)
ohmori_yusuke
2
390
テストから始めるAgentic Coding 〜Claude Codeと共に行うTDD〜 / Agentic Coding starts with testing
rkaga
12
4.5k
ふつうの技術スタックでアート作品を作ってみる
akira888
1
860
ISUCON研修おかわり会 講義スライド
arfes0e2b3c
1
450
A2A プロトコルを試してみる
azukiazusa1
2
1.4k
ペアプロ × 生成AI 現場での実践と課題について / generative-ai-in-pair-programming
codmoninc
1
18k
「Cursor/Devin全社導入の理想と現実」のその後
saitoryc
0
820
ソフトウェア品質を数字で捉える技術。事業成長を支えるシステム品質の マネジメント
takuya542
1
13k
生成AI時代のコンポーネントライブラリの作り方
touyou
1
220
脱Riverpod?fqueryで考える、TanStack Queryライクなアーキテクチャの可能性
ostk0069
0
150
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
72
4.9k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
6
300
Making the Leap to Tech Lead
cromwellryan
134
9.4k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
50
5.5k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
970
Scaling GitHub
holman
460
140k
Building Flexible Design Systems
yeseniaperezcruz
328
39k
Producing Creativity
orderedlist
PRO
346
40k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.7k
Measuring & Analyzing Core Web Vitals
bluesmoon
7
510
Rebuilding a faster, lazier Slack
samanthasiow
82
9.1k
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