Saving a Third of Our Memory by Re-Ordering Go Struct Fields

This page summarizes the projects mentioned and recommended in the original post on news.ycombinator.com

Our great sponsors
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • SaaSHub - Software Alternatives and Reviews
  • go

    The Go programming language

  • https://github.com/golang/go/issues/10014#issuecomment-91436...

    > It seems fine to me for the spec not to guarantee anything about struct field order in memory. The spec doesn't operate at that level.

    > That said, no Go compiler should probably ever reorder struct fields. That seems like it is trying to solve a 1970s problem, namely packing structs to use as little space as possible. The 2010s problem is to put related fields near each other to reduce cache misses, and (unlike the 1970s problem) there is no obvious way for the compiler to pick an optimal solution. A compiler that takes that control away from the programmer is going to be that much less useful, and people will find better compilers.

  • maligned

    Tool to detect Go structs that would take less memory if their fields were sorted.

  • The standard way to check for these was https://github.com/mdempsky/maligned. It is now deprecated in favour of https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/fie....

    You can now check for these using go vet:

    go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

    InfluxDB logo
  • golangci-lint

    Fast linters Runner for Go

  • rfcs

    RFCs for changes to Rust

  • Is the situation for Rust different than as described here: https://github.com/rust-lang/rfcs/pull/208

    “Currently, attributes and macros/syntax extensions are both conceptually macros: they are user-definable syntactic extensions that transform token trees to token trees.”

    FWIW I definitely admit the point about Go annotations sort of cheating into this space a bit (same with struct tags which I revile…).

  • structslop

    structslop is a static analyzer for Go that recommends struct field rearrangements to provide for maximum space/allocation efficiency.

  • Go doesnt do this by default, but there are some analyzers that help you do this - https://github.com/orijtech/structslop

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts