Slide 1

Slide 1 text

A Few Things I Know About Ruby Implementation Yuki Sasada(Torii) 
 March.2015 RubyConf.ph

Slide 2

Slide 2 text

I’m Yukiʢઇʣ. “Yuki” means snow in Japanese. I’m a Rails application developer of Everyleaf .Inc.
 2

Slide 3

Slide 3 text

Have you read ‘Ruby’ ? 3

Slide 4

Slide 4 text

The first thing you do. HJUDMPOFIUUQTHJUIVCDPNSVCZSVCZHJU 4

Slide 5

Slide 5 text

Your Map and Compass ❖ Ruby Under a Microscope ❖ Ruby Hacking Guide 5

Slide 6

Slide 6 text

Ruby Under a Microscope • Pat Shaughnessy • ISBN: 9781593275273 • an Illustrated Guide
 to Ruby Internals. MAP: 6

Slide 7

Slide 7 text

Ruby Under a Microscope a large scale Map of Ruby 7 Class MyCode deff my Your Ruby Perse Compile AST ByteCode VALUE RObject numiv ivptr flags klass RBasic PARSE-COMPILE DATA-STRUCTURE

Slide 8

Slide 8 text

Ruby Hacking Guide (RHG) • Minero Aoki • All contents are available at
 http://ruby-hacking- guide.github.io/ • Talking about very details on Ruby source code and implementation. • about Ruby ver. 1.7.3… Compass: Old, But Very Very Useful. 8

Slide 9

Slide 9 text

Why RHG is Useful? 
 Because it’s very Universal. ❖ “How to read complex source codes” ❖ making a method simple by cutting not essential code off. ❖ understanding object structure before. ❖ and many other wisdoms essence 9

Slide 10

Slide 10 text

Why RHG is Useful? 
 Because It is OLD. ❖ Ruby is improving. ❖ Difference between RHG and current code is a trace of improvement.
 You can trace it by ChangeLog
 and 10

Slide 11

Slide 11 text

source file map BSDL COPYING COPYING.ja ChangeLog GPL KNOWNBUGS.rb LEGAL Makefile.in NEWS README.EXT README.EXT.ja README.ja.md README.md addr2line.c addr2line.h array.c benchmark bignum.c bin boots traptest class.c common.mk compar.c compile.c complex.c configure.in constant.h cont.c cygwin debug.c defs dir.c dln.c dln.h dln_find.c dmydln.c dmyext.c doc enc encoding.c enum.c en umerator.c error.c eval.c eval_error.c eval_intern.h eval_jump.c ext file.c gc.c gc.h gem_prelude.rb golf_prelude.rb goruby.c hash.c ia64.s include inits.c insns.def internal.h io.c iseq.c iseq.h lex.c.blt lib load.c loadpath.c localeinit.c main.c man marshal.c math.c method.h miniinit.c misc missing nacl node.c node.h numeric.c object.c pack.c parse.y prelude.rb probes.d probes_helper.h proc.c process.c random.c range.c rational.c re.c regcomp.c regenc.c regenc.h regerror.c regexec.c regint.h regparse.c regparse.h regsyntax.c ruby.c ruby_atomic.h safe.c sample signal.c siphash.c siphash.h sparc.c spec sprintf.c st.c strftime.c string.c struct.c symbian template test thread.c thread_native.h thread_pthread.c thread_pth read.h thread_win32.c thread_win32.h time.c timev.h tool transcode.c transcode_data.h util.c variable.c version.c version.h vm.c vm_backtrace.c vm_core.h vm_debug.h vm_dump.c vm_eval .c vm_exec.c vm_exec.h vm_insnhelper.c vm_insnhelper.h vm_method.c vm_opts.h vm_trace.c vsnprintf.c win32 > cd ./ruby; ls (despair…) 11

Slide 12

Slide 12 text

source file map BSDL COPYING COPYING.ja ChangeLog GPL KNOWNBUGS.rb LEGAL Makefile.in NEWS README.EXT README.EXT.ja README.ja.md README.md addr2line.c addr2line.h array.c benchmark bignum.c bin boots traptest class.c common.mk compar.c compile.c complex.c configure.in constant.h cont.c cygwin debug.c defs dir.c dln.c dln.h dln_find.c dmydln.c dmyext.c doc enc encoding.c enum.c en umerator.c error.c eval.c eval_error.c eval_intern.h eval_jump.c ext file.c gc.c gc.h gem_prelude.rb golf_prelude.rb goruby.c hash.c ia64.s include inits.c insns.def internal.h io.c iseq.c iseq.h lex.c.blt lib load.c loadpath.c localeinit.c main.c man marshal.c math.c method.h miniinit.c misc missing nacl node.c node.h numeric.c object.c pack.c parse.y prelude.rb probes.d probes_helper.h proc.c process.c random.c range.c rational.c re.c regcomp.c regenc.c regenc.h regerror.c regexec.c regint.h regparse.c regparse.h regsyntax.c ruby.c ruby_atomic.h safe.c sample signal.c siphash.c siphash.h sparc.c spec sprintf.c st.c strftime.c string.c struct.c symbian template test thread.c thread_native.h thread_pthread.c thread_pth read.h thread_win32.c thread_win32.h time.c timev.h tool transcode.c transcode_data.h util.c variable.c version.c version.h vm.c vm_backtrace.c vm_core.h vm_debug.h vm_dump.c vm_eval .c vm_exec.c vm_exec.h vm_insnhelper.c vm_insnhelper.h vm_method.c vm_opts.h vm_trace.c vsnprintf.c win32 Don't give up your hope. File names are easy to understand. and README.EXT has
 “Appendix A. Ruby Source Files Overview” 12

