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

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

acidlemon
November 28, 2014

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

Yokohama.pm #12 スライド

acidlemon

November 28, 2014
Tweet

More Decks by acidlemon

Other Decks in Technology

Transcript

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

    :PLPIBNBQN
    !BDJEMFNPO

    View full-size slide

  2. ࣗݾ঺հ
    w ɹɹɹ!BDJEMFNPO
    w ໘ന๏ਓΧϠοΫ

    ιʔγϟϧήʔϜࣄۀ෦νʔϑΤϯδχΞ ࣗশ

    w $೥+BWB೥1FSM೥(P൒೥

    ͋ͱ0CKFDUJWF$$ͱ͔
    w *46$0/ഊୀɺग़୊ɺഊୀ
    w Ίͬͨʹ֎෦Ͱొஃ͠·ͤΜ ͻ͖͜΋Γ

    ࠓճ͸ձ৔͕ࣗࣾʹདྷͨͷͰొஃͨ͠

    View full-size slide

  3. ຊ೔ͷ͓୊͸

    View full-size slide

  4. (P͔Β1FSMΛ࢖͏

    View full-size slide

  5. (P࣌୅͕ນΛ։͚ͨ
    w ฐࣾ͸ࠓ೥ͷ݄ʹ(PͰߦ͜͏એݴΛ͠·ͨ͠

    ޿ใͷӄ๳Ͱ֎෦ʹ΋ެ։͞Εͯ͠·ͬͨࣾ಺޲͚ͷ

    ϝʔϧ͸ࢲ͕ॻ͖·ͨ͠

    w ͱ͍͏͜ͱͰ͜͜൒೥(Pॻ͍ͯͨΜ͚ͩͲɺ

    1FSMͷ΄͏ָ͕ͩͳ͊ͱࢥ͏͜ͱ΋͠͹͠͹
    w ͱ͍͏͔*46$0/͸1FSMͰग़·ͨ͠

    View full-size slide

  6. (P͔ΒผͷݴޠΛ࢖͏
    w $$͔Β-VBΛ࢖͏Α͏ʹɺ(P͔ΒͳΜ͔

    ผͷݴޠΛ࢖ͬͯΈ͍ͨ
    w -VB͸εΫϦϓτݴޠ࠷଎ͩ͠ɺ(P͔Β

    -VBΛ࢖ͬͯΈΑ͏

    View full-size slide

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

    View full-size slide

  8. (P͔ΒผͷݴޠΛ࢖͏
    w $$͔Β-VBΛ࢖͏Α͏ʹɺ(P͔ΒͳΜ͔

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

    View full-size slide

  9. Ͱ΋Ͳ͏΍ͬͯ

    View full-size slide

  10. FNCFEEFE1FSM

    View full-size slide

  11. FNCFEEFE1FSMͱ͸
    w QFSMEPDQFSMFNCFE
    w $ϓϩάϥϜ͔Β1FSMΠϯλϓϦλΛར༻͢Δ

    ͨΊͷ$ϥΠϒϥϦ
    w ࣮༻ྫ

    BQBDIFNPE@QFSM 

    OHJOYOHY@IUUQ@QFSM@NPEVMF 

    OBHJPTFNCFEEFE1FSM/BHJPTͳͲ

    View full-size slide

  12. ࢓૊Έ
    w (P͸DHPΛ༻͍ͯ$ͷؔ਺Λݺ΂·͢
    w 1FSM͸FNCFEEFE1FSMΛ༻͍ͯ$͔Βݺ΂·͢
    w (P͔Β$Λܦ༝ͯ͠1FSMΛ࢖͑Δʂʂʂ ͸ͣ

    View full-size slide

  13. ΍ͬͯΈ·͠ΐ͏

    View full-size slide

  14. ·ͣ͸$͔Β1FSM
    w ΅͘FNCFEEFE1FSM࢖ͬͨ͜ͱͳ͍ΜͰͨ͠

    $͔Β1FSMΛݺͿͷΛ࠷ॳʹ΍Γ·͢
    w ΍Γ͔ͨQFSMEPDʹ৐ͬͯΔͷͰίϐϖ͢Δ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  18. 5JQT
    w ຐ๏ͷίϚϯυ

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

    w QFSMEPDʹॻ͍ͯ͋Γ·͢

    View full-size slide

  19. ͜ΕΛ;·͑ͯ

    View full-size slide

  20. (PͰॻ͍ͯΈΔ
    w (Pʹ͸DHPͱ͍͏࢓૊Έ͕͋Δɻ

    (PͷϑΝΠϧͷ࠷ॳʹ$ͷίʔυΛίϝϯτͰ
    ॻ͘ͱΑ͠ͳʹऔΓࠐΜͰ͘ΕΔ
    package main
    /*
    #include
    const char* MyWorldName() {
    return "Go and C";
    }
    */
    import "C"
    import "fmt"
    func main() {
    fmt.Println("Hello!", C.GoString(C.MyWorldName()), "World!")
    }

    View full-size slide

  21. (PͰॻ͍ͯΈͨ
    w IUUQTHJTUHJUIVCDPNBDJEMFNPO
    FBBFEFDC
    w ୹ॖ63-IUUQHJUJPFNCQFSMHP
    w ࣮ߦํ๏͸HJTUͷίϝϯτΛࢀর͍ͩ͘͞

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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()
    }

    View full-size slide

  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

    View full-size slide

  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);

    View full-size slide

  27. ࣍ͷҰख
    w ΠϯλϓϦλݸ͕μϝͳΒͨ͘͞Μ࡞Ε͹͍͍
    w ͦͷͨΊʹ͸1FSMΛϏϧυ͢Δͱ͖ͷ

    ΦϓγϣϯΛ͍͡Δඞཁ͕͋Δ QFSMEPDࢀর

    VTFUISFBETVTFJUISFBET

    View full-size slide

  28. QFSMͰUISFBEΛ࢖͏࣌ͷ
    Πϝʔδ
    w ϞώΧϯ͕དྷΔ

    1FSMۦ͚ग़͠ͷࠒʹϞώΧϯʹʮ1FSMͰ

    εϨουͱ͔ߟ͑ͪΌμϝ͔ͩΒʯͬͯ

    ڴ͞ΕͨهԱ͕ોΓ·ͨ͠

    View full-size slide

  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

    View full-size slide

  30. ࣮ߦͯ͠ΈΑ͏
    w IUUQTHJTUHJUIVCDPNBDJEMFNPO
    GDGDCEE
    w ୹ॖ63-IUUQHJUJPFNCQFSMHPSPVUJOF
    w ࣮ߦํ๏͸HJTUͷίϝϯτΛࢀর͍ͩ͘͞

    View full-size slide

  31. ϥΠϒϥϦԽ΁ͷಓ
    w Α͠΍ΔͧͱࢥͬͯHJUIVCΛ୳ͨ͠Β͋ͬͨ
    w HJUIVCDPNCSBEpU[DBNQIFS

    $BNFM ϥΫμ
    (PQIFS (PͷϚείοτ
    DBNQIFSΒ͍͠
    w (PPHMFͷਓ͔ͩͯ༗໊ͳਓ΍ͳ͍͔
    w ιʔεͷίϝϯτʹ஫໨

    1BUDIFTXFMDPNF PSZPVDBOUBLFPWFSNBJOUBJOFSTIJQUPP


    ͩͦ͏Ͱ͢

    View full-size slide

  32. ·ͱΊ
    w (P͔Β1FSMΛݺͿ͜ͱ͸ɺͰ͖·͢
    w HPSPVUJOFͰฒྻ࣮ߦ͍ͨ͠ͱ͖͸ɺ

    ΠϯλϓϦλΛͨ͘͞Μ༻ҙ͠·͠ΐ͏
    w ୭͔࢖͍Ͳ͜Ζ͕ݟ͔ͭͬͨΒڭ͍͑ͯͩ͘͞

    View full-size slide

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

    View full-size slide

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

    View full-size slide