Skip to content

Add single-word fast path to RegisterIO for zero-cost accesses - #18

Merged
darsor merged 1 commit into
darsor:mainfrom
joecrop:inline-single-word-register-io
Jul 11, 2026
Merged

darsor merged 1 commit into
darsor:mainfrom
joecrop:inline-single-word-register-io

Conversation

@joecrop

@joecrop joecrop commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

Add a num_subwords == 1 fast path to both methods and mark them (and the Endian impl methods) #[inline(always)]. num_subwords is a compile-time constant derived from size_of, so for single-word registers the multi-word loop is dropped entirely and the access folds to a single instruction at the call site. The loop is retained as the fallback for multi-word registers, so behavior is unchanged (the fast path is the loop's single-iteration case).

Measured ~10% .text reduction on a register-heavy RISC-V (riscv32imc) -Oz firmware (72.5 KiB -> 64.8 KiB of .text+.text_high).

`RegisterIO::try_read_register`/`try_write_register` always ran the generic
multi-word subword loop (map/try_fold and a `for` loop). Even for single-word
registers — the common case — that loop prevented the access from folding to a
single volatile load/store, so each register access compiled to a call into a
monomorphized helper rather than one instruction.

Add a `num_subwords == 1` fast path to both methods. `num_subwords` is a
compile-time constant derived from `size_of`, so for single-word registers the
multi-word loop is dropped entirely and the access folds to a single instruction
at the call site. The loop is retained as the fallback for multi-word registers,
so behavior is unchanged (the fast path is the loop's single-iteration case).

Measured ~10% `.text` reduction on a register-heavy RISC-V (riscv32imc) `-Oz`
firmware (72.5 KiB -> 64.6 KiB of .text+.text_high). No `#[inline]` hints are
needed: the small fast-path bodies are inlined by normal optimization.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@joecrop
joecrop force-pushed the inline-single-word-register-io branch from 30ad9bd to 4076f3f Compare June 17, 2026 22:11
@darsor

darsor commented Jul 11, 2026

Copy link
Copy Markdown
Owner

Sorry it took me a while to get to this. I tested on my own project and saw a .text reduction of about 2 kB, verifying that the try_read_register functions were inlined to a couple instructions.

Thanks for figuring this out!

@darsor
darsor merged commit f02e3a3 into darsor:main Jul 11, 2026
24 checks passed
@darsor

darsor commented Jul 12, 2026

Copy link
Copy Markdown
Owner

This is now available with the peakrdl-rust crate version 0.2.2.

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