DEV Community

Moksh
Moksh

Posted on

πŸš€ Go Struct Alignment: Why Field Order Matters for Performance

Did you know that the way you order fields in a Go struct can waste memory? Go automatically adds padding bytes to ensure proper memory alignment, and a poorly structured layout can increase memory usage by 30% or more!


❌ Bad Struct: Wasting Memory

type BadStruct struct {
    A int8  // 1 byte
    B int64 // 8 bytes
    C int8  // 1 byte
}
Enter fullscreen mode Exit fullscreen mode

Checking its size:

fmt.Println(unsafe.Sizeof(BadStruct{})) // Output: 24 bytes
Enter fullscreen mode Exit fullscreen mode

Even though the actual data is just 10 bytes, the struct takes up 24 bytes due to padding.

πŸ” Step-by-step memory layout (BadStruct)

Field Type Size (bytes) Offset Why?
A int8 1 0 Starts at 0, perfectly aligned
(padding) - 7 1-7 Added to align B to the next 8-byte boundary
B int64 8 8 Must start at an 8-byte-aligned address
C int8 1 16 Starts at the next available byte
(padding) - 7 17-23 Ensures struct size is a multiple of 8

Total size:

1 (A) + 7 (padding) + 8 (B) + 1 (C) + 7 (padding) = 24 bytes

πŸ‘‰ Go adds 14 bytes of padding, making the struct much larger than necessary! 😱


βœ… Optimized Struct: Efficient Memory Use

type GoodStruct struct {
    B int64 // 8 bytes
    A int8  // 1 byte
    C int8  // 1 byte
}
Enter fullscreen mode Exit fullscreen mode

Checking its size:

fmt.Println(unsafe.Sizeof(GoodStruct{})) // Output: 16 bytes
Enter fullscreen mode Exit fullscreen mode

By simply reordering fields, we reduce the struct size by 33% (from 24B to 16B)! πŸš€

πŸ” Step-by-step memory layout (GoodStruct)

Field Type Size (bytes) Offset Why?
B int64 8 0 Starts at 0, perfectly aligned
A int8 1 8 Starts immediately after B
C int8 1 9 Starts right after A
(padding) - 6 10-15 Ensures struct size is a multiple of 8

Total size:

8 (B) + 1 (A) + 1 (C) + 6 (padding) = 16 bytes

πŸš€ By placing larger fields first, we eliminate unnecessary padding and optimize memory usage!


πŸ” Why Does This Happen?

Go aligns fields based on their natural size (e.g., int64 must be at an address divisible by 8).

  • If a smaller field (int8) comes before a larger field (int64), Go inserts padding bytes to align memory correctly.
  • By placing larger fields first, we reduce unnecessary padding, making the struct smaller and more efficient.

πŸ›  Pro Tip

Use this to check field memory offsets:

reflect.TypeOf(struct{}).Field(i).Offset
Enter fullscreen mode Exit fullscreen mode

πŸ’¬ Have you optimized struct layouts in your Go projects? Let's discuss in the comments! πŸš€

#Golang #MemoryOptimization #Performance #GoLangTips #SoftwareEngineering

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay