Home InternationalAllocating on the Stack...
International⭐ Featured

Allocating on the Stack

A description of some of the recent changes to do allocations on the stack instead of the heap.

7 April 2026 at 07:17 am
1 views
Allocating on the Stack

In recent years, the Go programming language has been making significant strides to improve the performance of its programs. One of the primary areas of focus has been mitigating the slowness caused by heap allocations. Each time a Go program allocates memory from the heap, a substantial amount of code must run to satisfy that allocation. Additionally, heap allocations place a significant burden on the garbage collector, even with recent advancements like Green Tea. To address these issues, the Go team has been exploring ways to perform more allocations on the stack instead of the heap.

Stack allocations are far more efficient than heap allocations. They often come at no cost and do not require the garbage collector to do any additional work. Furthermore, stack allocations enable prompt reuse, which is highly cache-friendly. This can lead to significant performance improvements in programs that frequently allocate and deallocate memory.

One example of how the Go team has implemented stack allocations is by optimizing the handling of constant-sized slices. Consider a scenario where a program is building a slice of tasks to process:

```go

func process(c chan task) {

var tasks []task

for t := range c {

tasks = append(tasks, t)

}

processAll(tasks)

}

```

In this code, the `tasks` slice is being populated by reading tasks from a channel `c`. Let's examine what happens at runtime when the `append` function is called to add tasks to the slice.

On the first iteration of the loop, there is no backing store for the `tasks` slice. As a result, the `append` function must allocate a new backing store. Since it cannot predict the final size of the slice, it starts with a small initial allocation, typically of size 1.

On the second iteration, the backing store exists, but it is full. The `append` function must allocate a new backing store, this time of size 2. The old backing store of size 1 is now considered garbage and will be collected by the garbage collector.

This pattern continues with each iteration. On the third iteration, the backing store of size 2 is full, so a new one of size 4 is allocated. The old backing store of size 2 becomes garbage. On the fourth iteration, the backing store of size 4 is full, leading to the allocation of a new one of size 8. This process continues, resulting in a series of growing allocations and subsequent garbage collections.

To address this inefficiency, the Go team has introduced stack-based allocations for constant-sized slices. By allocating memory on the stack instead of the heap, the program can avoid the overhead of garbage collection and the repeated allocations and deallocations associated with heap-based slices. This change not only improves performance but also reduces the complexity of memory management in Go programs.

In conclusion, the Go team's efforts to shift from heap to stack allocations represent a significant step forward in optimizing the performance of Go programs. By minimizing the use of the garbage collector and reducing the overhead associated with heap allocations, these changes enable developers to write more efficient and faster code. As the Go language continues to evolve, these optimizations will play a crucial role in maintaining its reputation for high performance and ease of use.

Source: The Go Blog
📰 Related News
Ollama 0.2.6 Released with Native Gemma 4 Support and Enhanced Performance
Ollama 0.2.6 Released with Native Gemma 4 Support and Enhanced Performance
Ollama 0.2.6 is now live, featuring native support for Google's Gemma 4 models and improved local inference performance for Windows, macOS, and Linux.
14 Apr
Weekly news roundup: Shortages spread to MLCCs; SK Hynix reportedly in talks with Microsoft and Google
Weekly news roundup: Shortages spread to MLCCs; SK Hynix reportedly in talks with Microsoft and Google
Below are the most-read DIGITIMES Asia stories from the week of April 6-April 13, 2026:
14 Apr
cutile-stencil 0.2.0
cutile-stencil 0.2.0
An xDSL-based stencil compiler that generates optimized GPU kernels via NVIDIA cuTile
14 Apr
merlin-llm added to PyPI
merlin-llm added to PyPI
Merlin — a fast local LLM for agentic coding on Apple Silicon
14 Apr
Fluent Cut - Craft and compose videos programmatically in PHP with an elegant fluent API
Fluent Cut - Craft and compose videos programmatically in PHP with an elegant fluent API
Craft and compose videos programmatically in PHP with an elegant fluent API - b7s/fluentcut
14 Apr
Crypto Investor at Center of Trump Corruption Allegations Now Sees Himself as ‘Victim’
Crypto Investor at Center of Trump Corruption Allegations Now Sees Himself as ‘Victim’
Justin Sun has accused Trump-affiliated World Liberty Financial of misconduct and a general lack of transparency.
14 Apr
nvidia-nat-weave 1.7.0a20260413
nvidia-nat-weave 1.7.0a20260413
Subpackage for Weave integration in NeMo Agent Toolkit
14 Apr
nvidia-nat-s3 1.7.0a20260413
nvidia-nat-s3 1.7.0a20260413
Subpackage for S3-compatible integration in NeMo Agent Toolkit
14 Apr
Social Security Trust Fund to Run Dry in 2032: Just 6 Years From Now
Social Security Trust Fund to Run Dry in 2032: Just 6 Years From Now
Six years. That is how much time separates retirees from a Social Security system that, by its own projections, runs out of money. If you are 56 years old...
14 Apr
cane-gpu-perf added to PyPI
cane-gpu-perf added to PyPI
GPU inference benchmarking with opinionated diagnostics
13 Apr