-
Notifications
You must be signed in to change notification settings - Fork 45
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
Option to replace initial mDNS and DevAtt post-construction #193
Conversation
Let me elaborate a bit on the background, as it might still be not so obvious as to what exactly is going on here: In my opinion, Rust currently has two main challenges when trying to fit it on devices which have small amount of RAM (like MCUs): Autogenerated futures end up a bit too largeThe futures generated by the No placement-new C++-like constructRust does not have constructors in the C++ sense. It just uses "constructor functions" which is merely a convention for any associated function which contains the The problem with construction functions is that - putting aside any optimizations - the value is first constructed on-stack, and only after that it is moved to its final location (be it still on-stack, or on-heap or others). The problem with these moves induced by Rust construction-functions (and in fact with any function in Rust that returns a large result, but this is usually the constructor functions) is that - with large objects - you are risking a stack blowup, if the stack of the thread is smaller than the object you are truing to return (or to place elsewhere, like in a The above situation is typical on MCUs where threads are usually running with just a few kilobytes of RAM (or max being a few tens of kilobytes). There are three workarounds w.r.t. stack blowups I'm aware of: Allocate staticallyThis is a very simple method which works 100% of the time. // This `const` is generated at compile-time and is put by the compiler
// in the `rodata` segment; `rodata` on MCUs means flash memory.
// I.e. the constant takes up firmware space, but no SRAM memory space
const FOO_CONST: Foo = Foo::new();
/// What the compiler does here is - it instructs the loader -
// when the `rwdata` segment is formed - to take the memory of
// `FOO_CONST` and `memcpy` it into RAM. I.e. no moves at all,
// and the static comes up pre-initialized even before the program
// had started.
static FOO: Foo = FOO_CONST; (There are variations of this scheme which are useful when e.g. Anyway, the key to this approach is that as long as the object is Make sure that rustc optimizes away the moveThis is not a real workaround in that it simply cannot be guaranteed in all circumstances and under all opt settings, except when the returned object is of type Something which also triggers almost always is this: const FOO: Foo = Foo::new();
let mut foo = Box::new_uninit(); // Or any other function that returns `MaybeUninit`. As per above, this is guaranteed to be optimized
let foo = unsafe {
obj.as_mut_ptr().write($val);
obj.assume_init()
}; ... but the above ^^^ requires that Invent
|
Great pointers to RFCs and efforts for some form of ways to address this in Rust |
There is a big comment above the newly-introduced
replace_mdns
utility method that explains the issue at hand.I have this problem in actual real-life code, hence pushing for it. :) Not a fictitious problem.