Shrink CAddress from 48 to 40 bytes on x64 #19705

pull vasild wants to merge 1 commits into bitcoin:master from vasild:shrink_caddress changing 1 files +2 −1
  1. vasild commented at 2:19 pm on August 12, 2020: member

    CAddress inherits CService which is 28 bytes (on 64 bit machines). CAddress then adds two member variables - one that requires 4 byte alignment (nTime) and one that requires 8 byte alignment (nServices).

    Declare the smaller one first so that it fits in bytes 29..32.

    On 32 bit machines this change has no effect and CAddress remains 40 bytes.

  2. Shrink CAddress from 48 to 40 bytes on x64
    `CAddress` inherits `CService` which is 28 bytes (on 64 bit machines).
    `CAddress` then adds two member variables - one that requires 4 byte
    alignment (`nTime`) and one that requires 8 byte alignment
    (`nServices`).
    
    Declare the smaller one first so that it fits in bytes 29..32.
    
    On 32 bit machines this change has no effect and `CAddress` remains 40
    bytes.
    767073fb96
  3. vasild commented at 2:20 pm on August 12, 2020: member
    I verified the size of CAddress on all Travis platforms - pushed a patch that prints the sizes of CAddress before and after the change. One result, all results
  4. laanwj commented at 2:46 pm on August 12, 2020: member

    I guess we store enough addresses in e.g. addrman for this to matter substantially? Might want to add a comment on why this specific ordering of fields is used.

    Edit: How does this interact with #19628?

  5. DrahtBot added the label P2P on Aug 12, 2020
  6. vasild commented at 3:46 pm on August 12, 2020: member

    I guess we store enough addresses in e.g. addrman for this to matter substantially?

    I guess this will reduce the memory footprint by a few 100s of KiB. I am not sure if this qualifies for “substantially” but the change is simple enough and not risky.

    Might want to add a comment on why this specific ordering of fields is used.

    Isn’t that too low level and basic to be mentioned? Like we don’t comment every ++it why it was preferred over it++. Maybe also other structs would warrant such comments? I think that every time the order of data members in a struct is considered, padding should be taken into account. But anyway, this is subjective, I will add a comment about it if you feel it is needed.

    Edit: How does this interact with #19628?

    No conflicts, either one can go first without disturbing the other one. Size reduced from 56 to 48 bytes.

  7. laanwj commented at 4:52 pm on August 12, 2020: member

    Isn’t that too low level and basic to be mentioned? Like we don’t comment every ++it why it was preferred over it++. Maybe also other structs would warrant such comments? I think that every time the order of data members in a struct is considered, padding should be taken into account. But anyway, this is subjective, I will add a comment about it if you feel it is needed.

    I agree in that case. But I wouldn’t exactly say this is obvious. Normally to create compact structures the preferred ordering is to have larger alignment fields first, then smaller one. The reason it’s different here is because of the inheritance.

    No conflicts, either one can go first without disturbing the other one. Size reduced from 56 to 48 bytes.

    Thanks!

  8. theStack approved
  9. theStack commented at 0:30 am on August 13, 2020: member

    Nice little memory-saving change. Verified that both on master branch and PR #19628 putting nServices after nTime in class CAddress reduces its size by 8 bytes. Running a x86_64 machine with gcc 5.4.0 here.

    ACK https://github.com/bitcoin/bitcoin/commit/767073fb9645f5cb0976a14288c03fe71912b569

  10. luke-jr commented at 6:12 pm on August 14, 2020: member
    x86-64*
  11. kristapsk commented at 3:10 pm on August 15, 2020: contributor
    Is this really x86-64 specific, doesn’t it reduce CAddress size on other 64-bit archs, like aarch64 or riscv64, too?
  12. laanwj commented at 3:15 pm on August 16, 2020: member

    Is this really x86-64 specific, doesn’t it reduce CAddress size on other 64-bit archs, like aarch64 or riscv64, too?

    I’d expect this is the same for every architecture that requires 64-bit values to be aligned to 64-bit.

    So practically for every 64-bit architecture.

  13. laanwj commented at 4:44 pm on August 16, 2020: member

    rv64:

    • without PR: 48
    • with PR: 40

    ACK 767073fb9645f5cb0976a14288c03fe71912b569

  14. laanwj merged this on Aug 16, 2020
  15. laanwj closed this on Aug 16, 2020

  16. sidhujag referenced this in commit 3bd8624932 on Aug 17, 2020
  17. vasild deleted the branch on Aug 25, 2020
  18. DrahtBot locked this on Nov 2, 2022

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: 2024-07-03 10:13 UTC

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