New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Box value encoded in a variant to reduce enum stack space #996
Conversation
before ``` print-type-size type: `util::psbt::error::Error`: 120 bytes, alignment: 8 bytes print-type-size discriminant: 1 bytes print-type-size variant `CombineInconsistentKeySources`: 115 bytes print-type-size padding: 3 bytes print-type-size field `.0`: 112 bytes, alignment: 4 bytes print-type-size variant `InvalidKey`: 39 bytes print-type-size padding: 7 bytes print-type-size field `.0`: 32 bytes, alignment: 8 bytes ``` after ``` print-type-size type: `util::psbt::error::Error`: 40 bytes, alignment: 8 bytes print-type-size discriminant: 1 bytes print-type-size variant `InvalidKey`: 39 bytes print-type-size padding: 7 bytes print-type-size field `.0`: 32 bytes, alignment: 8 bytes print-type-size variant `DuplicateKey`: 39 bytes print-type-size padding: 7 bytes print-type-size field `.0`: 32 bytes, alignment: 8 bytes ```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 9906cea
Interesting PR, I rarely think about the stack space of error enums. To educate me, can you please explain a few things if/when you have the time:
No rush, just if you have time. Thanks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 9906cea
In general, Boxing is something that should be avoided - it adds some overhead by waiting on |
I see, paraphrasing to make sure I get you, the So if we want to be particularly careful with stack usage we'd have to check the relation between And we may want to be careful with stack usage as we endevour to support smaller devices (WASM, no-std, ect.)? |
I don't think WASM qualifies as a "smaller device" here, more tiny embedded things. I think we don't need to be universal about this, only parts of the library that are usable on embedded devices. e.g. no embedded (memory < 1 MiB) device is going to decode a block, as the whole thing is bigger than the embedded device's memory anyway, same goes for P2P messages, etc. For PSBT and signing transactions, on the other hand, this is the exact code that an embedded device may care about. |
Cool, so any error path that can be hit from PSBT code, that's a non-trivial amount of work to find out, right? Follow every type used in the |
Yea, actually no idea - is there a concrete use-case where this is important @RCasatta? |
@RCasatta, I am curious how do you print byte alignments? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK 9906cea
RUSTFLAGS=-Zprint-type-sizes cargo +nightly build --release
No concrete use-case in this case for me, I just spotted this big variant by looking at the aforementioned program. There are some examples linked from this page https://nnethercote.github.io/perf-book/type-sizes.html?highlight=print#measuring-type-sizes where changes like this make performance impact |
Specter Wallet has indicated a desire to use rust-miniscript (and therefore rust-bitcoin) in embedded contexts. I take this pretty seriously. Maybe we have no actual users, but I think that's probably because rust-bitcoin can't be used rather than that it isn't used. So anything that improves the viability of rust-bitcoin-on-embedded is good by me. Regarding the reasoning for this, Matt's summary sounds great to me. (a) generally we should try not to have big error enums, (b) PSBT is both non-speed-critical and likely to be used on a hardware wallet, so let's try to be nice with stack space. |
…o reduce enum stack space 9906cea Box value encoded in a variant to reduce enum stack space (Riccardo Casatta) Pull request description: before ``` print-type-size type: `util::psbt::error::Error`: 120 bytes, alignment: 8 bytes print-type-size discriminant: 1 bytes print-type-size variant `CombineInconsistentKeySources`: 115 bytes print-type-size padding: 3 bytes print-type-size field `.0`: 112 bytes, alignment: 4 bytes print-type-size variant `InvalidKey`: 39 bytes print-type-size padding: 7 bytes print-type-size field `.0`: 32 bytes, alignment: 8 bytes ``` after ``` print-type-size type: `util::psbt::error::Error`: 40 bytes, alignment: 8 bytes print-type-size discriminant: 1 bytes print-type-size variant `InvalidKey`: 39 bytes print-type-size padding: 7 bytes print-type-size field `.0`: 32 bytes, alignment: 8 bytes print-type-size variant `DuplicateKey`: 39 bytes print-type-size padding: 7 bytes print-type-size field `.0`: 32 bytes, alignment: 8 bytes ``` `util::psbt::error::Error` is wrapped also in `consensus::encode::Error` and stack savings are gained there also ACKs for top commit: apoelstra: ACK 9906cea tcharding: ACK 9906cea sanket1729: utACK 9906cea Tree-SHA512: e03988fcbc3dd87f83d00dd84ec1c538bc5c63bea97ff4a69a715621f498f57d7fe2a623e351942d9532af40c723e42a9eb6ef48ebf4c62ddf5c0f44e9ea0a07
before
after
util::psbt::error::Error
is wrapped also inconsensus::encode::Error
and stack savings are gained there also