mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 15:11:38 +00:00
refactor : Reorder CBlockIndex members to save 8 bytes #36501
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36501
Loading…
Reference in a new issue
No description provided.
Delete branch "CBlockIndexPacking"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.
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.
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:
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.
No verified findings are available from this review.
Review coverage was incomplete.
Reviewed:
14edee63b643· Full review report · Previous reportspermalink
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
d9387c41c1lgtm ACK
d9387c41c1I disagree with the risk assessment. Also,
CDiskBlockIndexderives fromCBlockIndexand is serialized. The change is safe because serialization is field by name, not because it does not happen.CBlockIndexis 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:memcpy,reinterpret_cast, etcI checked these myself, including clang-query over all translation units for casts,
mem*calls andsizeof/offsetofon 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.
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.
Can you rebase this to include the latest changes to
CBlockIndex?I have. Now we are going from 160 to 152 bytes
ACK
14edee63b6The rebase adjusted the
nDataPos/nUndoPossizes from https://github.com/bitcoin/bitcoin/pull/36669#discussion_r4387377304According 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:
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
lgtm ACK
14edee63b6Only change is rebase
Opened #36819 looking for opinions in regards to future changes / keeping tabs on regressions.
Risk seems very low, but benefit also seems modest and uneven, if I understand correctly:
The change helps Windows nodes and any future allocator that does not round the relevant size classes as aggressively.
Hesitant ACK
14edee63b6Whether this change is worth it is an open question.
ACK
14edee63b6I think this is fine. The risk here seems a bit overstated to me.
ACK
14edee63b6Boss man dei8 I'm devoloper