Repository navigation
Make new_with_storage a const fn - #136
Open
qwerty19106 wants to merge 1 commit into
Open
qwerty19106 wants to merge 1 commit into
qwerty19106 wants to merge 1 commit into
Conversation
`BBQueue::new()` covers storage that can initialize itself, which today means `Inline<N>`: the capacity lives in the queue's type. A `Storage` that points at a buffer declared elsewhere cannot implement `ConstInit` — there is no single value that could point at every buffer — so it has no way into a `static`, because `new_with_storage` is not `const`. The body only moves the storage in and reads two associated constants, so `const` costs nothing and requires no other change. The test shows what it buys: queues of 64 and 4096 bytes built in `static`s share one concrete type and sit in the same array, so a lookup reaches them by reference instead of through `dyn` or a function pointer. Assisted-by: Claude Opus 5 (Anthropic) Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
BBQueue::new()covers storage that can initialize itself, which today meansInline<N>: the capacity lives in the queue's type. AStoragethat points ata buffer declared elsewhere cannot implement
ConstInit— there is no singlevalue that could point at every buffer — so it has no way into a
static,because
new_with_storageis notconst.The body only moves the storage in and reads two associated constants, so
constcosts nothing and requires no other change.The test shows what it buys: queues of 64 and 4096 bytes built in
staticsshare one concrete type and sit in the same array, so a lookup reaches them by
reference instead of through
dynor a function pointer.Where this comes from
A
defmtlogger for latency-critical firmware, where a log call may notdisable interrupts or block. It keeps one framed queue per execution context —
thread mode, plus one per interrupt-driven executor — each sized for how much
that context actually logs: 8 KiB for one, 4 KiB for the others.
Different capacities mean different types, so the dispatch table cannot hold
the queues themselves and holds erased function pointers instead. On a
Cortex-M7 at 400 MHz that costs, per log frame:
into the logger's entry point — 8 registers pushed and popped;
Measured with DWT CYCCNT: 50 cycles out of a 193-cycle log call.
There is a way to get this today — give every queue the same capacity, so the
types collapse. It trades RAM for it: 24 KiB instead of 16 KiB in that
firmware, and the gap grows with the number of contexts. With this patch
neither per-queue sizing nor the direct call has to be given up.
Checks
cargo test --features=std, boththumbv6m-none-eabibuilds frombuild.yml, andmiri.sh— the new test uses raw pointers, so it is includedunder miri:
Disclosure: this patch was written with AI assistance (Claude), reviewed and
tested by me. The commit carries an
Assisted-by:trailer. Happy to drop thetest or trim it to a doc example if you would rather keep the change to the
single word.