-
Notifications
You must be signed in to change notification settings - Fork 152
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
[RFC] initialize singletons at runtime #81
Conversation
@japaric Hm, this is weird. I actually don't see a Even more curious than that is that with the 0.4.2 code the initialisation data ends up in the So as I see it the only real overhead is the inclusion of all the One nice thing is that the new version allows to write:
(without unsafe!) and that'll also do away with the Oh, and if you do something like:
rustc will happily generate lots of assembly calls to generate the values in registers and store them in the right spot, i.e. completely without
but it will still put the appropriate amount of 0s in the
Anywhoo, I like the new version a lot better. |
Oh, and the syntax mentioned above
doesn't work for me:
It works without the underscore though. |
Right, that makes sense. Since the initial values is all zeros.
Err, that's wrong. You should require unsafe. I'll fix that. |
Hmm, additional flash usage is a problem, but being able to use non-const values is neat. I have the feeling which I prefer will always depend on the situation. Anyway, I don't feel strongly about this. If I ever need to save those last few bytes to fit something into flash, I'll figure something out. |
I don't see any additional flash usage compared to other approaches. The |
@therealprof Everything's great, then. Thanks for the clarification! |
OK. Let's land this. Thanks for the feedback! @homunkulus r+ |
📌 Commit 032af4b has been approved by |
[RFC] initialize singletons at runtime This PR changes how the singletons are initialized. The advantage of initializing a singleton at runtime is that the initial value is not constrained to only what works in const context. The disadvantage is that this approach will use up more Flash. With the new approach, `singleton!(_: [u8; 1024] = [0; 1024])` will invoke a memcpy at the caller site; with the old approach, the array would have been initialized (zeroed) during startup. The following code works with the new approach, but doesn't with the old one. ``` rust let x = 0; let y = singleton!(_: u32 = x); ``` cc @therealprof @hannobraun
☀️ Test successful - status-travis |
81: Add a __pre_init function to be called at the start of the reset handler r=korken89 a=yodaldevoid I needed this for a project so I went ahead and rebased and tweaked @jcsoo 's changes from #71. I will admit, I got a little impatient waiting and that also played into why I did this. If either @jcsoo or @japaric have an issue with this PR, please let me know and I will remove it. I apologize for toes that I have stepped on. Now onto the PR. This is possibly the simplest implementation that is possible. No nightly features were used in this implementation. Also, there is no hand-holding for the end user; if they want to mess with uninitialized statics, that is on them. If you would like me to add more documentation, please let me know what you would like to see. Fixes #17 Co-authored-by: Jonathan Soo <jcsoo@agora.com> Co-authored-by: Gabriel Smith <ga29smith@gmail.com>
This PR changes how the singletons are initialized. The advantage of initializing a singleton at
runtime is that the initial value is not constrained to only what works in const context. The
disadvantage is that this approach will use up more Flash. With the new approach,
singleton!(_: [u8; 1024] = [0; 1024])
will invoke a memcpy at the caller site; with the old approach, the arraywould have been initialized (zeroed) during startup.
The following code works with the new approach, but doesn't with the old one.
cc @therealprof @hannobraun