Sitemap

Boosting Go Performance with Better Memory Layout ๐Ÿš€

2 min readSep 30, 2025
Press enter or click to view image in full size

When we talk about optimizing Go programs, the first things that come to mind are goroutines, channels, or clever algorithms. But thereโ€™s a silent factor that often makes a huge difference: how your data is laid out in memory.

This is known as spatial locality โ€” and leveraging it can make your Go apps much faster without changing your logic.

Why Memory Layout Matters?

CPUs fetch memory in cache lines (small blocks, usually 64 bytes). If your data sits contiguously in memory, the CPU can grab an entire block at once. But if your data is scattered (like a linked list jumping around pointers), the CPU wastes cycles fetching from different places.

  • Contiguous data โ†’ Cache-friendly โ†’ Fast ๐Ÿš€
  • Scattered data โ†’ Cache misses โ†’ Slow ๐Ÿข

Example: Linked List vs Slice

Letโ€™s compare two ways of storing integers: a linked list and a slice.

package main

import (
"fmt"
"time"
)

// Linked list node
type Node struct {
value int
next *Node
}

// Build a linked list with n elements
func buildLinkedList(n int) *Node {
head := &Node{value: 0}
current := head
for i := 1; i < n; i++ {
current.next = &Node{value: i}
current = current.next
}
return head
}

// Traverse linked list and sum values
func sumLinkedList(head *Node) int {
sum := 0
for head != nil {
sum += head.value
head = head.next
}
return sum
}

// Traverse slice and sum values
func sumSlice(values []int) int {
sum := 0
for _, v := range values {
sum += v
}
return sum
}

func main() {
const N = 10_000_000

// Linked list
list := buildLinkedList(N)
start := time.Now()
sum1 := sumLinkedList(list)
fmt.Printf("Linked list sum=%d, time=%v\n", sum1, time.Since(start))

// Slice
values := make([]int, N)
for i := 0; i < N; i++ {
values[i] = i
}
start = time.Now()
sum2 := sumSlice(values)
fmt.Printf("Slice sum=%d, time=%v\n", sum2, time.Since(start))
}

Expected Output (approx)

Linked list sum=49999995000000, time=220ms
Slice sum=49999995000000, time=15ms

๐Ÿ‘‰ On the same dataset, iterating over a slice can be 10โ€“20x faster than a linked list, purely because of memory layout!

Tips for Go Developers

  1. โœ… Prefer slices/arrays over linked lists/maps for iteration.
  2. โœ… Group โ€œhotโ€ fields together in structs to reduce padding.
  3. โœ… Use batch allocations instead of spreading many small pointers across memory.

--

--

Vatsal
Vatsal

Written by Vatsal

Hi ๐Ÿ‘‹, Iโ€™m Vatsal. A passionate Software Developer | Fun fact: Funny, Anime-addict, Binge Watcher. | Follow Me on GitHub: https://github.com/backendArchitect

No responses yet