Skip to content

Embassy adapter: eliminate the unconditional clone in Mailbox push #186

Description

@lxsaah

The Embassy adapter's Mailbox push clones the value on every push, even
when the slot is free and the first try_send would succeed
(aimdb-embassy-adapter/src/buffer.rs):

EmbassyBufferInner::Mailbox(channel) => {
    // Try to send, if full, clear and try again (overwrite semantic)
    let sender = channel.sender();
    if sender.try_send(value.clone()).is_err() {
        let _ = channel.try_receive();
        let _ = sender.try_send(value);
    }
}

The clone is unnecessary: embassy_sync's try_send returns the message
back on failure — Err(TrySendError::Full(value)) — so the value can be
recovered from the error and reused for the retry. On an MCU, a per-push
clone of the payload type is real cost on the hot path.

Where to look

  • aimdb-embassy-adapter/src/buffer.rs — the Mailbox arm of push
  • embassy_sync::channel::TrySendError — the error variant carries the
    rejected message
  • aimdb-core/src/buffer/test_support.rs — the shared contract suite that
    pins the overwrite behavior across adapters

What to do

  • Match on Err(TrySendError::Full(v)), drain the stale message with
    try_receive(), and resend v — no clone anywhere in the path.
  • Behavior must stay observably identical (single-slot overwrite, latest
    value wins); this is a pure cost fix, not a semantics change.
  • The T: Clone bound itself stays — the other buffer variants still need
    it. Only the clone() call in this path disappears.

Done when

  • No clone() in the Mailbox push path
  • cargo test -p aimdb-embassy-adapter (contract suite included) is green
  • The embedded cross-compile check passes
    (cargo check --target thumbv7em-none-eabihf) and make check is green

Getting set up

CONTRIBUTING.md
covers prerequisites and the development setup. For the embedded check you
will need the cross-compile target installed:
rustup target add thumbv7em-none-eabihf.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions