
f4931c9db84273876661288a1295e0cc.ppt
- Количество слайдов: 43
Ask Ko 1 YARV Yet Another Ruby. VM SASADA Koichi Tokyo University of Agriculture and Technology Nihon Ruby no Kai for Ruby. Conf 2004 1
Ko 1 に聞け YARV Yet Another Ruby. VM ささだ こういち 東京農 大学大学院 日本Rubyの会 2
Agenda l l l From Japanese Rubyist About me About YARV • Background • Design overview • Implementation • Current status 3
Messages from Japanese Rubyists l From Matz (Mr. Matsumoto) l From eban (Mr. Watanabe) l From Na. Hi (Mr. Nakamura) • よろしく。来年は会いたいね • Regards. I hope to meet you next year. • Learn Japanese • Don’t pick a fight. 4
Q. Who are you? A. Self Introduction l A Student for Ph. D. 1 st degree • Systems Software for Multithreaded Arch. • SMT / CMP or other technologies • i. e. : Hyper threading (Intel), CMT (Sun) • OS, Library, Compiler and Interpreter • YARV is the my first step for Parallel interpreter (? ) • Computer Architecture for Next Generation At Public Position 5
A. Self Introduction (cont. ) l Nihon Ruby no Kai l Rubyist Magazine (10 th Sep. ) l Favorite method • Rubyist community in Japan founded on 8 th Aug. • Organized by Mr. Takahashi (maki) • http: //jp. rubyst. net/magazine • __send__ • Recently, Japanese rubyist must say it 6
A. Self Introduction (cont. ) l Ko 1? l Works (part time job) • “Koichi” → 「こういち」 → 「耕一」 • 「一」 means “one” in Japanese • Kahua: Application Server framework in Scheme • Coins: a Compiler Infrastructure • The compiler framework languages in Java supporting various language • I don’t like Java language. But Eclipse is quite good (If I have a high performance machine) 7
A. Self Introduction (cont. ) l Software • Rava: A Java. VM in Ruby • My best joke software • Rucheme: A Scheme interpreter in Ruby • I like Scheme • Nadoka: IRC Client Server software • Ruby’s killer application (… nobody else may agree) 8
A. Self Introduction (cont. ) l Home page l Organize polls on many topics • http: //www. namikilab. tuat. ac. jp/~sasada/ • Of course in Japanese : ) • Like that… 9
Q. Why do you make YARV? A. Project Background l Ruby - Object Oriented Scripting Language • Very easy to use, but still powerful • Used world-wide • From Japan (These words for Japanese public officer) l But… Current Ruby interpreter is slow • Traverse Abstract Syntax Tree in runtime • Some projects chose other languages (e. g. Java) • because Ruby was just too slow for them And everyone says “Ruby? Ah, slow language” (myth) 10
A. Project Background (cont. ) l Byte code Interpreter seems to be good l Existing bytecode interpreter → not enough • Like Lisp, Java, . Net, Smalltalk, … • Matz’ try → incomplete • Byte. Code. Ruby (Mr. Marrows) → Slow (old info? ) • And other incomplete interpreters 11
Q. What is YARV? A. Project overview l l Creating a Ruby VM Funded by “Exploratory Software Development” – “Exploratory youth” • 未踏ソフトウェア創造事業 – 未踏ユース • By IPA – Information-technology Promotion • Agency, Japan. Another Ruby Project is accepted in this year • Ruby Compiler for. Net by Mr. Asakawa 12
A. Project overview (cont. ) l l l VM Instruction (insn. ) set “design” Compiler design and implementation(impl. ) VM design and impl. JIT (Just In Time) and AOT (Ahead Of Time) compiler design and impl. Will be published as Open Source Software 13
A. Project overview (cont. ) Ruby Script Compiler YARV – The Proposed System Ruby Instruction Sequence JIT Compiler AOT Compiler C Source code Ruby VM (Evaluator) Native Code C Compiler Executable Shared Library 14
Q. What’s the goal of YARV? A. Goal of YARV l To be Rite l To be ‘the Fastest Ruby. VM in the world’ l To enable all Ruby features • If YARV accomplished (Matz promised) • Now, rival is only current ruby interpreter : ) • Very important for claiming “Ruby. VM” name • It’s easy to make “Ruby Subset VM” • … but is it really Ruby? 15
Q. How to implement YARV? A. Development Policy l l Simple stack machine YARV Implemented as Ruby C Extension Not “Bytecode” but “Wordcode” Use Ruby’s existing Infrastructure • GC • Ruby Script parser • Ruby API is very useful in C programming • i. e) Memory Management • using Array without “free()” is very happy 16
A. Development Policy l Compiler parse Ruby’s AST • Ruby Script Parser creates Node tree • Traverse this tree and translate to YARV instructions (insns) • This compiler written in C Ruby Script Compiler with Ruby Parser Compiler (Ruby – Node) (Node – Insns) Ruby Abstract Syntax Tree Ruby insn. Sequence 17
Q. How to implement YARV? (2) A. Implementation - Registers l 5 registers • PC: Program Counter • SP: Stack Pointer • LFP: Local Frame Pointer • DFP: Dynamic Frame Pointer • CFP: Control Frame Pointer 18
A. Implementation - Frames l Frame types • Method Frame • Block Frame • Class Frame 19
A. Implementation - Frames (. cont) l l Method Frame • Same as other VMs • Identical to Class Frame Control frame • Every frame has this • Includes “self”, instruction sequence information, • continuation(keep last regs) CFP[0] == self Stack … Args Locals Block Environment LFP, DFP Control SP Self ISeq Cont. 20
A. Implementation - Frames (cont. ) l Block Frame • Created when ‘yield’ • LFP points to method • • Stack … Args LFP Locals local environment DFP point to current environment DFP[0] point to previous environment … Args Locals Block Prev Env Ctrl … Args Locals DFP Prev Env Ctrl 21 CFP SP
A. Implementation - Proc l Creating Proc Object • Proc enables indefinite extent • Moving environment to heap • LFP and DFP point CFP Block Proc LFP Env. Ctrl env. in heap # Proc sample def m arg; x = 0 iter{|a| i=1 iter{|b| j=2 Proc. new }}; end Stack … Args Locals Struct Proc. Object: VALUE self; VALUE *lfp; VALUE *dfp VALUE iseqobj; SP Env. DFP 22
A. Implementation - Block l Blocks are pushed on stack • A Block body is allocated by • area allocation like “alloca()” Used by ‘yield’ insn. # Block sample iter{. . . } Struct Block. Object: VALUE self; VALUE *lfp; VALUE *dfp VALUE iseqobj; Stack … Args Locals Block Ctrl Blcok Info 23
A. Implementation - Block (Proc) l Procs are pushed on stack • Used by ‘yield’ insn. • Same data structure as Proc • Can treat as Block object # Proc sample def m arg; x = 0 iter{|a| i=1 iter{|b| j=2 Proc. new }}; end Stack … Args Locals Proc Block Env. Ctrl # cont. Env. iter(m(arg)) Env. 24
A. Implementation Exception / Jump l Use exception table to handle Like Java. VM Types of entries • l • • Each entry have • Rescue clause • Ensure clause • Retry point • PC range • Continuation PC and SP If jump occurred, rewind stack and check this table 25
A. Implementation Exception / Jump (cont. ) l Different from Java and other ordinary VM • Must manage continuation SP register # Java can do this? v = begin FOO rescue BAR ensure BAZ end 26
A. Implementation - Ensure l If no error/jump occurred, ensure is done by normal instruction flow (copied / like recent Java compiler) # sample begin A ensure B end Compiled: Exception. Table: Start_A: entry: A type: ensure End_A: range: Start_A – End_A B do: B End_B: restart point: End_B end restart sp: 0 27
Q. What Insn does YARV has? A. Insn Category List l l l Insn names are not abbreviated Stack control • Swap, dup, … Accessor • get/setlocal, get/setconstant, … Put something • putobject, putnil, putarray, … Apply some change to object • concatstrings, … 28
A. Insn Category Lists l l l Method definition • methoddef Method call, Class/Module def • send, classdef, moduledef, yield, super, … Control flow • goto, if, unless, throw Optimization • get/setinlinecache, opt_plus, opt_…, … And others 29
Q. How to write each insn? A. Insn Description Language l Instruction Description Language • Body is written in C • Declare variables • Operands • Values popped from or pushed to the stack • Parsed by Ruby • This scheme enables flexible VM creation • Apply some optimization techs • Insert debug print • Make document automatically (like Ruby. Doc) 30
Q. Does YARV have optimizer? A. YARV Optimization Tech. l Inline cache • Global “VM state version” counter • It’s incremented when some side-effect change • (Re)Definition of Constant • (Re)Definition of Method • Cache some values with this count • If kept counter equals current counter you can • use cached value This scheme is used by Constant access and method search 31
A. YARV Optimization Tech. (cont. ) l Inline cache (cont. ) • Constant access needs some insns • A: : B: : C needs 4 insns • With this inline cache, this can be shortened to 1 insn • Method search • Current using Global method cache (which works wells) • Inline caching: planned (to be measured first) 32
A. YARV Optimization Tech. (cont. ) l Stack caching • 2 level stack caching • Cache 2 stack top values • With insn description, this can be automated 33
A. YARV Optimization Tech. (cont. ) l Super instructions l Make Special instruction l These techs are effective because: l I want to do these automatically from statistics data, But difficult? • Merge two (or more) insns to one insn • Replace frequent insn sequence with super insn. • Ie: putobject true → puttrue • give C compiler more opportunity for optimization 34
A. YARV Optimization Tech. (cont. ) l JIT compile • I’m searching easy way • Using existing libraries • Using copy code technique • Compile in C, and copy with label information • Seems hard and need much more effort 35
A. YARV Optimization Tech. (cont. ) l AOT compile • Substitute insn to C implementation code and • • compile with C compiler Description language will helps this Easy and C compiler optimization is powerful Output will be normal C extension method Very very simple examination shows x 100 speedup 36
Q. Does YARV move? A. Current Status l l Variables • Method local, block local, global, instance, Constants, … Class/Module definition Control flow • • if/unless, while/until, case/when begin/rescue/ensure, return, break/retry/next/redo Method invocation and yield block • • Call and yield with arguments, optional/rest arguments Call with block 37
Q. What’s the limitation on YARV? A. Follows l Can’t call Ruby from C l Missing some usefule Ruby features • It’s mean that “ 10000. times{ … }” doesn’t perform • To enable that, I must patch ruby/eval. c • Stack trace, set_trace_func, method_missing, • • call Proc as method visibility check, creating Proc object, … And many schemes : -P 38
Q. How fast YARV work? A. Benchmark result l l Everyone love benchmark and this result : ) I’ll show you at the conference 39
Q. Why Original System? A. Comparing to other systems l v. s. Java. VM, . Net • They have very nice library and optimizer • After all, it will be mapping to Ruby spec to VM’s • • l models Trade off between optimizer and Ruby stub Is it fun? v. s. Parrot • Register model VM really fast in “Interpreter”? • Is it fun? 40
Q. What are the rest tasks? A. Future Work l Implement complete Ruby specification Implement Optimizers l Collect benchmark program l • JIT/AOT compiler • Implement other interesting optimize tech. 41
Q. How to join YARV development? A. YARV Development community l Home page l Mailing list • http: //www. atdot. net/yarv/ • Install instructions and some information • Yarv-dev (in Japanese) • Yarv-devel (in English) … no one using 42
Q. Finished? A. Yes. l Thank you Special Thanks l Any other questions? Please “Ask Ko 1” : ) l • Alexander Kellett, Sanimir Agovic • Ruby-talk, Yarv-dev subscriber • Matz and other rubyists • IPA (my sponsor : ) ) 43
f4931c9db84273876661288a1295e0cc.ppt