Real-World Optimization Examples

See how proper field ordering reduces memory usage across common Go struct patterns. Each example shows before and after layouts with exact byte savings.

User Profile (Web Application)

Save 16 bytes (29%)

Typical user struct in a web application with mixed field types.

BEFORE 56 bytes

type User struct {
    Active    bool      // 1 + 7 padding
    ID        uint64    // 8
    Name      string    // 16
    Age       uint8     // 1 + 7 padding
    Balance   float64   // 8
    Email     string    // 16
}

AFTER 40 bytes

type User struct {
    ID        uint64    // 8
    Balance   float64   // 8
    Name      string    // 16
    Email     string    // 16
    Active    bool      // 1
    Age       uint8     // 1 + 6 final padding
}
IMPACT: 16 MB saved per 1 million users

API Response

Save 8 bytes (14%)

REST API response with metadata fields.

BEFORE 56 bytes

type APIResponse struct {
    Success   bool      // 1 + 7 padding
    Timestamp int64     // 8
    Message   string    // 16
    Code      int32     // 4 + 4 padding
    RequestID string    // 16
}

AFTER 48 bytes

type APIResponse struct {
    Timestamp int64     // 8
    Message   string    // 16
    RequestID string    // 16
    Code      int32     // 4
    Success   bool      // 1 + 3 final padding
}
IMPACT: 8 MB saved per 1 million API responses

Database Record

Save 12 bytes (20%)

Database model with timestamps and status flags.

BEFORE 60 bytes

type Record struct {
    Active    bool      // 1 + 7 padding
    CreatedAt int64     // 8
    Title     string    // 16
    Status    uint8     // 1 + 7 padding
    UpdatedAt int64     // 8
    Content   string    // 16
}

AFTER 48 bytes

type Record struct {
    CreatedAt int64     // 8
    UpdatedAt int64     // 8
    Title     string    // 16
    Content   string    // 16
    Active    bool      // 1
    Status    uint8     // 1 + 6 final padding
}
IMPACT: 12 MB saved per 1 million records

Configuration Struct

Save 20 bytes (33%)

Application configuration with multiple small fields.

BEFORE 60 bytes

type Config struct {
    Debug     bool      // 1 + 7 padding
    Port      uint16    // 2 + 6 padding
    Timeout   int64     // 8
    MaxConns  uint32    // 4 + 4 padding
    Host      string    // 16
    Verbose   bool      // 1 + 7 padding
}

AFTER 40 bytes

type Config struct {
    Timeout   int64     // 8
    Host      string    // 16
    MaxConns  uint32    // 4
    Port      uint16    // 2
    Debug     bool      // 1
    Verbose   bool      // 1 + 4 final padding
}
IMPACT: Significant savings in long-running applications

Network Packet

Save 10 bytes (25%)

Network protocol packet with header fields.

BEFORE 40 bytes

type Packet struct {
    Type      uint8     // 1 + 7 padding
    Timestamp int64     // 8
    Size      uint16    // 2 + 6 padding
    Checksum  uint32    // 4 + 4 padding
    Data      []byte    // 24 (slice header)
}

AFTER 48 bytes

type Packet struct {
    Timestamp int64     // 8
    Data      []byte    // 24
    Checksum  uint32    // 4
    Size      uint16    // 2
    Type      uint8     // 1 + 1 final padding
}
IMPACT: Better cache efficiency for high-throughput networking

Optimization Tips

  • Order fields from largest to smallest alignment requirements
  • Group fields of the same size together
  • Place pointers, strings, and slices (8-byte aligned) first on 64-bit systems
  • Follow with int64, uint64, float64 (8-byte types)
  • Then int32, uint32, float32 (4-byte types)
  • Then int16, uint16 (2-byte types)
  • Finally bool, int8, uint8 (1-byte types)
  • Use the extension's one-click optimization for automatic reordering
Install Extension