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.8k
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
490
Railsの型ファイル自動生成における課題と解決
ksss
4
4.3k
RBS generation framework using Rack architecture
ksss
1
6.9k
mrubyでruby/specを走らせてみた結果www
ksss
1
2.4k
Rubyに型があると便利か
ksss
4
4.5k
A New Testing Framework Rgot
ksss
0
1.2k
Other Decks in Programming
See All in Programming
From Translations to Multi Dimension Entities
alexanderschranz
2
130
HTTP compression in PHP and Symfony apps
dunglas
2
1.7k
N.E.X.T LEVEL
pluu
2
300
103 Early Hints
sugi_0000
1
220
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
1.5k
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
240
The rollercoaster of releasing an Android, iOS, and macOS app with Kotlin Multiplatform | droidcon Italy
prof18
0
150
talk-with-local-llm-with-web-streams-api
kbaba1001
0
170
return文におけるstd::moveについて
onihusube
1
610
複雑な仕様に立ち向かうアーキテクチャ
myohei
0
170
Featured
See All Featured
Side Projects
sachag
452
42k
Designing for humans not robots
tammielis
250
25k
How GitHub (no longer) Works
holman
311
140k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
YesSQL, Process and Tooling at Scale
rocio
169
14k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Testing 201, or: Great Expectations
jmmastey
40
7.1k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.3k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.6k
Building Applications with DynamoDB
mza
91
6.1k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
Become a Pro
speakerdeck
PRO
26
5k
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