Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Few Things I Know About Ruby Implementation

A Few Things I Know About Ruby Implementation

My RubyConf.PH 2015 presentation.
About implementation of MRI, instance variable.

やきとりい

March 28, 2015
Tweet

More Decks by やきとりい

Other Decks in Programming

Transcript

  1. I’m Yukiʢઇʣ. “Yuki” means snow in Japanese. I’m a Rails

    application developer of Everyleaf .Inc.
 2
  2. Ruby Under a Microscope • Pat Shaughnessy • ISBN: 9781593275273

    • an Illustrated Guide
 to Ruby Internals. MAP: 6
  3. 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
  4. 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
  5. 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
  6. 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 <git log> 10
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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 ^
  14. How Instance variable is implemented in Ruby? Instance variable has

    a pair of name and value,
 like @ivar = “ivar”…. 21
  15. 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
  16. It ‘was’ the answer. Until Ruby 1.8 .
 (So Ruby

    Hacking Guide, which is about ver.1.7, describes ivar like this.) 23
  17. 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
  18. 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
  19. 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
  20. 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
  21. in source of rb_ivar_lookup TUBUJD7"-6& SC@JWBS@MPPLVQ 7"-6&PCK *%JE 7"-6&VOEFG 

    \ 7"-6&WBM  QUS TUSVDUTU@UBCMF JW@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<JOEFY> JG WBM2VOEFG  SFUVSOWBM CSFBL DBTF5@$-"44 DBTF[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
  22. Can “string” have @ivar? a instance of String dose not

    have pointer to iv. “string” “ivar” “yuki” 12 [5, 8] > str = “string” 29
  23. String can. > str = “String” => “String” > str.instance_variables

    => [ ] > str.instance_variables_set(“@val1”, “value one”) => “value one” > str.instance_variables => [:@val1] 30
  24. 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
  25. 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
  26. You can find all of them in ❖ rb_ivar_get ❖

    rb_ivar_set ❖ rb_ivar_lookup in “variable.c” 33
  27. 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