Hacker News new | ask | show | jobs
by fusslo 957 days ago
Rust looks nicer and nicer. Is anyone familiar with RAM/Memory requirements as compared to c?

Every microcontroller project I've worked on, as we approach maturity, goes through a round of freeing up ram and code space. Usually deleting strings, removing debug functionality, or shortening datatypes.. etc

Can I write rust code with the same footprint as c code?

4 comments

My employer does embedded Rust. We built our own little OS, Hubris, as the foundation of those projects. A no-op task in Hubris ends up at like 100 bytes, last I measured. https://hubris.oxide.computer/

You still have to like, actively think about binary size in order to get things to be that small. But the smallest x86_64 binary rustc has ever produced is 137 bytes. How small your program is up to you, not Rust.

EDIT: Oh yeah, I left a similar comment with more details about a year ago: https://news.ycombinator.com/item?id=34032824

You might enjoy this blog post: https://darkcoding.net/software/a-very-small-rust-binary-ind...

It illustrates the steps to take Rust from 3.6MiB executable to 400 bytes, by stripping more and more things away, and changing things.

Also see min-sized-rust repo[1], which also has this blog post at the bottom (along with others)

[1] https://github.com/johnthagen/min-sized-rust

thank you! I am currently enjoying it
You definitely can use rust in an embedded space. I rather liked having traits and proper types instead of a giant mess of integer constants. In general I think a lot of the so-called bloat you see with Rust binaries is due to a combination of not using a shared standard library and generics – the former is a non-issue in an embedded context and the latter is well under your control.

Sure, ELF includes a lot of fluff but you're not deploying ELF on a microcontroller.

yeah I think I just need to write some stuff to figure it out.

I was mostly trying to figure a 1:1 comparison. For example, If I write a Feature Control module in c and in rust, using the same design, are outputs similar?

seems like either no one has a good sense of that comparison, or it's a bad comparison and I don't understand why.

What I'm trying to avoid is having a space-saving task be "rewrite rust module X in c to save code memory"

IMO it would be good to familiarize yourself with the rust quirks/features (check out the rust book) and then poke at some of the embedded specific groups. Once you get a handle on the common types and patterns it's probably easier to find the information you're looking for, e.g.:

https://github.com/rust-lang/rust/issues/46213

In general Rust chases a "zero cost abstraction" ethos, so a lot of the type system niceties have no code or memory cost because the heavy lifting is done at compile time, not run time. For instance using specific traits for each GPIO pin ensures you won't accidentally apply some meaning meant for another pin, but the compiler will compile it right down to integers anyhow.

Things like options (how Rust deals with "null" values) are enums and usually incur a one byte penalty, but in some cases they can be compiled down to no overhead at all.

> it's a bad comparison and I don't understand why

Different languages are different, and so it's tough to compare. You don't generally write Rust code in the same way you write C, and so a "using the same design" constraint on a comparison means that some folks will not consider it to be an accurate one.

In general, similar constructs have the same amount of overhead, that is, zero. Rust has additional features that are useful that can increase code size if you're not careful, but getting that back down doesn't necessarily mean abandoning Rust, but it may lead to "write the Rust closer to C." I am thinking of generics specifically here, monomorphization can blow up your code size, but you could either not use generics or use a few techniques to minimize it.

I used Rust for my latest embedded project based on an MSP430 from Texas Instruments. The specific model has 128 bytes of ram, and it fits there quite neatly. Same for the flash.

This was quite the great experience!

That’s 128 KB, right?
Nope, the B was not a typo. It has 128 bytes of RAM.

The specific model is an MSP430G2231 [1] with 128 Bytes of RAM and 2kB of flash.

1: https://www.ti.com/lit/ds/symlink/msp430g2231.pdf

In the MSP430 family? Not likely.