TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Structslop: Struct Alignment Analyzer for Go

10 pointsby Jonas_baover 4 years ago

5 comments

wahernover 4 years ago
&gt; That said, some structs may have a smaller size, but for efficiency, the Go runtime will allocate them in the same size class, then those structs are not considered sloppy:<p>It&#x27;s not for efficiency, per se. As in C, to support arrays of structs all structs are padded so their size is a multiple of the strictest (largest) alignment of any of its members. If the example struct described above were 24 bytes instead of 32, then the second element of an <i>array</i> of those structs wouldn&#x27;t be properly aligned for any of its uint64 or pointer members. On architectures that require aligned loads and stores that&#x27;s not an efficiency optimization, but required for correctness.
anitilover 4 years ago
Looking at this from a performance point of view, I think this could be useful with the addition of some tracing capability.<p>What I mean is, reducing the size of a barely-used struct isn&#x27;t worth the effort, but reduction of a key struct in a critical region, or a large bloated array of structs could be very useful.<p>So some measure of: possible reduction (which structslop provides), &#x27;hotness&#x27;, and total memory usage, would make this a killer feature.<p>From my bare-metal days we used to manually rearrange structs or use them packed, but it&#x27;d be great to have a tool that could attach to your codebase and do that analysis for you.
GauntletWizardover 4 years ago
This is fascinating... but I do believe there’s a reason that those fields are not re-arranged by the go compiler already, which is to preserve C ABI Compatibility. I doubt that’s worth it in most cases, though. Does it still preserve them in “pure” mode?
评论 #25042320 未加载
Jonas_baover 4 years ago
Blog post: <a href="https:&#x2F;&#x2F;medium.com&#x2F;orijtech-developers&#x2F;efficient-struct-packing-guided-pass-for-go-92255872ec72" rel="nofollow">https:&#x2F;&#x2F;medium.com&#x2F;orijtech-developers&#x2F;efficient-struct-pack...</a>
mhh__over 4 years ago
You could do this whole analysis (and changes if you wanted to) at compile time in D. Not that you should (they&#x27;re aligned like they are for a reason)