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
620
Railsの型ファイル自動生成における課題と解決
ksss
4
4.6k
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
生成AIコーディングとの向き合い方、AIと共創するという考え方 / How to deal with generative AI coding and the concept of co-creating with AI
seike460
PRO
1
290
Webからモバイルへ Vue.js × Capacitor 活用事例
naokihaba
0
680
Cline指示通りに動かない? AI小説エージェントで学ぶ指示書の書き方と自動アップデートの仕組み
kamomeashizawa
1
520
Go1.25からのGOMAXPROCS
kuro_kurorrr
1
710
社内での開発コミュニティ活動とモジュラーモノリス標準化事例のご紹介/xPalette and Introduction of Modular monolith standardization
m4maruyama
1
120
セキュリティマネジャー廃止とクラウドネイティブ型サンドボックス活用
kazumura
1
170
人には人それぞれのサービス層がある
shimabox
3
680
The Evolution of Enterprise Java with Jakarta EE 11 and Beyond
ivargrimstad
1
740
UPDATEがシステムを複雑にする? イミュータブルデータモデルのすすめ
shimomura
1
550
データベースコネクションプール(DBCP)の変遷と理解
fujikawa8
1
260
[初登壇@jAZUG]アプリ開発者が気になるGoogleCloud/Azure+wasm/wasi
asaringo
0
130
Elixir で IoT 開発、 Nerves なら簡単にできる!?
pojiro
1
140
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
351
20k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
161
15k
How to Think Like a Performance Engineer
csswizardry
24
1.7k
Why You Should Never Use an ORM
jnunemaker
PRO
56
9.4k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
Making the Leap to Tech Lead
cromwellryan
134
9.3k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
2.8k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
46
9.6k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.5k
A Tale of Four Properties
chriscoyier
159
23k
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