draft: The Hidden Cost of Misalignment#622
draft: The Hidden Cost of Misalignment#622chrismerck wants to merge 4 commits intomemfault:masterfrom
Conversation
## User Prompt "use superpowers writing style skill to review packed-aligned.md" "use superpowers systemic debugging and subagent-driven approach to validate each of the accuracy concerns above. in particular, download the linked struct-lint repo as a subdir here and attempt to repro" "This Isn't Platform-Specific --> needs better title, since it _is_ platform specific" [SVGs, struct-lint repo, and Godbolt disassembly used for validation] ## Analysis Writing review identified accuracy concerns and style improvements. Technical review validated claims by reproducing disassembly with struct-lint and cross-compiling for seven architectures. ## Implementation - Rename title: "The Hidden Cost of Misalignment" - Rename section: "Not Just RISC-V" (was "This Isn't Platform-Specific") - Add fishtank intro linking to prior embedded-db post - Expand arch table: MIPS32 (2x), macOS arm64 (1x), i686/x86_64 (1x) - Add footnotes: endianness, aligned(8) for 64-bit, stride computation - Add array-stride SVG showing pack(1) vs packed+aligned(4) layout - Replace "instructions" with "ops" throughout for precision - Remove verbose 64-bit disassembly listing (summarize as "14 ops") - Remove redundant "Measured Results" table (covered by arch table) - Show full reassembly instructions in read examples - Clarify struct evolution risk: insertion vs append Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…tnotes Per user prompts: - "Why This Happens --> move to admonition block" - "Catching Misalignment with struct-lint --> also move to admonition" - "move the whole 'There's a bonus, too...' stuff as a footnote" - "The stride difference has a second cost: address computation. --> remove, and add the footnote after 'each element is padded to 20 bytes', rewriting it to be simpler" Changes: - Convert "Why This Happens" section to blockquote admonition - Convert "Catching Misalignment with struct-lint" to blockquote admonition with code block preserved inside - Move battery_mv read bonus (asm examples) to footnote [^bonus], simplified to plain English - Simplify stride footnote [^stride] and relocate inline Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Statement on the use of AI in the preparation of this post: I heavily used Claude Code / superpowers in developing |
|
Thank you @chrismerck for submitting! This post looks great. We'll formally review this as soon as we can |
| why you lint. | ||
|
|
||
| > **Catching misalignment with struct-lint.** | ||
| > [struct-lint](https://github.com/chrismerck/struct-lint) reads DWARF debug |
There was a problem hiding this comment.
would be interesting to compare with pahole https://linux.die.net/man/1/pahole , worth noting in the article I think
There was a problem hiding this comment.
Hey Noah! Thanks for the review and pointing to pahole, I'm ashamed to say I had never heard of it. --- Definitely something I will try out here, and if it serves the purpose I'll switch focus to the standard tool.
There was a problem hiding this comment.
Tbh I think struct-lint is more useful- definitely adding to the tool belt!
noahp
left a comment
There was a problem hiding this comment.
Looks great, thanks for the article @chrismerck !
| Here's the same 32-bit write (`temperature_mc` at offset 8) across seven | ||
| targets: | ||
|
|
||
| | Architecture | `pack(1)` | `packed, aligned(4)` | Ratio | |
I was speaking with an anonymous acquaintance who is expert in designing performance databases, and they gave me the idea to study how alignment impacts performance in embedded systems at Bond Home.
I thought we were in good shape, maybe just needing some lints to flag introducing unaligned fields into packed structs in our database and network protocols.
What I found surprised me:
pragma packwas wrecking codegen, emitting 7x more instructions than expected.This post shares the learnings from this, suggestions on how to pack efficiently (aligned and packed attributes can be combined), and to use a struct linter to catch alignment regressions as structs evolve over time.