How capacity hints work in Go
grobinson.net·1w·
🧠Memory Management
Preview
Report Post

30 September 2023 — 1804 words — approx 10 minutes

Today we will look at what capacity hints are in Go and how they work for slices and maps. We will see that when creating slices the Go runtime always allocates the requested capacity. However when creating maps the Go runtime uses lazy allocation for small hints, and allocates more capacity than hinted for large hints. Hopefully by the end of this page you will have a much better understanding of how both slices and maps work internally and how their memory is allocated and resized by the Go runtime.

What is capacity?

In Go, slices have a length and a capacity. The length of a slice is the number of elements in the slice, while its capacity is the maximum number of elements that could be stored in the slice before having to …

Similar Posts

Loading similar posts...