
Masalah
Pelolosan memori heap memiliki masalah berikut yang berpotensi dapat diperbaiki:
Penurunan kinerja karena biaya alokasi memori
Penurunan kinerja karena biaya pengumpulan sampah
Kesalahan
Out of Memorymuncul jika tingkat kemunculan sampah melebihi tingkat penghapusannya
Masalah ini dapat diselesaikan dengan beberapa cara:
Peningkatan jumlah sumber daya komputasi (memori, prosesor)
Menyetel pengumpul sampah
Dengan meminimalkan jumlah tunas dalam satu tumpukan
Pada artikel ini, saya hanya akan membahas cara ketiga.
Dari awal
, , , . , : make , new , map ,go . , "", - .
make new . sync.Pool(), habr . , []byte , , - 32 ( go1.14.6 windows/amd64). , ; , , butebufferpool @valyala.
map . map , . , map, , - Discord Rust. .. , , , - . @valyala fastcache, , , , , , slices maps.
go - . , .
, - "" , , fastcache "". , , []byte->[]byte, , . , , - , "rps", "95th percentile latency" .. , , "" , . " " . .. "".
" " :
git clone https://github.com/valyala/fasthttpgo test -bench=PServerGet10Req -benchmem -memprofile netmem.outgo test -bench=kServerGet10Req -benchmem -memprofile fastmem.out http.Server, - fasthttp.Server. , fasthttp zero-allocation . , netmem.out fastmem.out. -, :
echo top | go tool pprof netmem.out10 "" :
Showing top 10 nodes out of 53
flat flat% sum% cum cum%
698.15MB 21.85% 21.85% 710.15MB 22.22% net/textproto.(*Reader).ReadMIMEHeader
466.13MB 14.59% 36.43% 466.13MB 14.59% net/http.Header.Clone
423.07MB 13.24% 49.67% 1738.32MB 54.39% net/http.(*conn).readRequest
384.12MB 12.02% 61.69% 384.12MB 12.02% net/textproto.MIMEHeader.Set
299.07MB 9.36% 71.05% 1186.24MB 37.12% net/http.readRequest
137.02MB 4.29% 75.33% 137.02MB 4.29% bufio.NewReaderSize
134.02MB 4.19% 79.53% 134.02MB 4.19% net/url.parse
122.45MB 3.83% 83.36% 122.45MB 3.83% bufio.NewWriterSize (inline)
99.51MB 3.11% 86.47% 133.01MB 4.16% context.WithCancel
87.11MB 2.73% 89.20% 87.11MB 2.73% github.com/andybalholm/brotli.(*h5).Initialize:
go tool pprof -svg netmem.out > netmem.svg netmem.svg ():

:
go tool pprof -http=:8088 netmem.out, , , - : Could not execute dot; may need to install graphviz. , Unix- , , Windows chocolatey , , cinst graphviz. - . VIEW/Source:

, make, []byte string. , " ", []byte. "", , , .. return &localVar . , - .
fasthttp , . - fasthttp HTTP/2.0, websockets , fasthttp ( , ), , , . c5.4xlarge 250.000 RPS fasthttp.Server 190.000 RPS http.Server . , , 190.000 RPS? , , , , .
, , . " " , json/yaml , , . - , , , .
, flatbuffers, dynobuffers, - , . , , , .
" ":
Avro 23394 ns/op 11257 B/op
Dyno_Untyped 6437 ns/op 808 B/op
Dyno_Typed 3776 ns/op 0 B/op
Flat 1132 ns/op 0 B/op
Json 87331 ns/op 14145 B/op" ":
Avro 19311 ns/op 11257 B/op
Dyno_Typed 62.2 ns/op 0 B/op
Flat 19.8 ns/op 0 B/op
Json 83824 ns/op 11073 B/op , , , linkedin/goavro - .
- . , () , avro "" , flatbuffer.
Masalah yang menyebabkan kinerja buruk di Go dapat diatasi, namun perlu diingat bahwa perjuangan ini memiliki konsekuensi. Sebelum memulainya, lebih baik secara eksperimental membandingkan kemampuan metode pemrosesan data yang dipilih dengan persyaratan sistem - mungkin semuanya akan berfungsi "di luar kotak" dengan sedikit penyesuaian.