Skip to content

rust: box some futures in callers#1945

Open
benma wants to merge 2 commits into
BitBoxSwiss:masterfrom
benma:boxfut
Open

rust: box some futures in callers#1945
benma wants to merge 2 commits into
BitBoxSwiss:masterfrom
benma:boxfut

Conversation

@benma
Copy link
Copy Markdown
Collaborator

@benma benma commented Apr 21, 2026

Reduces binary size by ~1360b.

async fn lowers to a concrete state-machine type. In a function like signtx::_process() in src/rust/bitbox02-rust/src/hww/api/bitcoin/signtx.rs, every directly-awaited child future becomes part of that parent
future’s stored state. If the child is large, the parent future gets larger too, and its generated poll() logic has to handle that larger layout.

Box::pin(child()).await changes the shape of the parent future:

  • The child future is still a concrete type.
  • But the parent no longer stores that full child state inline.
  • It stores a boxed pointer instead.
  • So the parent future becomes smaller and simpler.

@benma benma requested a review from NickeZ April 21, 2026 19:57
benma added 2 commits April 22, 2026 22:15
Reduces binary size by ~1360b.

  async fn lowers to a concrete state-machine type. In a function like signtx::_process() in src/rust/bitbox02-rust/src/hww/api/bitcoin/signtx.rs, every directly-awaited child future becomes part of that parent
  future’s stored state. If the child is large, the parent future gets larger too, and its generated poll() logic has to handle that larger layout.

  Box::pin(child()).await changes the shape of the parent future:

  - The child future is still a concrete type.
  - But the parent no longer stores that full child state inline.
  - It stores a boxed pointer instead.
  - So the parent future becomes smaller and simpler.
The top-level HWW API dispatch function builds one large async state
machine that has to cover every request variant. That keeps all of the
branch-specific future layouts live in a single monomorphized type and
costs a noticeable amount of ROM.

Change the top-level dispatch helpers to return boxed futures and box
each branch individually. This moves the size cost from code generation
into a small runtime allocation at dispatch time, while leaving the API
surface and request handling behavior unchanged.

Saves 1832 bytes in firmware.bin.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants