38. EPMO Open Source Coordination Office Redaction File Detail Report

Produced by Araxis Merge on 11/16/2017 4:02:38 PM Eastern Standard Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.

38.1 Files compared

# Location File Last Modified
1 Thu Nov 16 21:02:37 2017 UTC
2 CUI-CPP-v2.3.1-release.zip\public\fake_docstore 2016_rubykaigi.docx Thu Nov 16 20:12:37 2017 UTC

38.2 Comparison summary

Description Between
Files 1 and 2
Text Blocks Lines
Unchanged 0 0
Changed 0 0
Inserted 1 1199
Removed 0 0

38.3 Comparison options

Whitespace
Character case Differences in character case are significant
Line endings Differences in line endings (CR and LF characters) are ignored
CR/LF characters Not shown in the comparison detail

38.4 Active regular expressions

No regular expressions were active.

38.5 Comparison detail

        1   A proposal  of
        2   new concur rency mode l for Ruby  3
        3  
        4   Koichi Sas ada
        5   ko1@heroku .com
        6  
        7   People lov e “Concurr ency”
        8  
        9  
        10  
        11  
        12  
        13  
        14  
        15  
        16  
        17  
        18  
        19   Concurrent
        20   RubyKaigi
        21   (at least,  there are  two
        22   parallel s essions)
        23  
        24   Why people  love (to  discuss)
        25   “Concurren cy”?
        26   • Performa nce by “Pa rallel” ex ecution to  utilize
        27   multiple-c ores
        28   • Ruby has  thread sy stem, but  MRI doesn’ t permit t o allow pa rallel exe cution.
        29  
        30   About this  presentat ion
        31   • Show “Wh y difficul t multi-th reads prog rams”
        32   • Propose  new concur rent and p arallel me chanism id ea named “ Guild”
        33   For Ruby 3
        34  
        35   Koichi Sas ada
        36   •A program mer living  in Tokyo,  Japan
        37   •Ruby core  committer  since 200 7
        38   •YARV, Fib er, … (Rub y 1.9)
        39   •RGenGC, R incGC(Ruby  2…)
        40  
        41   Koichi is  an Employe e
        42  
        43  
        44  
        45  
        46  
        47  
        48  
        49  
        50  
        51  
        52  
        53  
        54  
        55  
        56  
        57  
        58  
        59  
        60   Difficulty  of
        61   Multi-thre ads progra mming
        62  
        63   Programmin g language  evolution
        64   • Trade-of f: Perform ance v.s.  Safety/Eas ily
        65   Performanc e: making  faster pro grams
        66  
        67   Safety: ma king bug-f ree progra ms
        68   Easily: ma king progr ams with s mall effor ts
        69  
        70   Two exampl e
        71   C language
        72   • String m anipulatio n with poi nters
        73   • Memory m anagement  without GC
        74  
        75   String man ipulation  with point ers
        76   • C: Using  raw point ers to man ipulate st rings
        77   Good: all- purpose an d fast
        78   Bad: Error -prone
        79   Generates  strange be havior, su ch as abno rmal termi nation
        80   • Ruby: Wr ap with St ring class
        81   Good: Easy  to use
        82   Bad: slowe r than C i n some cas es
        83  
        84   Object man agement wi thout GC
        85   • C: Free  memory obj ects manua lly
        86   Good: full  control ( target, ti ming and s o on)
        87   Bad: Error -prone
        88   double-fre e/memory-l eak, …
        89   • Ruby: Au tomatic co llection w ith GC
        90   Good: noth ing to car e about ob ject colle ction
        91   Bad: intro duce some  overhead
        92  
        93   Ruby chose  “safety/e asily” app roach
        94   •Ruby enco urage “Hap py Program ming”
        95   •Reduce pr ogrammer’s  cost
        96   •Nowadays  computer i s enough f aster
        97   •Implement ation tech niques ove rcome
        98   performanc e penaltie s
        99  
        100   Do you wan t to progr am without  GC?
        101  
        102   Muilti-thr eads progr amming is  difficult
        103   • Introduc e data rac e, race co ndition
        104  
        105  
        106   • Introduc e deadlock , livelock
        107   • Difficul ty on debu gging beca use of
        108   nondetermi nistic beh avior
        109   difficult  to reprodu ce same pr oblem
        110   Difficult  to make co rrect (bug -free)
        111   programs
        112  
        113  
        114  
        115  
        116  
        117   • Difficul t to tune  performanc e
        118   Difficult  to make
        119   fast progr ams
        120  
        121   Data race  and race c ondition
        122   • Bank amo unt transf er example
        123   Quoted fro m Race Con dition vs.  Data Race  http://bl og.regehr. org/archiv es/490
        124  
        125  
        126  
        127   def transf er1 (amoun t, account _from, acc ount_to) i f (account _from.bala nce < amou nt) return  NOPE acco unt_to.bal ance += am ount accou nt_from.ba lance -= a mount
        128   return YEP
        129   end
        130  
        131   Data race
        132   •“account_ to.balance  += amount ” has Data -race
        133   Assume two  threads ( T1 and T2)  invoke th is methods  with same  bank acco unts
        134  
        135  
        136  
        137  
        138   # interlea ve two thr eads (T1:  amount = 1 00, T2: am ount = 200 ) T1: t1 =  account_t o.balance  # t1 = 10
        139   T2: t2 = a ccount_to. balance #  t2 = 10
        140   T2: accoun t_to.balan ce = t2 +  200 #=> 21 0
        141   T1: accoun t_to.balan ce = t1 +  100 #=> 11 0 (expecte d: 310)
        142  
        143   Race condi tion
        144   • To avoid  data-race  with the  lock
        145   • But ther e is anoth er problem  yet
        146  
        147  
        148   # Lock wit h “Thread. exclusive”
        149   def transf er2 (amoun t, account _from, acc ount_to) i f (account _from.bala nce < amou nt) return  NOPE  Thr ead.exclus ive{ accou nt_to.bala nce += amo unt }
        150   Thread.exc lusive{ ac count_from .balance - = amount }
        151   return YEP  end
        152  
        153   Race condi tion
        154   • To avoid  data-race  with the  lock
        155   • But ther e is anoth er problem  yet
        156  
        157  
        158  
        159   # T1 amoun t = 100, T 2 amount =  200, acco unt_from.b alance = 2 50
        160   T1: if (ac count_from .balance ( == 250) <  100) retur n NOPE # O K, go thro ugh
        161   T2: if (ac count_from .balance ( == 250) <  200) retur n NOPE T2: Thread.exc lusive{ ac count_to.b alance +=  200 }
        162   T2:Thread. exclusive{  account_f rom.balanc e -= 200 }  #=> 250-2 00 => 50
        163   T1:Thread. exclusive{  account_t o.balance  += 100 }
        164   T1:Thread. exclusive{  account_f rom.balanc e -= 100 }  #=> 50 -  100 => neg ative numb er!!
        165  
        166   Final solu tion
        167   • Lock who le of meth od
        168  
        169  
        170  
        171   def transf er1 (amoun t, account _from, acc ount_to)
        172   Thread.exc lusive{
        173   if (accoun t_from.bal ance < amo unt) retur n NOPE acc ount_to.ba lance += a mount acco unt_from.b alance -=  amount
        174   return YEP
        175   }
        176   end
        177  
        178   Another ex ample
        179   Multi-thre ad quiz
        180   • What hap pen on thi s program?
        181  
        182  
        183  
        184   ary = [1,  2, 3]
        185   t1 = Threa d.new{ ary .concat [4 , 5, 6]
        186   }
        187   t2 = Threa d.new{
        188   p ary # wh at ’s happ en? 
        189   }.join
        190  
        191   (1) [1, 2,  3]
        192   (2) [1, 2,  3, 4, 5,  6]
        193   (3) (1) or  (2)
        194  
        195   Another ex ample
        196   Multi-thre ad quiz
        197   • Answer:  (4) depend s on an in terpreter
        198  
        199  
        200  
        201   ary = [1,  2, 3]
        202   t1 = Threa d.new{ ary .concat [4 , 5, 6]
        203   }
        204   t2 = Threa d.new{
        205   p ary # wh at ’s happ en? 
        206   }.join
        207   On MRI, (3 ) is corre ct
        208  
        209   It will sh ows
        210   [1, 2, 3]  or
        211   [1, 2, 3,  4, 5, 6]
        212   (depends o n thread
        213   switching  timing)
        214  
        215   Another ex ample
        216   Multi-thre ad quiz
        217   • Answer:  (4) depend s on an in terpreter
        218  
        219  
        220  
        221   ary = [1,  2, 3]
        222   t1 = Threa d.new{
        223   ary.concat  [4, 5, 6]
        224   }
        225   t2 = Threa d.new{
        226   p ary # wh at ’s happ en?
        227   }.join
        228   On JRuby:
        229  
        230   It can cau se Java ex ception be cause “Arr ay#concat”  is not th read safe
        231  
        232   On JRuby …
        233  
        234  
        235   # similar  program h  = Hash.new (0) NA = 1 _000
        236   10_000.tim es{ ary =  [] (1..10) .each{ Thr ead.new{
        237   NA.times{| i| ary.con cat [i]
        238   }
        239   }
        240   }
        241   t2 = Threa d.new{ s =  ary.dup
        242   }.join
        243   }Unhandled  Java exce ption: jav a.lang.Nul lPointerEx ception
        244  
        245   java.lang. NullPointe rException : null
        246   rbInspect  at org/jru by/RubyBas icObject.j ava:1105 i nspect at  org/jruby/ RubyObject .java:516
        247   inspectAry  at org/jr uby/RubyAr ray.java:1 469 inspec t at org/j ruby/RubyA rray.java: 1497
        248   cacheAndCa ll at org/ jruby/runt ime/callsi te/Caching CallSite.j ava:293 ca ll at org/ jruby/runt ime/callsi te/Caching CallSite.j ava:131
        249   block in t .rb at t.r b:17
        250   yieldDirec t at org/j ruby/runti me/Compile dIRBlockBo dy.java:15 6 yieldSpe cific at o rg/jruby/r untime/IRB lockBody.j ava:73 yie ldSpecific  at org/jr uby/runtim e/Block.ja va:136
        251   times at o rg/jruby/R ubyFixnum. java:291
        252   cacheAndCa ll at org/ jruby/runt ime/callsi te/Caching CallSite.j ava:303 ca llBlock at  org/jruby /runtime/c allsite/Ca chingCallS ite.java:1 41
        253   call at or g/jruby/ru ntime/call site/Cachi ngCallSite .java:145
        254   <top> at t .rb:3
        255   invokeWith Arguments  at java/la ng/invoke/ MethodHand le.java:59 9 load at  org/jruby/ ir/Compile r.java:111
        256   runScript  at org/jru by/Ruby.ja va:833 run Script at  org/jruby/ Ruby.java: 825 runNor mally at o rg/jruby/R uby.java:7 60 runFrom Main at or g/jruby/Ru by.java:57 9 doRunFro mMain at o rg/jruby/M ain.java:4 25 interna lRun at or g/jruby/Ma in.java:31 3
        257   run at org /jruby/Mai n.java:242  main at o rg/jruby/M ain.java:2 04
        258  
        259   jruby 9.1. 2.0 (2.3.0 ) 2016-05- 26 7357c8f  OpenJDK 6 4-Bit Serv er VM 24.9 5-b01 on 1 .7.0_101-b 00 +jit [l inux-x86_6 4]
        260   On 8 hardw are thread s machine
        261  
        262   Difficulty  of multi- threads pr ograms
        263   • We need  to synchro nize all s haring mut able
        264   objects co rrectly
        265   We need to  know whic h methods  are thread -safe.
        266   Easy to tr ack all on  small pro gram
        267   Difficult  to track o n big prog rams, espe cially on
        268   programs u sing gems
        269   • We need  to check a ll of sour ce codes,  or believe
        270   library do cuments (b ut documen ts should  be correct )
        271   • Multi-th reads prog . requires  “complete ness” 
        272  
        273   Difficulty  of multi- threads pr ograms (co nt.)
        274   • For debu gging, it  is difficu lt to find  out the b ugs
        275   Backtrace  may not wo rk well be cause the  problem ma y be place d on anoth er line.
        276   Bugs don’t  appear fr equently w ith small  data
        277   Difficult  to reprodu ce issues  because of
        278   nondetermi nistic beh avior
        279  
        280   FYI:
        281   Why MRI Ar ray#concat  is thread -safe?
        282   • MRI uses  GVL (Gian t/Global V M Lock) to  control t hread swit ching timi ng and C m ethods (su ch as Arra y#concat)  are workin g atomical ly.
        283   • GVL proh ibits para llel threa d executio n (BAD), h owever it  avoids sev eral sever e issues ( GOOD).
        284  
        285   Thread pro gramming:
        286   Performanc e tuning i ssue
        287  
        288  
        289   a1 = []; a 2 = []
        290   NA = 10_00 0_000
        291   t1 = Threa d.new{
        292   NA.times{| i| a1 << i  }
        293   }.join
        294   t2 = Threa d.new{
        295   NA.times{| i| a2 << i  }
        296   }.join
        297   Serial pro gram:
        298   real
        299   user sys
        300   0m8.568s
        301   0m37.816s
        302   0m5.530s
        303   on JRuby
        304  
        305   Thread pro gramming:
        306   Performanc e tuning i ssue
        307  
        308  
        309   a1 = []; a 2 = []
        310   NA = 10_00 0_000
        311   t1 = Threa d.new{ NA. times{|i|  a1 << i }
        312   }
        313   t2 = Threa d.new{ NA. times{|i|  a2 << i }
        314   }
        315   t1.join; t 2.join
        316   Parallel p rogram
        317   (2 threads ):
        318   real
        319   user
        320   sys
        321   0m6.411s
        322   0m20.527s
        323   0m7.798s
        324  
        325   Thread pro gramming:
        326   Performanc e tuning i ssue
        327  
        328  
        329   a1 = []; a 2 = []
        330   NA = 10_00 0_000
        331   m1, m2= Mu tex.new, M utex.new t 1 = Thread .new{
        332   NA.times{| i| m1.sync hronize{ a 1 << i }}
        333   }
        334   t2 = Threa d.new{
        335   NA.times{| i| m2.sync hronize{ a 2 << i }}
        336   }
        337   t1.join; t 2.join
        338   Parallel p rogram wit h
        339   a useless  lock 1
        340   (2 threads ):
        341   real0m10.2 64s
        342   user
        343   sys
        344   0m38.370s
        345   0m4.406s
        346  
        347   Thread pro gramming:
        348   Performanc e tuning i ssue
        349  
        350  
        351   a1 = []; a 2 = []
        352   NA = 10_00 0_000
        353   m = Mutex. new t1 = T hread.new{
        354   NA.times{| i| m.synch ronize{ a1  << i }}
        355   }
        356   t2 = Threa d.new{
        357   NA.times{| i| m.synch ronize{ a2  << i }}
        358   }
        359   t1.join; t 2.join
        360   Parallel p rogram wit h
        361   a useless  lock 2
        362   (2 threads ):
        363   real0m15.1 63s
        364   user
        365   sys
        366   0m45.317s
        367   0m9.658s
        368  
        369   Performanc e tuning i ssue
        370  
        371  
        372  
        373  
        374   Execution  time
        375   Serial pro gram
        376   8.568s
        377   Parallel p rogram
        378   6.411s
        379   Parallel p rogram wit h a
        380   useless lo ck 1
        381   10.264s
        382   Parallel p rogram wit h a useles s lock 2
        383   15.163s
        384  
        385   Thread pro gramming:
        386   Performanc e tuning i ssue
        387  
        388   We need to  use just  correct nu mber locks
        389  
        390   Not enough → unexpect ed behavio r
        391   Too much→  performanc e penalty
        392  
        393   FYI: synch ronization  mechanism
        394   • Many syn chronizati on mechani sms…
        395   Mutual exc lusion (Mu tex), moni tor, criti cal sectio n
        396   Transactio nal memory  (optimist ic lock)
        397   Atomic ins tructions
        398   Synchroniz ed Queue
        399   • …
        400   Research o n many lig htweight l ock algori thms
        401   • They ass ume we can  use them  correctly
        402  
        403  
        404  
        405  
        406  
        407  
        408  
        409  
        410  
        411  
        412  
        413  
        414  
        415  
        416  
        417  
        418  
        419  
        420  
        421  
        422   Overcome t hread diff iculty
        423  
        424   Key idea
        425   Problem:
        426   Easy to sh are mutabl e objects
        427   Idea:
        428   Do not all ow to shar e mutable  objects
        429   without an y restrict ion
        430  
        431   Study from  other lan guages
        432   • Shell sc ript with  pipes, Rac ket (Place )
        433   Copy mutab le data be tween proc esses w/ p ipes
        434   • Erlang/E lixir
        435   Do not all ow mutable  data
        436   • Clojure
        437   Basically  do not all ow mutable  data
        438   Special da ta structu re to shar e mutable  objects
        439   Note that  it can sha re mutable  objects o n Java lay er
        440   NOTE: we d o not list  approache s using “t ype system
        441  
        442  
        443  
        444  
        445  
        446  
        447  
        448  
        449   Don’t you  know Elixi r language ?
        450  
        451  
        452  
        453  
        454  
        455  
        456  
        457  
        458   Programmin g Elixir 1 .2
        459   by Dave Th omas
        460   邦訳:プログラミング Elixir
        461   笹田耕一・鳥井雪共訳  2016/08/1 9
        462   You can bu y it TODAY !!
        463   サイン会は明日13時 らしいです
        464  
        465   Summary of  approache s
        466   Communicat ion with c opied data  (shell sc ripts)
        467   Good: we d on’t need  locks
        468   Bad: copy  everything  is slow
        469   Prohibit m utable obj ects
        470   Good: we d on’t need  locks
        471   Bad: Ruby  utilizes m any “write ” op erati ons . Unac ceptable.
        472   Provide sp ecial data  structure  to share  mutable ob jects
        473   Good: we d on’t need  locks (who  don’t use  such spec ial data
        474   structures )
        475   Bad: Diffi cult to us e special  data struc tures.
        476  
        477  
        478  
        479  
        480  
        481  
        482  
        483  
        484  
        485  
        486  
        487  
        488  
        489  
        490  
        491  
        492  
        493  
        494  
        495  
        496   Background  wasfinish ed
        497  
        498   Our goal f or Ruby 3
        499   • We need  to keep co mpatibilit y with Rub y 2.
        500   • We can m ake parall el program .
        501   • We shoul dn’t consi der about  locks any  more.
        502   • We can s hare objec ts with co py, but co py operati on should  be fast.
        503   • We shoul d share ob jects if w e can.
        504   • We can p rovide spe cial objec ts to shar e mutable  objects li ke Clojure  if we rea lly need s peed.
        505  
        506  
        507  
        508  
        509  
        510  
        511  
        512  
        513  
        514   “Guild”
        515   New concur rency mode l for Ruby  3
        516  
        517   Guild: New  concurren cy abstrac tion
        518   • Guild ha s at least  one threa d (and a t hread has
        519   at least o ne fiber)
        520  
        521  
        522   Guild
        523   Thread
        524   Fiber
        525   Fiber
        526   Thread
        527   Fiber
        528   Guild
        529   ThreaGduil d FibeTrhr ead
        530   Fiber
        531  
        532   Threads in  different  guilds ca n run in
        533   Parallel
        534   Threads in  different  guilds ca n run in p arallel
        535   Threads in  a same gu ild can no t run in p arallel
        536   because of  GVL (or G GL: Giant  Guild Lock )
        537  
        538  
        539  
        540   G1:T1 G1:T 2 G2:T3
        541  
        542  
        543  
        544   Acquire GG L
        545   Acquire GG L
        546  
        547   Guild and  objects:
        548   All object s have the ir ownmemb ership
        549   • All of m utable obj ects shoul d belong t o only one
        550   Guild (all  mutable o bjects are  member of  one guild )
        551   • Other gu ilds can n ot access  objects
        552  
        553  
        554   Guild 1
        555   obj
        556   obj
        557   obj
        558   NG!!
        559   Can’t acce ss
        560   (read/writ e)
        561   Guild 2
        562   obj
        563   obj
        564  
        565   Object mem bership
        566   Only one g uild can a ccess muta ble object
        567   → Wedon’t  needtocons ideraboutl ocks
        568  
        569   Because:
        570   NO data ra ces and NO  race cond itions
        571   (if all gu ilds use o nly one th read)
        572  
        573   Inter guil ds communi cation
        574   •“Guild::C hannel” to  communica te each gu ilds
        575   •Two commu nication m ethods
        576   Copy
        577   Transfer m embership  or Move in  short
        578  
        579   Copy using  Channel
        580   • Guild::C hannel#tra nsfer(obj)  send deep  copied
        581   object(s)  to a desti nation gui ld.
        582   • dRuby an d multi-pr ocess syst em use thi s kind of  communicat ion
        583  
        584   Copy using  Channel
        585  
        586  
        587   channel.tr ansfer(o1) o1 = chann el.receive
        588  
        589  
        590   O2:Data
        591   O2:Data
        592   O3:Data
        593   O3:Data
        594   Guild1
        595   Guild2
        596   o1
        597   channel
        598   o1
        599   o2
        600   o2
        601   o3o3
        602   COPY
        603  
        604   Move using  Channel
        605   [New techn ique!!]
        606   Guild::Cha nnel#trans fer_member ship(obj)      change
        607   the member ship of ob ject(s)
        608   Leave from  the sourc e guild
        609   Join to th e destinat ion guild
        610   Prohibit a ccessing t o left obj ects
        611   Cause exce ptions and  so on
        612   ex)obj = “ foo”
        613   ch.transfe r_membersh ip(obj) ob j.upcase#= > Error!!  p(obj)#=>  Error!!
        614  
        615   Move using  Channel
        616  
        617  
        618   channel.tr ansfer_mem bership(o1 )o1 = chan nel.receiv e
        619  
        620  
        621   O2:Data
        622   O3:Data
        623   Guild1
        624   Guild2
        625   o1
        626   channel
        627   o2
        628   o3
        629   MOVE
        630  
        631   Move using  Channel
        632  
        633  
        634   channel.tr ansfer_mem bership(o1 )o1 = chan nel.receiv e
        635  
        636  
        637   O2:Data
        638   O3:Data
        639   From Guild 1 perspect ive,
        640   transferre d objects  are invali dated
        641   Guild1
        642   Guild2
        643   -
        644   channel
        645   o1
        646   -
        647   o2
        648   -o3
        649   MOVE
        650  
        651   Sharing im mutable ob jects
        652   • Immutabl e objects  can be sha red with a ny
        653   guilds
        654   a1 = [1, 2 , 3].freez e: a1 is I mmutable o bject
        655   a2 = [1, O bject.new,  3].freeze : a2 is no t immutabl e
        656   • We only  need to se nd referen ces
        657   very light weight, li ke thread- programmin g
        658   • Numeric  objects, s ymbols, tr ue, false,  nil are i mmutable(f rom Ruby 2 .0, 2.1, 2 .2)
        659  
        660   Sharing im mutable ob jects
        661   We can sha re referen ce to immu table obje cts
        662   channel.tr ansfer(o1) o1 = chann el.receive
        663  
        664  
        665   If o1 is i mmutable,  any Guild  can read o 1
        666   O2:Data
        667   O3:Data
        668   Guild1
        669   Ref to
        670   o1
        671   read
        672   channel
        673   Guild2
        674   Ref to o1
        675   o1
        676   read
        677   o2
        678   o3
        679  
        680   Use-case 1 : master –  worker ty pe
        681  
        682  
        683  
        684  
        685  
        686  
        687   def fib(n)  ... end
        688   g_fib = Gu ild.new(sc ript: %q{  ch = Guild .default_c hannel
        689   while n, r eturn_ch =  ch.receiv e
        690   return_ch. transfer f ib(n) end
        691   })
        692  
        693   ch = Guild ::Channel. new g_fib. transfer([ 3, ch])
        694   p ch.recei ven, retur n_ch
        695  
        696  
        697   Main
        698   Guild
        699   ch
        700   Fibonacci
        701   Guild
        702   return_ch
        703   Answer of  fib(n)
        704   NOTE: Maki ng other F ibonacci g uilds,
        705   you can co mpute fib( n) in para llel
        706  
        707   Use-case 2 : pipeline
        708  
        709  
        710  
        711  
        712   result_ch  = Guild::C hannel.new  g_pipe3 =  Guild.new (script: % q{
        713   while obj  = Guild.de fault_chan nel.receiv e obj = mo dify_obj3( obj) Guild .argv[0].t ransfer_me mbership(o bj) end
        714   }, argv: [ result_ch] )
        715   g_pipe2 =  Guild.new( script: %q {
        716   while obj  = Guild.de fault_chan nel.receiv e obj = mo dify_obj2( obj) Guild .argv[0].t ransfer_me mbership(o bj) end
        717   }, argv: [ g_pipe3])
        718   g_pipe1 =  Guild.new( script: %q {
        719   while obj  = Guild.de fault_chan nel.receiv e obj = mo dify_obj1( obj) Guild .argv[0].t ransfer_me mbership(o bj) end
        720   }, argv: [ g_pipe2])  obj = Some Class.new
        721   g_pipe1.tr ansfer_mem bership(ob j) obj = r esult_ch.r eceiveMain
        722   Guild
        723   Obj’’’
        724  
        725  
        726   Move
        727   Pipe 3
        728   Guild
        729  
        730  
        731  
        732   Pipe 1
        733   Guild
        734   obj
        735  
        736  
        737  
        738   Move
        739   and modify
        740   Obj’’’
        741   Move
        742   and modify
        743   Pipe 2
        744   Guild
        745  
        746   Obj’
        747  
        748   Move
        749   and modify
        750   Obj’’
        751  
        752   Use-case:
        753   Bank examp le
        754  
        755  
        756  
        757   Only bank  guild main tains bank  data
        758  
        759  
        760  
        761  
        762   g_bank = G uild.new(s cript: %q{
        763   while acco unt_from,  account_to , amount,  ch = Guild .default_c hannel.rec eive
        764   if (Bank[a ccount_fro m].balance  < amount)
        765   ch.transfe r :NOPE
        766   else
        767   Bank[accou nt_to].bal ance += am ount Bank[ account_fr om].balanc e -= amoun t ch.trans fer :YEP
        768   end end
        769   })
        770  
        771   Bank
        772   Guild
        773   requests
        774   Other
        775   guilds
        776   Other
        777   guilds
        778  
        779   Use-case:
        780   Introduce  special da ta structu re
        781  
        782   ??
        783   Other
        784   guilds
        785   Other
        786   guildsIdea s of speci al data st ructure to  share mut able objec ts
        787   Use extern al RDB
        788   In process /external  Key/value  store
        789   Software t ransaction al
        790   memory
        791   • …
        792  
        793   Summary of  use cases
        794   Making mul tiple work ers and co mpute in p arallel
        795   Requests a nd respons es are com municate v ia channel s
        796   You can se nd it with  copy or m ove
        797   Maybe web  applicatio n can empl oy this mo del
        798   Making Pip eline stru ctures and  compute i n parallel
        799   Each task  has own Gu ild
        800   Receive ta rget objec t, modify  it and sen d it next  pipeline
        801   You will s end it wit h move (tr ansfer mem bership)
        802   It will he lp applica tions like  applying  several fi lters for  input data
        803   Own respon sibility b y one Guil d
        804   All access es are man aged by on e responsi ble Guild
        805   If you wan t to share  mutable o bjects, we  need spec ial data s tructures
        806   External R DBs or key /value sto res are al so good id ea for thi s purpose
        807  
        808   Communicat ion strate gy
        809   [Upper is  better]
        810   • Passing  immutable  objects
        811   • Copy mut able objec ts
        812   • If you h ave perfor mance prob lem, move  (transfer  membership ) mutable  objects
        813   • If you h ave perfor mance prob lem too, u se
        814   special da ta structu re to shar e mutable  objects
        815  
        816   Compare be tween
        817   Thread mod el and Gui ld model
        818   • On threa ds, it is  difficult  to find ou t which ob jects
        819   are shared  mutable o bjects
        820   • On Guild s, there a re no shar ed mutable  objects
        821   If there a re special  data stru cture to s hare mutab le
        822   objects, w e only nee d to check  around th is code
        823  
        824   → Encourag e “Safe” a nd “Easy”  programmin g
        825  
        826   Compare be tween
        827   Thread mod el and Gui ld model
        828   On threads , inter th reads comm unication  is very fa st.
        829   On guilds,  inter gui lds commun ication in troduce ov erhead
        830   “Move” (tr ansfer mem bership) t echnique c an reduce
        831   this kind  of overhea ds
        832  
        833   Trade-off:  Performan ce v.s. Sa fety/Easil y
        834   Which do y ou want to  choose?
        835  
        836   Digression : The name  of “Guild
        837   •“Guild” i s good met aphor for  “object’s
        838   membership
        839   • Check du plication
        840   First lett er is not  same as ot her simila r abstract ions
        841   For variab le names
        842   P is for P rocesses,  T is for T hreads, F  is for Fib ers
        843   There are  no duplica ting top-l evel class es and
        844   modules in  all of ru bygems
        845  
        846   Implementa tion of “G uild”
        847   • How to i mplement i nter Guild s communic ation
        848   • How to i solate pro cess globa l data
        849  
        850   Howto impl ement inte r Guilds
        851   communicat ion
        852   • Copy
        853   • Move (tr ansfer mem bership)
        854  
        855   Copy using  Channel
        856  
        857  
        858   channel.tr ansfer(o1) o1 = chann el.receive
        859  
        860  
        861   O2:Data
        862   O2:Data
        863   O3:Data
        864   O3:Data
        865   Guild1
        866   Guild2
        867   o1
        868   channel
        869   o1
        870   o2
        871   o2
        872   o3o3
        873   COPY
        874  
        875   Copy using  Channel
        876   Implementa tion
        877   channel.tr ansfer(o1) o1 = chann el.receive
        878  
        879  
        880   O2:Data
        881   O3:Data
        882   Guild1
        883   (1) Make
        884   deep copy
        885   Guild2
        886   o1o1
        887   channel
        888   o2
        889   o2
        890   o3o3
        891   O2:Data
        892   O3:Data
        893  
        894  
        895  
        896   Copy using  Channel I mplementat ion
        897   We can use  CoW
        898   technique  for data
        899  
        900  
        901   channel.tr ansfer(o1) o1 = chann el.receive
        902  
        903  
        904   O2:Data
        905   O2:Data
        906   O3:Data
        907   Guild1
        908   Guild2
        909   o1
        910   channel
        911   o1
        912   o2
        913   o2
        914   o3o3
        915   O3:Data
        916   (2) Move/J oin
        917  
        918   Move using  Channel
        919  
        920  
        921   channel.tr ansfer_mem bership(o1 )o1 = chan nel.receiv e
        922  
        923  
        924   O2:Data
        925   O3:Data
        926   Guild1
        927   Guild2
        928   o1
        929   channel
        930   o2
        931   o3
        932   MOVE
        933  
        934   Move using  Channel
        935  
        936  
        937   channel.tr ansfer_mem bership(o1 )o1 = chan nel.receiv e
        938  
        939  
        940   O2:Data
        941   O3:Data
        942   From Guild 1 perspect ive,
        943   transferre d objects  are invali dated
        944   Guild1
        945   Guild2
        946   -
        947   channel
        948   o1
        949   -
        950   o2
        951   -o3
        952   MOVE
        953  
        954   Move using  Channel
        955   Implementa tion
        956   channel.tr ansfer_mem bership(o1 )o1 = chan nel.receiv e
        957  
        958  
        959   O2:Data
        960   O3:Data
        961   Guild1
        962   (1) Make
        963   deep copy
        964   Guild2
        965   o-1
        966   (2) Invali date origi nals
        967   o-2
        968   o-3
        969   o1
        970   channel
        971   o2
        972   o3
        973  
        974   Move using  Channel
        975   Implementa tion
        976   channel.tr ansfer_mem bership(o1 )o1 = chan nel.receiv e
        977  
        978  
        979   O2:Data
        980   Guild1
        981   Guild2
        982   o-1
        983   (2) Invali date origi nals
        984   o-2
        985   o-3
        986   channel
        987   o1
        988   o2
        989   o3
        990   (3) Move/J oin
        991   O3:Data
        992  
        993   Ruby globa l data
        994   Global var iables ($f oo)
        995   Change the m to Guild  local var iables
        996   Class and  module obj ects
        997   Share betw een guilds
        998   Class vari ables
        999   Change the m to guild  local. So  that it i s guild/cl ass local  variables
        1000   Constants
        1001   Share betw een guilds
        1002   However if  assigned  object is  not a immu table obje ct, this c onstant is  accessed  only by se tting guil ds. If oth er guilds  try to acc ess it, th em cause e rror.
        1003   Instance v ariables o f class an d module o bjects
        1004   Difficult.  There are  several a pproaches.
        1005   Proc/Bindi ng objects
        1006   Make it co py-able wi th env obj ects or en v independ ent object s
        1007   ObjectSpac e.each_obj ect
        1008   OMG
        1009  
        1010   Interprete r process  global dat a
        1011   GC/Heap
        1012   Share it.  Do stop th e world pa rallel mar king- and  lazy concu rrent swee ping.
        1013   Synchroniz e only at  page acqui re timing.  No any sy nchronizat ion at cre ation time .
        1014   Inline met hod cache
        1015   To fill ne w entry, c reate an i nline cach e object a nd update  atomically .
        1016   Tables (su ch as meth od tables  and consta nt tables)
        1017   Introduce  mutual exc lusions.
        1018   Current wo rking dire ctory (cwd )
        1019   Each guild  should ha ve own cwd  (using op enat and s o on).
        1020   Signal
        1021   Design new  signal de livery pro tocol and  mechanism
        1022   C level gl obal varia bles
        1023   Avoid them .
        1024   Main guild  can use C  extension s depends  on them
        1025   Current th read
        1026   Use TLS (t emporary),  but we wi ll change  all of C A PIs to rec eive conte xt data as  first par ameter in  the future .
        1027  
        1028   Performanc e evaluati on
        1029   • On 2 cor e virtual  machine
        1030   Linux on V irtualBox  on Windows  7
        1031   • Now, we  can’t run  Ruby progr am on othe r than mai n guild, s o other gu ilds are i mplemented  by C code
        1032  
        1033   Performanc e evaluati on
        1034   Simple num eric task  in paralle l
        1035  
        1036  
        1037   Execution  time (sec)
        1038   Single-Gui ld
        1039   19.45
        1040   Multi-Guil d
        1041   10.45
        1042   Fibonacci
        1043  
        1044   Main
        1045   Guild
        1046   FGibuoilnd acci
        1047   FGibuoilnd acci
        1048   GFiubioldn acci Guild
        1049  
        1050  
        1051   Total 50 r equests to  compute f ib(40) Sen d 40 (inte ger) in ea ch request
        1052  
        1053  
        1054  
        1055   Execution
        1056   time (sec)
        1057   Single-Gui ld
        1058   1.00
        1059   Multi/ref
        1060   0.64
        1061   Multi/move
        1062   4.29
        1063   Multi/copy
        1064   5.16
        1065   Performanc e evaluati on
        1066  
        1067   Main
        1068   Guild
        1069   sum
        1070   sum
        1071   sum
        1072   sum GuildC opy/Move
        1073  
        1074  
        1075  
        1076  
        1077  
        1078  
        1079  
        1080  
        1081  
        1082  
        1083  
        1084  
        1085  
        1086  
        1087  
        1088  
        1089  
        1090  
        1091  
        1092  
        1093  
        1094  
        1095  
        1096   Total 100  requests t o compute  sum of arr ay Send (1 ..10_000_0 00).to_a i n each req uest
        1097   Too slow!!
        1098   Because “m ove” need  to
        1099   check all  of element s
        1100  
        1101   Performanc e evaluati on
        1102   Copy/Move
        1103  
        1104  
        1105   Main
        1106   Guild
        1107   sum
        1108   sum
        1109   sum
        1110   sum Guild
        1111  
        1112   Execution  time (sec)
        1113   Single-Gui ld
        1114   1.00
        1115   Multi/ref
        1116   0.64
        1117   Multi/move
        1118   0.64
        1119  
        1120  
        1121   If we know  this arra y only has  immutable  objects,
        1122   we don’t n eed to che ck all ele ments => s pecial dat a structur e
        1123  
        1124   Check our  goal for R uby 3
        1125   We need to  keep comp atibility  with Ruby  2.
        1126   OK: Only i n main gui ld, it is  compatible .
        1127   We can mak e parallel  program.
        1128   OK: Guilds  can run i n parallel .
        1129   We shouldn ’t conside r about lo cks any mo re.
        1130   OK: Only u sing copy  and move,  we don’t n eed to car e locks.
        1131   We can sha re objects  with copy , but copy  operation  should be  fast.
        1132   OK: Move ( transfer m embership)  idea can  reduce ove rhead.
        1133   We should  share obje cts if we  can.
        1134   OK: We can  share imm utable obj ects fast  and easily .
        1135   We can pro vide speci al objects  to share  mutable ob jects like  Clojure i f we reall y need spe ed.
        1136   OK: Yes, w e can prov ide.
        1137  
        1138   Summary
        1139   • Introduc e “why thr eads are v ery diffic ult”
        1140   • Propose  new concur rency abst raction “G uild” for  Ruby 3
        1141   Not implem ented ever ything yet , but I sh ow key ide as and pre liminary e valuation
        1142  
        1143  
        1144  
        1145  
        1146   Thank you  for your a ttention
        1147  
        1148   Koichi Sas ada
        1149   <ko1@herok u.com>
        1150  
        1151  
        1152  
        1153  
        1154  
        1155   Approach c omparison
        1156  
        1157  
        1158  
        1159  
        1160   Process/MV M
        1161   Place (Rac ket)
        1162   Guild (cop y/move)
        1163   Thread
        1164   Heap
        1165   Separate
        1166   Separate
        1167   Share
        1168   Share
        1169   Communicat ion
        1170   Mutable ob jects
        1171   Copy
        1172   Copy
        1173   Copy/Move
        1174   Share
        1175   Communicat ion Frozen  object
        1176   Copy
        1177   Share (may be)
        1178   Share
        1179   Share
        1180   Lock
        1181   Don’t need
        1182   Don’t need
        1183   (mostly) D on’t need
        1184   Required
        1185   ISeq
        1186   Copy
        1187   Share
        1188   Share
        1189   Share
        1190   Class/Modu le
        1191   Copy
        1192   Copy (fork )
        1193   Share
        1194   Share
        1195  
        1196  
        1197  
        1198  
        1199