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

goのメモリアロケーターの話

nasa
June 29, 2022

 goのメモリアロケーターの話

https://bitkey.connpass.com/event/248608で10分LTしたやつ
Goのメモリアロケーターの構造について軽く話した。

nasa

June 29, 2022
Tweet

More Decks by nasa

Other Decks in Technology

Transcript

  1. ©2022 Wantedly, Inc. ࣗݾ঺հ • nasa Ͱ͢ • Wantedly, inc.

    ৽ଔ2೥໨ • ਪનج൫ͷӡ༻ɾ։ൃ • σʔλαΠΤϯςΟετͷੜ࢈ੑര্͛ • কདྷͷເ͸nasaͱ͍͏ϋϯυϧωʔϜΛ΋͗औΔ͜ͱ • GitHub: k-nas a • Twitter: nasa_desu • Zenn: nasa 🎉 🎉 🎉 🎉 🎉
  2. ©2022 Wantedly, Inc. ༨ஊIFBQ TUBDLͲͪΒ͕࢖ΘΕ͍ͯΔ͔ package main import "fmt" func

    main() { slice := make([]int64, 0, 0) fmt.Println(slice) c := make(chan string) fmt.Println(c) s := "string" fmt.Println(s) } :) % go build -gcflags '-m' alloc_playground.go # command-line-arguments ./alloc_playground.go:7:13: inlining call to fmt.Println ./alloc_playground.go:10:13: inlining call to fmt.Println ./alloc_playground.go:13:13: inlining call to fmt.Println ./alloc_playground.go:6:15: make([]int64, 0, 0) escapes to heap ./alloc_playground.go:7:13: slice escapes to heap ./alloc_playground.go:7:13: []interface {}{ ... } does not escape ./alloc_playground.go:10:13: []interface {}{ ... } does not escape ./alloc_playground.go:13:13: s escapes to heap ./alloc_playground.go:13:13: []interface {}{ ... } does not escape <autogenerated >: 1: .this does not escape • -gcflags “-m”Λ͚ͭΔͱ෼͔Δ
  3. ©2022 Wantedly, Inc. (PΞϩέʔλʔͷߏ੒ཁૉ • ͋ͱ͔Βৄ͘͠ݟ͍͕ͯͬ͘͘͟Γઆ໌ͯ͠ΈΔ • G : ΰϧʔνϯ

    • mcache: OSεϨου͝ͱͷΩϟογϡ • mcentral: ۭ͖ྖҬΛ؅ཧ͢Δ • mheap: OS͔ΒϝϞϦΛऔಘ͠؅ཧ
  4. ©2022 Wantedly, Inc. • mcach e • mcache͸ϚϧνεϨουͰ΋ੑೳΛग़ͨ͢Ίʹଘࡏ • mcentral͔ΒϝϞϦͷׂ౰͕ߦΘΕΔ

    • 1 threadʹ1mcacheͱ͍͏ߏ੒ͳͷͰϩοΫΛऔΒ ͳͯ͘΋҆શʹѻ͑Δ (PΞϩέʔλʔͷߏ੒ཁૉ
  5. ©2022 Wantedly, Inc. • mcentra l • mheap͔ΒϝϞϦׂ౰͕ߦΘΕΔ • (ຊ౰͸ϝϞϦͰ͸ͳ͍͕Ұ୴୺ંͬͯઆ໌͢Δ

    ) • ෳ਺εϨου͔Βࢀর͞ΕΔͷͰϩοΫΛऔΔඞཁ͕͋Δ • ଎౓͕ॏཁͳͷͰϩοΫͷ୯Ґ͸࠷খʹͳΔΑ͏ʹઃܭ͞ Ε͍ͯΔ • mhea p • OS͔ΒϝϞϦΛಘΔ • ॏ͍ॲཧʹͳΔͷͰҰ౓ͰΨοπϦϝϞϦΛऔಘ͢Δ (PΞϩέʔλʔͷߏ੒ཁૉ
  6. ©2022 Wantedly, Inc. TQBO • αΠζ͸Ͳ͏ܾ·͍ͬͯΔͷ͔ ? • sizeclass.goʹఆٛͯ͋͠Δ •

    ࣮ࡍʹׂΓ౰ͯΒΕΔΦϒδΣΫ τ͸͜ͷαΠζϐολϦʹ͸ͳΒ ͳ͍ • ͋Δఔ౓ແବ͸ڐ༰͞Ε͍ͯΔ package runtime / / class bytes/obj bytes/span objects tail waste max waste min align / / 1 8 8192 1024 0 87.50% 8 / / 2 16 8192 512 0 43.75% 16 / / 3 24 8192 341 8 29.24% 8 / / 4 32 8192 256 0 21.88% 32 / / 5 48 8192 170 32 31.52% 16 / / 6 64 8192 128 0 23.44% 64 / / 7 80 8192 102 32 19.07% 16 / / 8 96 8192 85 32 15.95% 32 / / 9 112 8192 73 16 13.56% 16 / / 10 128 8192 64 0 11.72% 128 / / 11 144 8192 56 128 11.82% 16 / / 12 160 8192 51 32 9.73% 32 / / 13 176 8192 46 96 9.59% 16 / / 14 192 8192 42 128 9.25% 64
  7. ©2022 Wantedly, Inc. • Ξϩέʔλʔ • ࢖༻ՄೳͳώʔϓϝϞϦͷ؅ཧΛߦ͏ • ߴ଎ͰஅยԽ͕ى͜ΓͮΒ͍Ξϩέʔλʔ͕ྑ͍ GoΞϩέʔλʔ

    • εϨου͝ͱʹΩϟογϡΛઃ͚ͯCPU਺ʹରͯ͠εέʔϧ͢ΔΑ͏ʹͳ͍ͬͯΔ • OS͔Βऔಘͨ͠ϝϞϦΛϒϩοΫʹ෼͚ͯߴ଎ͳׂ౰Λ࣮ݱ͍ͯ͠Δ ·ͱΊ