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

dragon-importsで爆速goimports生活 - プロジェクト Go modules -/dragon-imports

dragon-importsで爆速goimports生活 - プロジェクト Go modules -/dragon-imports

Fukuoka.go#13+Okayama.go #fukuokago
https://fukuokago.connpass.com/event/112073/

monochromegane

February 12, 2019
Tweet

More Decks by monochromegane

Other Decks in Programming

Transcript

  1. - ϓϩδΣΫτ Go modules -
    ࡾ୐༔հ / Pepabo R&D Institute, GMO Pepabo, Inc.
    2019.02.12 Fukuoka.go#13+Okayama.go
    dragon-importsͰ
    ര଎goimportsੜ׆

    View full-size slide

  2. ϓϦϯγύϧΤϯδχΞ
    ࡾ୐༔հ!NPOPDISPNFHBOF
    (.0ϖύϘגࣜձࣾϖύϘݚڀॴ
    IUUQTCMPHNPOPDISPNFHBOFDPN

    View full-size slide

  3. 1. Motivation
    2. Problem of goimports
    3. Approach by dragon-imports
    4. Evalution
    5. Discussion about Go modules
    3
    ໨࣍

    View full-size slide

  4. 1.
    Motivation

    View full-size slide

  5. ͓·͑͸
    ࠓ·ͰΠϯϙʔτͨ͠
    ίʔυͷ਺Λ
    ͓΅͍͑ͯΔͷ͔ʁ

    View full-size slide

  6. • goimports͸importߦͷߋ৽ΛࣗಈͰߦͬͯ͘ΕΔͨΊඇৗʹศར
    • goimports͸։ൃதɼԿ౓΋࣮ߦ͞ΕΔ
    • ୳ࡧର৅ͷύοέʔδ͕૿͑Δͱ࣮ߦ͕஗͘ͳΓੜ࢈ੑ͕Լ͕Δ
    • ಛʹghq+pecoͰϩʔΧϧϦϙδτϦΛ౷Ұ؅ཧ͍ͯ͠Δ৔߹ʹݦஶ
    • ͦ͜Ͱ୳ࡧର৅ͷύοέʔδ͕૿͑ͯ΋࣮ߦ଎౓͕มΘΒͳ͍άϨʔτͳ
    goimports͕ٻΊΒΕΔ
    6
    goimportsͷ௿଎ԽΛղܾ͍ͨ͠

    View full-size slide

  7. 2.
    Problem of goimports

    View full-size slide

  8. • ର৅ͷίʔυΛASTʹม׵͠importߦΛߋ৽ɼ௥Ճɼ࡟আɽͦͷޙɼϑΥʔ
    ϚοτࡁΈίʔυΛग़ྗ͢Δ
    • ୳ࡧର৅ͷύοέʔδʹ͸ඪ४ϥΠϒϥϦͱ֎෦ύοέʔδ͕͋Δ
    • $GOPATH/src, $GOPATH/pkg/mod
    • ର৅ͷೖྗதͷύοέʔδ໊ɼγϯϘϧ໊ͷ૊Έ߹ΘͤΛશͯຬͨ͢Πϯϙʔ
    τύεΛղܾ͢Δ
    • math/rand (import path), rand (package name), Int (symbol)
    8
    goimportsͷجຊ

    View full-size slide

  9. 9
    goimportsͷ࣮ߦྫ
    $ cat imp.txt
    package imp
    func hoge() {
    fmt.Println("hello.")
    kaburaya.Recorder
    }
    $ goimports imp.txt
    package imp
    import (
    "fmt"
    "github.com/monochromegane/kaburaya"
    )
    func hoge() {
    fmt.Println("hello.")
    kaburaya.Recorder
    }

    View full-size slide

  10. • ΑΓਖ਼͘͠͸୳ࡧର৅ͱͳΔʮ֎෦ʯύοέʔδ͕૿Ճͨ͠৔߹ʹ஗͘ͳΔ
    • ΫϥΠΞϯτ؀ڥ΍࣌ؒܦաʹΑͬͯ֎෦ύοέʔδҰཡ͸มԽ͢Δ͜ͱ͔Β౎౓ɼ୳ࡧ
    ର৅ͷσΟϨΫτϦ୳ࡧɼߏจղੳɼϦϑΝϨϯεͷநग़Λߦ͏ඞཁ͕͋ΔͨΊɽ
    • ൓ରʹɼύοέʔδҰཡ͕؀ڥʹΑͬͯมԽ͠ͳ͍ඪ४ύοέʔδ͸Πϯϙʔ
    τύεɼύοέʔδ໊ɼγϯϘϧͷϦετΛࣄલʹղੳ͞Ε͍ͯΔ
    10
    ͳͥgoimports͕஗͘ͳΔͷ͔

    View full-size slide

  11. goimportsඪ४ύοέʔδ
    11
    package imports
    var stdlib = map[string]map[string]bool{
    "archive/tar": map[string]bool{
    "ErrFieldTooLong": true,
    "ErrHeader": true,
    "ErrWriteAfterClose": true,
    "ErrWriteTooLong": true,
    $GOROOT/api/go1.*txt
    golang.org/x/tools/imports/mkstdlib.go
    golang.org/x/tools/imports/zstdlib.go
    golang.org/x/tools/cmd/goimports.go#main
    golang.org/x/tools/imports/imports.go#Process
    golang.org/x/tools/imports/fix.go#addStdlibCandidates
    pkg crypto/rsa, method (*PrivateKey) Size() int
    pkg crypto/rsa, method (*PublicKey) Size() int

    View full-size slide

  12. • First pass (Πϯϙʔτ͕ඞཁ͔ௐ΂Δ)
    • ର৅಺ͷϦϑΝϨϯεҰཡͱimportߦ͕߹க͢Δ͔֬ೝ
    • Second pass (ඪ४ύοέʔδ·ͨ͸طimportߦ͔Βղܾ)
    • var stdlib·ͨ͸ଞϑΝΠϧͰimportࡁΈͷߦΛީิͱͯ͠߹க͢Δ͔֬ೝ
    • Third pass (֎෦ύοέʔδ͔Βղܾ)
    • $GOPATH/src΋͘͠͸$GOPATH/pkg/mod͔ΒϦϑΝϨϯεʹࣅͨσΟϨΫτϦΛߜΓࠐ
    ΈɼީิϑΝΠϧΛղੳͯ֬͠ೝ
    12
    goimportsͷߴ଎Խͷ޻෉

    View full-size slide

  13. 13
    ஈ֊͝ͱͷ࣮ߦ଎౓
    TUQBTT ЖT
    OEQBTT ЖT
    SEQBTT ЖT

    Πϯϙʔτෆཁ
    ඪ४ϥΠϒϥϦͷΤΫεϙʔτΛࢀর
    ଞϑΝΠϧͷΠϯϙʔτΛࢀর
    ֎෦ύοέʔδͷΤΫεϙʔτΛࢀর
    $ tree
    !"" imp.txt
    !"" other.go
    #"" other2.go
    $ find . -type d | wc -l
    120840
    Original goimports

    View full-size slide

  14. 3.
    Approach
    by dragon-imports

    View full-size slide

  15. • ΫϥΠΞϯτ؀ڥ͝ͱʹ͸୳ࡧର৅ͷύοέʔδҰཡ͸ਓؒ࣌ؒͰ͔͠มԽ͠
    ͳ͍
    • ࣄલղੳͷ݁Ռ͕ݱঢ়ʹԊΘͳ͚Ε͹શ୳ࡧΛߦ͏ϑΥʔϧόοΫ͕Մೳ
    • ͞Βʹ͸ΠϯϙʔτߦʹޡΓ͕͋ͬͯ΋ϏϧυͰࣦഊ͢Δ
    • ͜ΕΒͷલఏ͔Β֤ΫϥΠΞϯτ؀ڥ͝ͱʹ୳ࡧର৅ͷύοέʔδΛ೚ҙͷλ
    ΠϛϯάͰࣄલղੳ͓ͯ͘͜͠ͱͰgoimportsͷߴ଎Խ͕Մೳ
    15
    ఏҊख๏ͷண؟఺

    View full-size slide

  16. • monochromegane/dragon-imports
    • A tool for speedup go imports command
    • https://github.com/monochromegane/dragon-imports
    • ඪ४ύοέʔδͱ$GOPATH/srcΛղੳ͠ɼzstdlib.goΛ࠶ੜ੒ɼgoimportsͷ
    ࠶Πϯετʔϧ·Ͱߦ͏
    • zstdlib.go(=var stdlib)ͷ෼཭౓ͷߴ͞ʹΑΓ௥ैੑ΍൚༻ੑ͕ߴ·Δ
    16
    ఏҊख๏ͷ࣮૷
    $ go get github.com/monochromegane/dragon-imports/...
    $ dragon-imports

    View full-size slide

  17. dragon-imports
    17
    package imports
    var stdlib = map[string]map[string]bool{
    "archive/tar": map[string]bool{
    "ErrFieldTooLong": true,
    "ErrHeader": true,
    "ErrWriteAfterClose": true,
    "ErrWriteTooLong": true,
    $GOROOT/api/go1.*txt
    dragon-imports
    golang.org/x/tools/imports/zstdlib.go
    golang.org/x/tools/cmd/goimports.go#main
    golang.org/x/tools/imports/imports.go#Process
    golang.org/x/tools/imports/fix.go#addStdlibCandidates
    $GOPATH/**/*.go

    View full-size slide

  18. 19
    ࣮ߦ࣌ؒͷൺֱ
    TUQBTT ЖT
    OEQBTT ЖT
    SEQBTT ЖT

    Πϯϙʔτෆཁ
    ඪ४ϥΠϒϥϦͷΤΫεϙʔτΛࢀর
    ଞϑΝΠϧͷΠϯϙʔτΛࢀর
    ֎෦ύοέʔδͷΤΫεϙʔτΛࢀর
    Original goimports
    TUQBTT ЖT
    OEQBTT ЖT
    SEQBTT ЖT

    Πϯϙʔτෆཁ
    ඪ४ϥΠϒϥϦͷΤΫεϙʔτΛࢀর
    ଞϑΝΠϧͷΠϯϙʔτΛࢀর
    ֎෦ύοέʔδͷΤΫεϙʔτΛࢀর
    Dragonize goimports

    View full-size slide

  19. 5.
    Discussion
    about Go modules

    View full-size slide

  20. • Go modulesΛ༗ޮʹ͢Δͱ$GOPATH/srcͰ͸ͳ͘$GOPATH/pkg/mod഑Լ
    ͷόʔδϣχϯά؅ཧ͞Εͨύοέʔδ͕ར༻͞ΕΔ
    • goimportsͰ΋୳ࡧର৅Λಉ͡Α͏ʹ੾Γସ͍͑ͯΔ
    • ֎෦ύοέʔδʹόʔδϣχϯάͷ֓೦͕ಋೖ͞ΕΔͨΊɼಉҰΠϯϙʔτύ
    εͰ͋ͬͯ΋γϯϘϧϦετ͕ҟͳΔՄೳੑ͕͋Δʢͨͩ͠ϝδϟʔόʔδϣ
    ϯ͕ҟͳΔͱ/v2ͷΑ͏ʹผͱͯ͠ѻ͑Δʣ
    • ඪ४ύοέʔδ͸όʔδϣχϯάͷ֓೦͕·ͩͳ͍ͨΊzstdlib.go͸ෳ਺όʔ
    δϣϯͷγϯϘϧΛѻ͏ߏ଄ʹͳ͍ͬͯͳ͍
    21
    Go modulesΛͲ͏ѻ͏͔

    View full-size slide

  21. • Go modules؅ཧͷ֎෦ύοέʔδΛࣄલղੳ͢Δ
    • ΦϓγϣϯʹΑͬͯղੳର৅ΛGOPATH/pkg/modʹมߋ͢ΔɽGO111MODULES=autoͷ
    ڍಈ͸ա౉ظͰ͋Δ͜ͱɼzstdlib.go͸WorkingDirΛ஌Εͳ͍͜ͱ͔ΒରԠ͠ͳ͍
    • ֎෦ύοέʔδ͸֤ϝδϟʔόʔδϣϯ಺ͷ࠷৽ͷΈΛࣄલղੳ͢Δ
    • ϚΠφʔόʔδϣϯҎԼͷෳ਺όʔδϣϯ͕ଘࡏ͢Δ৔߹ɼbreaking change͸ͳ͍લఏͰ
    ඪ४ύοέʔδͱಉ༷ʹ࠷৽ͷΈରԠɽsemverະରԠͳ֎෦ύοέʔδʹ͓͍ͯ΋ຊํ਑
    ʹΑͬͯޡ൑ఆ๷ࢭɼ࠷৽Խ΁ͷಈػ෇͚ͱ͢Δ
    • vendoring͸ࣄલղੳ͠ͳ͍
    • ͦ΋ͦ΋ڞ௨Խ͞Εͨύοέʔδ܈͕૿େ͢Δ͜ͱͰҾ͖ى͜͞ΕΔ஗ԆͰ͋ΓɼϓϩδΣ
    Ϋτ͝ͱͷ࠷খͷύοέʔδ୳ࡧʹΑΔ஗Ԇ͸ߟ͑ʹ͍ͨ͘Ί 22
    Go modulesΛͲ͏ѻ͏͔

    View full-size slide

  22. 23
    dragon-imports
    $ go get -u github.com/monochromegane/dragon-imports/...
    $ dragon-imports --gomodules
    • gomodulesΦϓγϣϯΛ࣮ݧతʹఏڙ
    • Go1.12ͷਖ਼ࣜαϙʔτͷঢ়گΛݟͯΦϓγϣϯॳظ஋Λtrueͱ͢Δ༧ఆ
    • ܅΋ࠓ͙͢go get -u !!!

    View full-size slide

  23. • Go modulesͰ͸v2Ҏ߱ͷϝδϟʔόʔδϣϯ͝ͱʹΠϯϙʔτύε͕ҟͳΔ
    • ͔͠͠ύοέʔδύε͸ϝδϟʔόʔδϣϯΛؚ·ͳ͍
    24
    v2ͱ͍͏ࢗ٬
    $ tree
    !"" hellomod
    $ #"" [email protected]
    #"" [email protected]
    package main
    import "github.com/donvito/hellomod/v2"
    func main() {
    hellomod.SayHello("Melvin")
    }
    • ඪ४ϥΠϒϥϦ͸όʔδϣϯΛߟྀ͠ͳ͍ͨΊ “v2”ͱ“hellomod”Λൺֱ͞Ε
    ͯ͠·͍ɼ֎෦ύοέʔδ୳ࡧ͕࢝·ͬͯ͠·͏……………………………!!!

    View full-size slide

  24. • dragon-importsͷ࢓૊ΈΛ঺հͨ͠
    • UsefulͳΕͲGo modules࣌୅ʹ͸׬શରԠ͸ݫͦ͠͏
    • ७ਮʹύοέʔδ͚ͩͷσΟϨΫτϦͱͳΔͷͰ஗Ԇ΋ൃੜ͠ͳ͍ͱࢥΘΕ
    Δɽ͠͹Β͘ӡ༻ͯ͠ࠔͬͨΒdragon-import.v2Λߟ͑Δɽ
    • Go modules࣌୅ͷศརπʔϧ΋࡞͍ͬͯ͜͏
    • ͱΓ͋͑ͣGo modulesʹدͤΔʹ౰ͨͬͯҰཡΛऔಘ͢ΔͨΊͷ

    ’mole(౔ཽ)’Λ࡞ͬͯΔ https://github.com/monochromegane/mole
    26
    ·ͱΊ

    View full-size slide