-
Notifications
You must be signed in to change notification settings - Fork 168
[RFC] #[uninit]
for truly uninitialized static
variables
#398
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
Comments
I guess I'm partial on this one as written down. On one hand it sounds like a useful feature to have, even if it just improves the ergonomics of static variables which is quite a boon. OTOH I don't buy into the expense argument of the zeroing; if wasting a few cycles once during initialisation is the biggest problem of your design you can be very proud of yourself... I'd rather highlight the ergonomics benefit than the slight performance improvement and by doing that your drawback also vanishes into thin air because it's actually a pro that you cannot abuse this to do partial initialisation. Regarding syntax, I'd very much prefer the unit type: |
+1 on @therealprof for me. The zeroing does not convince me, though ergonomics is indeed a good plus. |
that contains static variables that will not be initialized by the runtime this implements only the linker section described in RFC #116 so that downstream libraries / framework can leverage it without (a) forking this crate or (b) requiring the end user to pass additional linker scripts to the linker when building their crate. This commit doesn't add the user interface described in RFC #116; instead it documents how to use this linker section in the advanced section of the crate level documentation
192: add a .uninit section r=therealprof a=japaric that contains static variables that will not be initialized by the runtime this implements only the linker section described in RFC #116 so that downstream libraries / framework can leverage it without (a) forking this crate or (b) requiring the end user to pass additional linker scripts to the linker when building their crate. This commit doesn't add the user interface described in RFC #116; instead it documents how to use this linker section in the advanced section of the crate level documentation Co-authored-by: Jorge Aparicio <jorge@japaric.io>
Was this RFC accepted or not? Churning out the implementation for this should go fast. |
I needed to brush up my proc macro knowledge, so I made a simple impl to do the expansion and type signature checking: https://github.com/rust-embedded/cortex-m-rt/tree/uninit_impl |
I'm not terribly excited about having yet another way in which the user-specified type gets changed to something else by a macro. IMO we should require an Also, the |
Maybe you will find this article interesting: |
Summary
The
#[uninit]
attribute will placestatic [mut]
variables into an.uninit
section, located in RAM, that won't be initialized before
main
.Motivation
Today, uninitialized (see
MaybeUninit
)static
variables will be placed in.bss
, which means that'll be zeroed beforemain
. This leads to unnecessarywork as leaving the memory uninitialized was the intended behavior.
Design
The
#[uninit]
attribute will have the following syntax.The initial value of
uninit
variables must be the placeholder{}
. Thisattribute will expand into:
#[uninit]
composes with#[entry]
and#[exception]
; it can be used onsafe local
static mut
variables.Implementation
Before this can be implemented,
MaybeUninit
must first land in the corelibrary. This feature will live behind a "nightly" (Cargo) feature until
MaybeUninit
and its const constructor are stabilized.Alternative syntax
Option A
Option B
We can't omit the RHS of
static mut
because then the code won't parse and thecompiler will never reach the macro expansion phase.
Drawbacks
This is not a perfect solution.
For example, a
heapless
vector contains an uninitialized buffer when it'sconstructed using the
new
method but it also contains a length field that mustbe initialized to zero. Applying
#[uninit]
to such data structure means thatthe vector will have to be initialized (assign 0 to its length field) at
runtime, which is not ergonomic. Not using
#[uninit]
means that the vectorlength and its buffer will be zeroed, which is wasteful.
It's not possible to have partial initialization of
static
variables so thisis as good as it gets.
The text was updated successfully, but these errors were encountered: