-
Notifications
You must be signed in to change notification settings - Fork 181
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
Packing Rhai into 128KB under no-std on STM32 #791
Comments
That is strange as the exact same command line passed CI... The I'll look into it. Perhaps a new version of In the meantime you can specially include |
The standard Rhai with all libraries is too large to fit inside 128KB. You'd want to use a minimal build. Check this out: https://rhai.rs/book/start/builds/minimal.html |
Well, did you use Otherwise you'd be pulling in the entire standard library which will be very large. |
I would also say that, if you want to keep arrays and maps, it would probably be difficult to fit it inside 128KB, possibly together with your own code as well... |
Hhhmmm... No idea. I'm not an embedded programmer myself... Maybe you can search on the net... Probably you'll need a custom allocator that can allocate from specific locations. BTW l think I'd also need to turn off memory-heavy stuff like strings interning and function resolution caching... Maybe I'll add that into Rhai. |
As I mentioned, it is quite difficult to fit inside 64K if you don't take out language features. I remember some user successfully packed it under 64K, but he had to disable arrays and objects support ( If you want to pack under 128K, then I think it is possible... Are you sure you're making a release build with optimization for size? Also have you |
Have you stripped the binary? |
Try to do a |
Just curious how the status is. Did you succeed to squeeze it down further? |
another curious user here 😄 |
Since it seems like folks are still curious, here are my initial results on a Cortex-M7 (substantially similar to the M4). I'm not sure what the original poster was trying to do and whether this will be at all helpful to them, since they're asking about code size and then highlighting an SRAM, which is not typically where code lives on these parts.
FWIW stripping the binary doesn't help it go into flash, in general, because these systems don't get the debug symbols in flash. Only the text and data initialization image is actually written. All measurements are taken at Default no_std build: 421,692 bytes Turning on size reduction features individually:
Turning them all on gets the build down to 148,876, though it produces a language that doesn't meet my needs (no functions, for instance). Turning back off only Adding Both get significantly smaller if you enable I looked into what was responsible for the significant size increase when adding
I haven't looked at the code generator, but this sort of thing is pretty common in programs that haven't been written with text size in mind -- my guess is that you've got a code generator producing these All in all, So, the system as it stands can fit into larger STM32 parts (I'm building these tests for the STM32H753 with 1MiB of flash) but the codebase doesn't appear to have been written with size (or startup time) in mind. (Which is fine! You haven't claimed otherwise. But I wanted to post these numbers for the next person who tries to fit this into a small microcontroller.) In case you're curious, here's the test program. I derived it from the no_std example. #![no_std]
#![no_main]
extern crate alloc;
use core::{mem::MaybeUninit, ptr::addr_of};
use panic_halt as _;
use rhai::{packages::Package, Engine, INT};
use stm32_metapac as _;
use embedded_alloc::Heap;
#[global_allocator]
static HEAP: Heap = Heap::empty();
#[cortex_m_rt::entry]
fn main() -> ! {
{
const HEAP_SIZE: usize = 16384;
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe {
HEAP.init(addr_of!(HEAP_MEM) as usize, HEAP_SIZE);
}
}
let mut engine = Engine::new_raw();
// this bit gets commented out to test size without Core
let std = rhai::packages::CorePackage::new();
std.register_into_engine(&mut engine);
loop {
// Evaluate a simple expression: 40 + 2
let _ = engine.eval_expression::<INT>("40 + 2").unwrap() as isize;
cortex_m::asm::nop();
}
} |
That's a very interesting observation!
You're absolutely correct. That's what the code generator does: generates a bunch of individual function registration calls. And yes, they most probably can go into a table instead... I'll experiment with that and report back. |
Yeah, I'm specifically looking at scripting options for doing embedded handling of arrays of bytes. I felt like |
There is a builtin data type called I write device drivers with Rhai so I added that into Rhai many versions ago. Give that a spin. |
error:
The text was updated successfully, but these errors were encountered: