せっかくなのでYokohama.pmでGoとPerlの話

9480e27b8d6ae2a668a98144516b5513?s=47 acidlemon
November 28, 2014

 せっかくなのでYokohama.pmでGoとPerlの話

Yokohama.pm #12 スライド

9480e27b8d6ae2a668a98144516b5513?s=128

acidlemon

November 28, 2014
Tweet

Transcript

  1. ͔ͤͬ͘ͳͷͰ :PLPIBNBQNͰ (Pͱ1FSMͷ࿩  :PLPIBNBQN !BDJEMFNPO

  2. ࣗݾ঺հ w ɹɹɹ!BDJEMFNPO w ໘ന๏ਓΧϠοΫ
 ιʔγϟϧήʔϜࣄۀ෦νʔϑΤϯδχΞ ࣗশ  w $

    ೥+BWB೥1FSM೥(P൒೥
 ͋ͱ0CKFDUJWF$$ ͱ͔ w *46$0/ഊୀɺग़୊ɺഊୀ w Ίͬͨʹ֎෦Ͱొஃ͠·ͤΜ ͻ͖͜΋Γ 
 ࠓճ͸ձ৔͕ࣗࣾʹདྷͨͷͰొஃͨ͠
  3. ຊ೔ͷ͓୊͸

  4. (P͔Β1FSMΛ࢖͏

  5. (P࣌୅͕ນΛ։͚ͨ w ฐࣾ͸ࠓ೥ͷ݄ʹ(PͰߦ͜͏એݴΛ͠·ͨ͠
 ޿ใͷӄ๳Ͱ֎෦ʹ΋ެ։͞Εͯ͠·ͬͨࣾ಺޲͚ͷ
 ϝʔϧ͸ࢲ͕ॻ͖·ͨ͠  w ͱ͍͏͜ͱͰ͜͜൒೥(Pॻ͍ͯͨΜ͚ͩͲɺ
 1FSMͷ΄͏ָ͕ͩͳ͊ͱࢥ͏͜ͱ΋͠͹͠͹ w

    ͱ͍͏͔*46$0/͸1FSMͰग़·ͨ͠
  6. (P͔ΒผͷݴޠΛ࢖͏ w $$ ͔Β-VBΛ࢖͏Α͏ʹɺ(P͔ΒͳΜ͔
 ผͷݴޠΛ࢖ͬͯΈ͍ͨ w -VB͸εΫϦϓτݴޠ࠷଎ͩ͠ɺ(P͔Β
 -VBΛ࢖ͬͯΈΑ͏

  7. ͦ͏͍͑͹ ࠓ೔1FSMͷΠϕϯτͩͬͨ

  8. (P͔ΒผͷݴޠΛ࢖͏ w $$ ͔Β-VBΛ࢖͏Α͏ʹɺ(P͔ΒͳΜ͔
 ผͷݴޠΛ࢖ͬͯΈ͍ͨ w 1FSM͸ࣗ෼͕ॻ͖׳Ε͍ͯΔݴޠͩ͠ɺ(P͔Β 1FSMΛ࢖ͬͯΈΑ͏

  9. Ͱ΋Ͳ͏΍ͬͯ

  10. FNCFEEFE1FSM

  11. FNCFEEFE1FSMͱ͸ w QFSMEPDQFSMFNCFE w $ϓϩάϥϜ͔Β1FSMΠϯλϓϦλΛར༻͢Δ
 ͨΊͷ$ϥΠϒϥϦ w ࣮༻ྫ
 BQBDIFNPE@QFSM 


    OHJOYOHY@IUUQ@QFSM@NPEVMF 
 OBHJPTFNCFEEFE1FSM/BHJPTͳͲ
  12. ࢓૊Έ w (P͸DHPΛ༻͍ͯ$ͷؔ਺Λݺ΂·͢ w 1FSM͸FNCFEEFE1FSMΛ༻͍ͯ$͔Βݺ΂·͢ w (P͔Β$Λܦ༝ͯ͠1FSMΛ࢖͑Δʂʂʂ ͸ͣ

  13. ΍ͬͯΈ·͠ΐ͏

  14. ·ͣ͸$͔Β1FSM w ΅͘FNCFEEFE1FSM࢖ͬͨ͜ͱͳ͍ΜͰͨ͠
 $͔Β1FSMΛݺͿͷΛ࠷ॳʹ΍Γ·͢ w ΍Γ͔ͨQFSMEPDʹ৐ͬͯΔͷͰίϐϖ͢Δ

  15. ӳޠΑΊͳ͍ ͱ͍͏͋ͳͨʹ΋

  16. QFSMEPDKQ͸ਆ ೔ຊޠͰಡΊ·͢ Α͓͘ੈ࿩ʹͳ͍ͬͯ·͢

  17. ࣮ࡍʹ$Ͱॻ͍ͯΈͨ w IUUQTHJTUHJUIVCDPNBDJEMFNPO ECEG w ୹ॖ63-IUUQHJUJPC2S-H w ࣮ߦํ๏͸HJTUͷίϝϯτΛࢀর͍ͩ͘͞

  18. 5JQT w ຐ๏ͷίϚϯυ
 QFSM.&YU6UJMT&NCFEFDDPQUTFMEPQUT w HDDͰ࢖͏ΠϯΫϧʔυϥΠϒϥϦύεͷίϯ ύΠϧΦϓγϣϯΛग़ྗͯ͘͠ΕΔ DMBOH΋0,  w

    QFSMEPDʹॻ͍ͯ͋Γ·͢
  19. ͜ΕΛ;·͑ͯ

  20. (PͰॻ͍ͯΈΔ w (Pʹ͸DHPͱ͍͏࢓૊Έ͕͋Δɻ
 (PͷϑΝΠϧͷ࠷ॳʹ$ͷίʔυΛίϝϯτͰ ॻ͘ͱΑ͠ͳʹऔΓࠐΜͰ͘ΕΔ package main /* #include <stdio.h>

    const char* MyWorldName() { return "Go and C"; } */ import "C" import "fmt" func main() { fmt.Println("Hello!", C.GoString(C.MyWorldName()), "World!") }
  21. (PͰॻ͍ͯΈͨ w IUUQTHJTUHJUIVCDPNBDJEMFNPO FBBFEFDC w ୹ॖ63-IUUQHJUJPFNCQFSMHP w ࣮ߦํ๏͸HJTUͷίϝϯτΛࢀর͍ͩ͘͞

  22. (PͰॻ͍ͯΈͨ $ݴޠ (Pݴޠ $ݴޠߦ (Pݴޠߦ

  23. ͏ʔΜʜ ·͊͜͏͍͏΋ΜͩͶ

  24. ͔ͤͬ͘ͳͷͰฒྻ࣮ߦ func main() { C.init_perl() // Perl準備 wg := sync.WaitGroup{}

    // goroutine待ち合わせの準備 // 並列列実⾏行行するルーチン test_func := func() { defer wg.Done() // Perlの⽂文を評価してスカラ値を取得 sval := C.Perl_eval_pv(C.CString("my $powawa = 3; $powawa *= 3"), 1) fmt.Printf("powawa = %v\n", C.Sv2Iv(sval)) } // 2並列列で実⾏行行して結果を待つ wg.Add(2) go test_func() go test_func() wg.Wait() C.term_perl() }
  25. ࣮ߦ݁Ռ w ཧ૝ w ݱ࣮ powawa = 9 powawa =

    9 fatal error: unexpected signal during runtime execution [signal 0xb code=0x1 addr=0x262d2750 pc=0x40890bc] runtime stack: runtime: unexpected return pc for runtime.sigpanic called from 0x40890bc runtime.throw(0x426ca16) /Users/kawazoel/.homebrew/Cellar/go/1.3.1/libexec/src/pkg/runtime/ panic.c:520 +0x69 runtime: unexpected return pc for runtime.sigpanic called from 0x40890bc runtime.sigpanic() /Users/kawazoel/.homebrew/Cellar/go/1.3.1/libexec/src/pkg/runtime/ os_darwin.c:439 +0x3d
  26. --%#ͰݪҼΛ֬ೝ w ຖճࢮ͵৔ॴ͕มΘΔ w ͭͷΠϯλϓϦλͰಉ࣮࣌ߦͨ͠ͷͰΠϯλϓϦλ಺ͷ
 εςʔτΛഁյͨͬ͠Ά͍งғؾΛײ͡Δ ͬͯͨ͠ (lldb) run Process

    11751 launched: '/Users/kawazoel/letsgo_perl/golang/parallel' (x86_64) Process 11751 stopped * thread #1: tid = 0xb7a9af, 0x0000000004084fd2 parallel`Perl_op_clear(o=0x0000000005100350) + 498 at op.c:625, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT) frame #0: 0x0000000004084fd2 parallel`Perl_op_clear(o=0x0000000005100350) + 498 at op.c:625 622 allowed to raise and lower references during global destruction, 623 so any *valid* code that happens to do this during global 624 destruction might well trigger premature cleanup. */ -> 625 bool still_valid = gv && SvREFCNT(gv); 626 627 if (still_valid) 628 SvREFCNT_inc_simple_void(gv);
  27. ࣍ͷҰख w ΠϯλϓϦλݸ͕μϝͳΒͨ͘͞Μ࡞Ε͹͍͍ w ͦͷͨΊʹ͸1FSMΛϏϧυ͢Δͱ͖ͷ
 ΦϓγϣϯΛ͍͡Δඞཁ͕͋Δ QFSMEPDࢀর VTFUISFBETVTFJUISFBET

  28. QFSMͰUISFBEΛ࢖͏࣌ͷ Πϝʔδ w ϞώΧϯ͕དྷΔ
 1FSMۦ͚ग़͠ͷࠒʹϞώΧϯʹʮ1FSMͰ
 εϨουͱ͔ߟ͑ͪΌμϝ͔ͩΒʯͬͯ
 ڴ͞ΕͨهԱ͕ોΓ·ͨ͠ 


  29. ࣮ࡍʹ΍ͬͯΈΔ w ͍͖ͳΓίϯύΠϧ͕௨Βͳ͍ w ௐ΂ͯΈΔͱɺ1FSMͷ$ؔ਺͸VTFUISFBET͢Δͱ͖ͱ
 ͠ͳ͍ͱ͖Ͱؔ਺ͷએݴ͕มΘΔ w VTFUISFBET͢Δͱ΄΅શ෦ୈҰҾ਺ʹ1FSM*OUFSQSFUFSͷ
 ϙΠϯλΛͱΔˠ౉͢ඞཁ͕ w

    ࢓ํͳ͍ͷͰΤϥʔݟͳ͕Β͕Μ͹ͬͯ௚͢ In file included from ./parallel.go:4:0: ./parallel.go: In function 'init_perl': /usr/lib64/perl5/CORE/perl.h:189:16: error: 'my_perl' undeclared (first use in this function) # define aTHX my_perl
  30. ࣮ߦͯ͠ΈΑ͏ w IUUQTHJTUHJUIVCDPNBDJEMFNPO GDGDCEE w ୹ॖ63-IUUQHJUJPFNCQFSMHPSPVUJOF w ࣮ߦํ๏͸HJTUͷίϝϯτΛࢀর͍ͩ͘͞

  31. ϥΠϒϥϦԽ΁ͷಓ w Α͠΍ΔͧͱࢥͬͯHJUIVCΛ୳ͨ͠Β͋ͬͨ w HJUIVCDPNCSBEpU[DBNQIFS
 $BNFM ϥΫμ  (PQIFS (PͷϚείοτ

    DBNQIFSΒ͍͠ w (PPHMFͷਓ͔ͩͯ༗໊ͳਓ΍ͳ͍͔ w ιʔεͷίϝϯτʹ஫໨
 1BUDIFTXFMDPNF PSZPVDBOUBLFPWFSNBJOUBJOFSTIJQUPP 
 ͩͦ͏Ͱ͢
  32. ·ͱΊ w (P͔Β1FSMΛݺͿ͜ͱ͸ɺͰ͖·͢ w HPSPVUJOFͰฒྻ࣮ߦ͍ͨ͠ͱ͖͸ɺ
 ΠϯλϓϦλΛͨ͘͞Μ༻ҙ͠·͠ΐ͏ w ୭͔࢖͍Ͳ͜Ζ͕ݟ͔ͭͬͨΒڭ͍͑ͯͩ͘͞

  33. ࣍ճ༧ࠂ

  34. ࣍߸༧ࠂ w ࠓճ(Pº1FSMͰͨ͠ w ࣍ճ߈Ίͱड͚Λٯʹͯ͠ɺ1FSMº(P (P FNCFEEFE1FSM 1FSM (P ͨͿΜͰ͖ΔΜ͡Όͳ͍Ͱ͔͢Ͷ͐ʙ

  35. ͋Γ͕ͱ͏ ͍͟͝·ͨ͠