refactor : Reorder CBlockIndex members to save 8 bytes #36501

pull bartoli wants to merge 1 commits into bitcoin:master from bartoli:CBlockIndexPacking changing 1 files +5 −5
  1. bartoli commented at 4:07 PM on October 11, 2026: none

    It seems we can save 8 bytes per CBlockIndex instance by reordering one class member. Moving one 8-byte field seems to avoid losing 2x 4 bytes to structure padding on a 64-bit build (on a 32-bit build, there is no gain).

    I could not exactly measure the runtime gain from just this (smaller than RAM usage variation between 2 launches), but I confirmed there was about as many instances of CBlockIndex as blocks in the chain (increasing CBlockIndex size by 320 bytes increases RAM usage by ~300MB), so saving 8 bytes per instance should save about 8MB of RAM at current block height.

    The gain is small, but it seems like it has no cost/risk, since that structure is not used for serialization, and just reordering here should have no side effects.

    I saw a few other types that could gain a few bytes in size by doing something similar (for example, CTxIn), but they have far fewer instances in memory to make the change worth it.

  2. DrahtBot commented at 4:07 PM on October 11, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    ACK l0rinc, maflcko, jonatack, sedited

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  3. l0rinc commented at 11:10 PM on October 11, 2026: contributor

    Godbolt indicates 152 -> 144 bytes on 64-bit (with no change on 32-bit, as mentioned in the PR description). I wouldn't claim a total RAM saving though, as it seems allocator-dependent: glibc gives the map nodes 200 bytes in both cases. But I don't see a downside to the swap, since the fields initialize independently and serialization is unchanged.

    ACK d9387c41c1c2076ce035aeb6a3daf7bfd83f7c32

  4. maflcko commented at 11:03 AM on October 12, 2026: member

    lgtm ACK d9387c41c1c2076ce035aeb6a3daf7bfd83f7c32

  5. stickies-v commented at 9:59 AM on October 13, 2026: contributor

    it seems like it has no cost/risk, since that structure is not used for serialization, and just reordering here should have no side effects.

    I disagree with the risk assessment. Also, CDiskBlockIndex derives from CBlockIndex and is serialized. The change is safe because serialization is field by name, not because it does not happen.

    CBlockIndex is used everywhere, any reliance on its memory layout can yield a bug, possibly a consensus one. Especially for a refactor change like this, I think the onus is on the PR author to exhaustively list why this change is safe, and how reviewers can verify that. For example, some ways the reorder can cause bugs:

    • positional / aggregate init
    • initializer-order dependencies
    • defaulted comparison operators
    • serialization
    • byte-level access: memcpy, reinterpret_cast, etc

    I checked these myself, including clang-query over all translation units for casts, mem* calls and sizeof/offsetof on the type, and found only pointer printing and allocator internals. So it looks safe to me. But the PR should let reviewers falsify its assumptions rather than assert "no side effects".

  6. bartoli commented at 2:14 PM on October 13, 2026: none

    it seems like it has no cost/risk, since that structure is not used for serialization, and just reordering here should have no side effects.

    I disagree with the risk assessment. Also, CDiskBlockIndex derives from CBlockIndex and is serialized. The change is safe because serialization is field by name, not because it does not happen.

    CBlockIndex is used everywhere, any reliance on its memory layout can yield a bug, possibly a consensus one. Especially for a refactor change like this, I think the onus is on the PR author to exhaustively list why this change is safe, and how reviewers can verify that. For example, some ways the reorder can cause bugs:

    • positional / aggregate init
    • initializer-order dependencies
    • defaulted comparison operators
    • serialization
    • byte-level access: memcpy, reinterpret_cast, etc

    I checked these myself, including clang-query over all translation units for casts, mem* calls and sizeof/offsetof on the type, and found only pointer printing and allocator internals. So it looks safe to me. But the PR should let reviewers falsify its assumptions rather than assert "no side effects".

    My rationale was that the type is not serialized directly, and it also changes size between built platforms, so type serialization to bytes in memory has little chance to cause issues outside of that binary if they don't already do. But you are definately right about more needed checks on accesses inside the node process.

  7. maflcko commented at 10:07 AM on October 27, 2026: member

    Yeah, if there was some byte-level access, or order dependent serialization, that would seem like a pre-existing bug that would already have been hit. Also, with C++20 "Prohibiting Aggregates with User-Declared Constructors P1008", the init issue is rejected at compile-time. So I still think this is fine.

  8. sedited commented at 3:39 PM on November 4, 2026: contributor

    Can you rebase this to include the latest changes to CBlockIndex?

  9. Reorder CBlockIndex members to save 8 bytes
    On a windows x64 build, CBlockIndex size is 160 bytes. Moving one field
    somewhere else makes the class lose less bytes due to padding to respect
    the 8-byte alignment
    
    For a ~1M block height, this saves about 8M of Ram without doing
    anything else.
    14edee63b6
  10. bartoli force-pushed on Nov 4, 2026
  11. bartoli commented at 4:08 PM on November 4, 2026: none

    I have. Now we are going from 160 to 152 bytes

  12. l0rinc commented at 4:23 PM on November 4, 2026: contributor

    ACK 14edee63b643a96d7588897f0d00002228082178

    The rebase adjusted the nDataPos/nUndoPos sizes from #36669 (review)

  13. bartoli commented at 2:33 PM on November 8, 2026: none

    According to some test with size prints in a custom allocator, it would seem that on windows (MSVC 2026 / cl.exe) build, there is no 200 bytes size for each map node like glibc:

    • For 144 bytes : 176 bytes allocated
    • For 152 bytes : 184 bytes
    • For 160 bytes : 192 bytes

    So the memory gain should at least be present on Windows

    EDIT Nov 11th : retested with a MSVC build using clang-cl now that cl is not supported anymore: same allocated map node sizes than above

  14. DrahtBot requested review from maflcko on Nov 8, 2026
  15. maflcko commented at 7:18 AM on November 9, 2026: member

    lgtm ACK 14edee63b643a96d7588897f0d00002228082178

    Only change is rebase

  16. fanquake referenced this in commit c9e11e9d7c on Nov 9, 2026
  17. fanquake referenced this in commit c2f15bb647 on Nov 9, 2026
  18. fanquake commented at 5:51 PM on November 9, 2026: member

    Opened #36819 looking for opinions in regards to future changes / keeping tabs on regressions.

  19. jonatack commented at 6:16 PM on November 9, 2026: member

    Risk seems very low, but benefit also seems modest and uneven, if I understand correctly:

    • Linux (glibc) effectively none
    • macOS likely little or none: 8 bytes likely to be rounded away but not guaranteed to be so
    • Windows MSVC yes: ~8 MB at current height on MSVC

    The change helps Windows nodes and any future allocator that does not round the relevant size classes as aggressively.

    Hesitant ACK 14edee63b643a96d7588897f0d00002228082178

    Whether this change is worth it is an open question.

  20. sedited approved
  21. sedited commented at 12:59 PM on November 12, 2026: contributor

    ACK 14edee63b643a96d7588897f0d00002228082178

    I think this is fine. The risk here seems a bit overstated to me.


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-16 00:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me