top | item 38945900

(no title)

boustrophedon | 2 years ago

Without specifying a particular representation[0] on the struct you want to put in "permanent memory", this seems liable to break any time you recompile the code with different optimization flags, or with a newer or older compiler that may optimize differently, or even if you just change seemingly unrelated code that then triggers or de-triggers some optimization.

[0] https://doc.rust-lang.org/reference/type-layout.html#represe...

discuss

order

conradev|2 years ago

The rkyv crate has stable in-memory representations for many types in the Rust standard library: https://rkyv.org/

I feel like it is the missing piece here because you could do hash table lookups in shared memory, too!

WirelessGigabit|2 years ago

Yup, at a minimum they should apply #[repr(C)] to avoid this optimization.

o11c|2 years ago

Worse, it breaks all pointers since you can't guarantee `mmap` can give you the same address.

kouteiheika|2 years ago

You absolutely can, with `MAP_FIXED`. But it's not trivial to do because you'd have to handle the case where the rtld and/or the kernel has already put something in memory where you want to put down your map. So it'd be tricky to implement, but definitely possible.

astrange|2 years ago

I think you could fixup the pointers if you tried. It's more or less the same problem as compacting GC.

junon|2 years ago

You can detect when it doesn't, though, I think. If memory serves you can request a specific memory address to mount it at and it'll do it if it's not already mapped. It should (in theory?) return an error in such a case, no?

graham_king_3|2 years ago

Good point, thank you! I added `repr(C)`.