Slide 13

Slide 13 text

source file map ”Ruby Source Files Overview” in README.EXT groups source files by their contents. Ruby Language Core class.c :: classes and modules error.c :: exception classes and exception mechanism gc.c :: memory management load.c :: library loading object.c :: objects variable.c :: variables and constants Class Library array.c :: Array bignum.c :: Bignum compar.c :: Comparable complex.c :: Complex cont.c :: Fiber, Continuation dir.c :: Dir enum.c :: Enumerable enumerator.c :: Enumerator file.c :: File hash.c :: Hash io.c :: IO marshal.c :: Marshal math.c :: Math numeric.c :: Numeric, Integer, Fixnum, Float pack.c :: Array#pack, String#unpack proc.c :: Binding, Proc process.c :: Process random.c :: random number range.c :: Range rational.c :: Rational re.c :: Regexp, MatchData signal.c :: Signal sprintf.c :: String#sprintf string.c :: String struct.c :: Struct time.c :: Time Ruby Syntax Parser parse.y :: grammar definition parse.c :: automatically generated from parse.y keywords :: reserved keywords lex.c :: automatically generated from keywords Ruby Evaluator (a.k.a. YARV), Regular Expression Engine (Oniguruma), Utility Functions, Ruby Interpreter Implementation, Multilingualization, goruby Interpreter Implementation and other groups

Slide 14

Slide 14 text

Ruby Language Core class.c error.c gc.c load.c object.c variable.c Ruby Syntax Parser parse.y :: grammar definition parse.c :: automatically generated from parse.y keywords :: reserved keywords lex.c :: automatically generated from keywords Ruby Evaluator (a.k.a. YARV) Regular Expression Engine (Oniguruma) Utility Functions Ruby Interpreter Implementation Multilingualization goruby Interpreter Implementation and other groups Class Library array.c :: Array bignum.c :: Bignum compar.c :: Comparable complex.c :: Complex cont.c :: Fiber, Continuation dir.c :: Dir enum.c :: Enumerable enumerator.c :: Enumerator file.c :: File hash.c :: Hash io.c :: IO Easy file names

Slide 15

Slide 15 text

extension also give us hints. xx.c => C code xx.h => header file. Many definitions such as macros, data-structures. xx.y => only parse.y (grammar definition) xx.rb => ruby file. xx.def => miscellaneous definitions 15

Slide 16

Slide 16 text

Special Directories /include/ … many ruby’s core header files. /defs/ … definition files such as keywords, errors… /ext/ … c extensions such as json, socket… /lib/ …ruby libraries such as csv, irb, yaml, rake… /missing/ …implements of C functions need for ruby,
 but not all Systems have. /test/ … test codes for ruby implementations. /spec/ … RubySpec will be downloaded here. 16

Slide 17

Slide 17 text

Google Map: Pry and Pry-doc HFNJOTUBMMQSZQSZEPD QSZ TIPXTPVSDF)BTIUP@B

Slide 18

Slide 18 text

Google Map: Pry and Pry-doc <>QSZ NBJO TIPXTPVSDF)BTIUP@B 'SPNIBTID $.FUIPE 0XOFS)BTI 7JTJCJMJUZQVCMJD /VNCFSPGMJOFT TUBUJD7"-6& SC@IBTI@UP@B 7"-6&IBTI \ 7"-6&BSZ BSZSC@BSZ@OFX SC@IBTI@GPSFBDI IBTI UP@B@J BSZ 0#+@*/'&$5 BSZ IBTI SFUVSOBSZ ^

Slide 19

Slide 19 text

NOW, YOU GOT A READY TO TRIP. 19

Slide 20

Slide 20 text

A Short Trip: Instance variable @ivar 20

Slide 21

Slide 21 text

How Instance variable is implemented in Ruby? Instance variable has a pair of name and value,
 like @ivar = “ivar”…. 21

Slide 22

Slide 22 text

