|
|
Subscribe / Log in / New account

shared memory and loading savings

shared memory and loading savings

Posted Jan 31, 2025 19:38 UTC (Fri) by pizza (subscriber, #46)
In reply to: shared memory and loading savings by mb
Parent article: Vendoring Go packages by default in Fedora

> Rust can be used on extremely small systems such as AVR8 with 128 bytes of RAM total.

Rust-the-language may be used, but most existing Rust code (including most crates) won't be useful on targets that limited.


to post comments

shared memory and loading savings

Posted Jan 31, 2025 20:34 UTC (Fri) by Cyberax (✭ supporter ✭, #52523) [Link] (2 responses)

There are actually crates that are specifically designed for such environments. A "no allocation" filter is a good way to do it: https://crates.io/categories/no-std::no-alloc

A lot of crates like serde that are not affected at all.

shared memory and loading savings

Posted Jan 31, 2025 21:47 UTC (Fri) by excors (subscriber, #95769) [Link] (1 responses)

Wouldn't you also have to be extremely careful to keep stack usage to approximately zero, which is a big step beyond merely not using a heap? I expect almost none of the no-alloc crates are designed with that constraint.

When you're trying to strictly limit stack usage, I'm not sure Rust is a good fit - something simple like assigning to a static struct will conceputally construct a temporary value on the stack then memcpy it into the static, and you're relying on the optimiser to turn that into direct writes to the static's memory, and sometimes the optimiser won't cooperate and you'll overflow your tiny stack.

If I remember correctly, I tried this a while ago and it stopped optimising away the stack allocation when the struct size exceeded maybe 8KB. If you neatly contain all your application state in one big struct that's more than half your RAM, and try to dynamically initialise it, that's a problem. You have to write ugly, unidiomatic code and then hope the optimiser cooperates. It's easier to avoid implicit stack allocations in C, and in such a small program with no dynamic allocation and no threading you don't need the memory safety benefits of Rust, so it doesn't seem a good tradeoff.

(By the time you get up to, say, 256KB of RAM, you're not going to be accounting so carefully for every byte and you'll probably have a real heap and an RTOS, and then I think the tradeoff flips the other way and Rust is much more appropriate.)

shared memory and loading savings

Posted Jan 31, 2025 21:57 UTC (Fri) by mb (subscriber, #50428) [Link]

>When you're trying to strictly limit stack usage, I'm not sure Rust is a good fit

As I said.
128 bytes RAM total work just fine.
This is what actually works in real life.

>something simple like assigning to a static struct

Assigning to a static struct is not simple. Neither in C nor in Rust.
But anyway, it has to grow *much* larger than anything possible with 128 bytes of RAM to become a memcpy problem.
I have seen this problem with multi-megabyte ESP32. And then the solution is just to use heap allocation. Which is not a real problem on ESP32.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds