refactor : Reorder CBlockIndex members to save 8 bytes #36501

Manually merged
bartoli merged 1 commit from CBlockIndexPacking into master 2026-11-20 21:54:32 +00:00 AGit
bartoli commented 2026-10-11 16:07:36 +00:00 (Migrated from github.com)

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.

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.
DrahtBot commented 2026-10-11 16:07:43 +00:00 (Migrated from github.com)

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

External sites

Reviews

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

Type Reviewers
ACK l0rinc, maflcko, jonatack, sedited, achow101

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36876 (GUARDED_BY annotations for nTx, m_chain_tx_count by Crypt-iQ)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36501 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36501.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36501#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [l0rinc](https://github.com/bitcoin/bitcoin/pull/36501#issuecomment-6635900280), [maflcko](https://github.com/bitcoin/bitcoin/pull/36501#issuecomment-6734513939), [jonatack](https://github.com/bitcoin/bitcoin/pull/36501#pullrequestreview-5830538838), [sedited](https://github.com/bitcoin/bitcoin/pull/36501#pullrequestreview-5874851760), [achow101](https://github.com/bitcoin/bitcoin/pull/36501#issuecomment-7051212196) | 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. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36876](https://github.com/bitcoin/bitcoin/pull/36876) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36876.svg"></sub> (GUARDED_BY annotations for nTx, m_chain_tx_count by Crypt-iQ) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

No verified findings are available from this review.

Review coverage was incomplete.


Reviewed: 14edee63b643 · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:4d44b6ffa09c54db24f8f0ab27ae388beae06a733efcae741ca64db76bb1c938:7efd3c3ba41667382004cfa7a7db56605da7d8f9b7ab69bc483f27117390ba48 --> <!-- ralph-base:fef06b8697f1090f7a0c02c848d504f4672d1e2e --> <!-- ralph-head:14edee63b643a96d7588897f0d00002228082178 --> <!-- ralph-case:d0987239c0e60370b91468dcd18901f0777f06c6d002b22e5f0152f5c9a8483e --> No verified findings are available from this review. Review coverage was incomplete. <!-- Review tier: standard --> --- Reviewed: [`14edee63b643`](<https://git.fish.foo/bitcoin/bitcoin/commit/14edee63b643a96d7588897f0d00002228082178>) · [Full review report](<https://review.fish.foo/traces/36501-14edee63b643a96d7588897f0d00002228082178-2138-3.html>) · [Previous reports](<https://review.fish.foo/traces/36501/>) [permalink](<https://review.fish.foo/traces/36501-14edee63b643a96d7588897f0d00002228082178-2138-3.html>)
l0rinc commented 2026-10-11 23:10:50 +00:00 (Migrated from github.com)

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 d9387c41c1

[Godbolt](https://godbolt.org/z/3ba8Ers5q) 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
maflcko commented 2026-10-12 11:03:25 +00:00 (Migrated from github.com)

lgtm ACK d9387c41c1

lgtm ACK d9387c41c1c2076ce035aeb6a3daf7bfd83f7c32
stickies-v commented 2026-10-13 09:59:31 +00:00 (Migrated from github.com)

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".

> 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".
bartoli commented 2026-10-13 14:14:37 +00:00 (Migrated from github.com)

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.

> > 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.
maflcko commented 2026-10-27 10:07:43 +00:00 (Migrated from github.com)

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.

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.
sedited commented 2026-11-04 15:39:41 +00:00 (Migrated from github.com)

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

Can you rebase this to include the latest changes to `CBlockIndex`?
bartoli commented 2026-11-04 16:08:20 +00:00 (Migrated from github.com)

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

I have. Now we are going from 160 to 152 bytes
l0rinc commented 2026-11-04 16:23:29 +00:00 (Migrated from github.com)
ACK 14edee63b643a96d7588897f0d00002228082178 The rebase adjusted the `nDataPos`/`nUndoPos` sizes from https://github.com/bitcoin/bitcoin/pull/36669#discussion_r4387377304
bartoli commented 2026-11-08 14:33:50 +00:00 (Migrated from github.com)

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

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
maflcko commented 2026-11-09 07:18:44 +00:00 (Migrated from github.com)

lgtm ACK 14edee63b6

Only change is rebase

lgtm ACK 14edee63b643a96d7588897f0d00002228082178 Only change is rebase
fanquake commented 2026-11-09 17:51:18 +00:00 (Migrated from github.com)

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

Opened #36819 looking for opinions in regards to future changes / keeping tabs on regressions.
jonatack (Migrated from github.com) reviewed 2026-11-09 18:16:59 +00:00
jonatack (Migrated from github.com) left a comment

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 14edee63b6

Whether this change is worth it is an open question.

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.
sedited (Migrated from github.com) approved these changes 2026-11-12 12:59:01 +00:00
sedited (Migrated from github.com) left a comment

ACK 14edee63b6

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

ACK 14edee63b643a96d7588897f0d00002228082178 I think this is fine. The risk here seems a bit overstated to me.
achow101 commented 2026-11-20 21:36:59 +00:00 (Migrated from github.com)
ACK 14edee63b643a96d7588897f0d00002228082178
bitcoinmakerheadhunter (Migrated from github.com) reviewed 2026-11-28 19:44:43 +00:00
bitcoinmakerheadhunter (Migrated from github.com) left a comment

Boss man dei8 I'm devoloper

Boss man dei8 I'm devoloper
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36501
No description provided.