Like This? obj name value @ivar "ivar" @name "foo" @admin FALSE obj name value @ivar "ivar2" @name "buzz" @currency “PHP” obj name value @ivar “ivar3" @name “bar" @admin TRUE 22

Slide 23

Slide 23 text

It ‘was’ the answer. Until Ruby 1.8 .
 (So Ruby Hacking Guide, which is about ver.1.7, describes ivar like this.) 23

Slide 24

Slide 24 text

Instance variable in over Ruby 1.9 obj obj obj “ivar” “foo” FALSE nil “ivar2” “buzz” nil “PHP” “ivar3” “bar” TRUE nil Object has array of values. But, wait, where is ‘Name’ of the value; ‘@ivar’? 24

Slide 25

Slide 25 text

Names of ivars are in Class. obj obj Class name index @ivar 0 @name 1 @admin 2 @currency 3 “ivar” “foo” FALSE nil “ivar2” “buzz” nil “PHP” iv_index_tbl 25

Slide 26

Slide 26 text

The process of ‘instance_variable_get’ 1. search the object’s class 2. get index value of @ivar from iv_index_tbl of the class 3. get the value from the array of value of the object by the index obj.instance_variable_get(:@ivar) obj obj Class “ivar” “foo” FALSE nil “ivar2” “buzz” nil “PHP” name index @ivar 0 @name 1 @admin 2 @currency 3 iv_index_tbl 1 2 3 26

Slide 27

Slide 27 text

obj Why the change? name value @ivar “ivar" @name “fuzz” “ivar” “fuzz” 12 [5, 8] > More objects generated, More memory is needed Memory obj 27

Slide 28

Slide 28 text

in source of rb_ivar_lookup TUBUJD7"-6& SC@JWBS@MPPLVQ 7"-6&PCK *%JE 7"-6&VOEFG \ 7"-6&WBM QUS TUSVDUTU@UBCMFJW@JOEFY@UCM MPOHMFO TU@EBUB@UJOEFY JG 41&$*"-@$0/45@1 PCK HPUPHFOFSJD TXJUDI #6*-5*/@5:1& PCK \ DBTF5@0#+&$5 MFO30#+&$5@/6.*7 PCK QUS30#+&$5@*7153 PCK JW@JOEFY@UCM30#+&$5@*7@*/%&9@5#- PCK JG JW@JOEFY@UCM CSFBL JG TU@MPPLVQ JW@JOEFY@UCM TU@EBUB@U JE JOEFY CSFBL JG MFO MPOH JOEFY CSFBL WBMQUS JG WBM2VOEFG SFUVSOWBM CSFBL DBTF5@$-"44 [email protected]%6-& JG 3$-"44@*7@5#- PCK TU@MPPLVQ 3$-"44@*7@5#- PCK TU@EBUB@U JE JOEFY SFUVSO 7"-6& JOEFY CSFBL EFGBVMU HFOFSJD JG '-@5&45 PCK '-@&9*7"3 ]]SC@TQFDJBM@DPOTU@Q PCK SFUVSOHFOFSJD@JWBS@HFU PCK JE VOEFG CSFBL ^ SFUVSOVOEFG ^ • T_OBJECT is the class that we define in our code. • SPECIAL_CONST_P is simple ruby value like Integer or Symbol • default: Built-in type class without T_OBJECT (and T_CLASS, T_MODULE) 
 ex. String, Array, Regexp 28

Slide 29

Slide 29 text

Can “string” have @ivar? a instance of String dose not have pointer to iv. “string” “ivar” “yuki” 12 [5, 8] > str = “string” 29

Slide 30

Slide 30 text

String can. > str = “String” => “String” > str.instance_variables => [ ] > str.instance_variables_set(“@val1”, “value one”) => “value one” > str.instance_variables => [:@val1] 30

Slide 31

Slide 31 text

Built-in Class(not T-Object type)
 stores its instance variables in generic_iv_tbl generic_iv_tbl “string” hash [a, 1] hash @ val1 “value one” @val2 “value two” Simple Ruby Value, Integer, Symbol…etc, also refers genetic_iv_tbl but find nothing now. “string” 31

Slide 32

Slide 32 text

The flow of looking up the value of instance variable is simple ruby value
 (integer, symbol…) true false Type of Class is T_OBJECT simple ruby value not T_OBJECT generic_iv_tbl own class’s iv_indx_tbl and values 32 nor T_CLASS,T_MODULE

Slide 33

Slide 33 text

You can find all of them in ❖ rb_ivar_get ❖ rb_ivar_set ❖ rb_ivar_lookup in “variable.c” 33

Slide 34

Slide 34 text

next trip? Ruby internal is deep and vast. if you enjoy this trip, I hope you will got your own trip with maps and compass. 34

Slide 35

Slide 35 text

Enjoy your Ruby Life. Thank you!