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

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

3f0a6648237e57a670f76b50d0b72bf1?s=47 nasa
June 29, 2022

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

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

3f0a6648237e57a670f76b50d0b72bf1?s=128

nasa

June 29, 2022
Tweet

More Decks by nasa

Other Decks in Technology

Transcript

  1. ©2022 Wantedly, Inc. GoͷϝϞϦΞϩέʔλʔ Bitkey × Voicyओ࠵ ~ Goษڧձ ~

    2022/06/29 nasa (Asan Kondo)
  2. ©2022 Wantedly, Inc. ࣗݾ঺հ • nasa Ͱ͢ • Wantedly, inc.

    ৽ଔ2೥໨ • ਪનج൫ͷӡ༻ɾ։ൃ • σʔλαΠΤϯςΟετͷੜ࢈ੑര্͛ • কདྷͷເ͸nasaͱ͍͏ϋϯυϧωʔϜΛ΋͗औΔ͜ͱ • GitHub: k-nas a • Twitter: nasa_desu • Zenn: nasa 🎉 🎉 🎉 🎉 🎉
  3. ©2022 Wantedly, Inc. -5ͷ໨ඪ • ϝϞϦΞϩέʔλʔ͕ԿΛ͍ͯ͠Δ͔෼͔Δ • GoݴޠͷΞϩέʔλͷߏ଄͕গ͠෼͔Δ • RuntimeύοέʔδΛ೷͍ͯΈΔ΂ʂͱ͍͏ؾ࣋ͪʹͳΔ

  4. ©2022 Wantedly, Inc. ໨࣍ 1. ϝϞϦΞϩέʔλʔͱ͸ 2. GoͷϝϞϦΞϩέʔλʔͷઃܭ 3. ·ͱΊ

  5. ©2021 Wantedly, Inc. ϝϞϦΞϩέʔλʔͱ͸ʁ

  6. ©2022 Wantedly, Inc. • ࢖༻ՄೳͳώʔϓϝϞϦͷ؅ཧΛߦ͏ ϓϩάϥϜʮ3byteͩ͘͞ʔ͍ʯ Ξϩέʔλʔʮ΁͍ɺ͓଴ͪʂ(3byteۭ͍ͯΔ͔Β͋ͦ͜Λฦͦ͏)ʯ ϝϞϦΞϩέʔλʔͱ͸ʁ

  7. ©2022 Wantedly, Inc. • ࢖༻ՄೳͳώʔϓϝϞϦͷ؅ཧΛߦ͏ ଞͰ࢖༻͍ͯ͠ΔϝϞϦΛฦͪ͠Ό͍͚ͳ͍ Ξϩέʔλʔ͸ະ࢖༻ྖҬΛ஌͍ͬͯΔඞཁ͕͋Δ ղ์͞ΕͨϝϞϦΛ࠶ར༻͢Δ ϝϞϦΞϩέʔλʔͱ͸ʁ

  8. ©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”Λ͚ͭΔͱ෼͔Δ
  9. ©2022 Wantedly, Inc. ਖ਼֬ʹϝϞϦΛฦ͚ͩ͢Ͱ͍͍ͷ͔ʁ • ਖ਼֬͞͸MustͰ࣍ͷ̎ͭ΋௥ٻ͍ͨ͠ • ଎౓ • ଎͍΄͏͕ͦΓΌخ͍͠ΑͶʂ

    • அยԽ • ϝϞϦΛແବͳ͘ར༻͍ͨ͠ΑͶʂ
  10. ©2022 Wantedly, Inc. ਖ਼֬ʹϝϞϦΛฦ͚ͩ͢Ͱ͍͍ͷ͔ʁ • அยԽʹ͍ͭͯܰ͘࿩͢ ϓϩάϥϜʮ8byteͩ͘͞ʔ͍ʯ Ξϩέʔλʔʮ඼੾ΕͰ͢ɺɺɺʯ

  11. ©2022 Wantedly, Inc. ਖ਼֬ʹϝϞϦΛฦ͚ͩ͢Ͱ͍͍ͷ͔ʁ • ࢖༻ՄೳͳώʔϓϝϞϦͷ؅ཧΛߦ͏ • ߴ଎ͰஅยԽ͕ى͜ΓͮΒ͍Ξϩέʔλʔ͕ྑ͍

  12. ©2021 Wantedly, Inc. (PͷϝϞϦΞϩέʔλʔ

  13. ©2022 Wantedly, Inc. (PΞϩέʔλʔͷߏ੒ཁૉ • ͋ͱ͔Βৄ͘͠ݟ͍͕ͯͬ͘͘͟Γઆ໌ͯ͠ΈΔ • G : ΰϧʔνϯ

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

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

    ) • ෳ਺εϨου͔Βࢀর͞ΕΔͷͰϩοΫΛऔΔඞཁ͕͋Δ • ଎౓͕ॏཁͳͷͰϩοΫͷ୯Ґ͸࠷খʹͳΔΑ͏ʹઃܭ͞ Ε͍ͯΔ • mhea p • OS͔ΒϝϞϦΛಘΔ • ॏ͍ॲཧʹͳΔͷͰҰ౓ͰΨοπϦϝϞϦΛऔಘ͢Δ (PΞϩέʔλʔͷߏ੒ཁૉ
  16. ©2022 Wantedly, Inc. TQBO • OS͔Βऔಘͨ͠ϝϞϦ͸༧ΊܾΊΒΕͨαΠζʹ۠੾Β Ε؅ཧ͞ΕΔ • ͜ͷαΠζ͝ͱʹ۠੾ΒΕͨϝϞϦΛspanͱݺͿ •

    mcache, mcentral͸͜ͷspanΛ؅ཧ͢Δ
  17. ©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
  18. ©2022 Wantedly, Inc. TQBO • mcache, mcentral͸͜ͷspanΛ؅ཧ͢Δ

  19. ©2022 Wantedly, Inc. TQBO • GoΦϒδΣΫτ͸spanʹׂΓ౰ͯΒΕΔ

  20. ©2022 Wantedly, Inc. • ϝϞϦ͕ඞཁʹͳͬͨΒ͜ͷਤͷྲྀΕͰׂ౰͕ߦΘΕΔ 1. mcacheʹ͋Δspanʹۭ͖͕͋ΔͳΒͦ͜Λ࢖͏ 2. mcentral͔ΒspanΛ໯ͬͯ࢖͏ 3.

    OS͔ΒϝϞϦΛଟΊʹ໯ͬͯspanʹ෼ׂͯ͠࢖͏ TQBO΋౿·ׂ͑ͨ౰
  21. ©2021 Wantedly, Inc. ·ͱΊ

  22. ©2022 Wantedly, Inc. • Ξϩέʔλʔ • ࢖༻ՄೳͳώʔϓϝϞϦͷ؅ཧΛߦ͏ • ߴ଎ͰஅยԽ͕ى͜ΓͮΒ͍Ξϩέʔλʔ͕ྑ͍ GoΞϩέʔλʔ

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