Volatile copy (again)

⚓ Rust    📅 2025-11-26    👤 surdeus    👁️ 8      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Volatile copy (again)

I am looking for a way to perform a what I though a simple operation - read an arbitrary typed value from "volatile" memory (think a shared buffer between processors) and write it to a provided local buffer. Naively:

pub unsafe fn copy_from_volatile<T: Copy>(copy_to: &mut T, copy_from: &T) {
    *copy_to = unsafe {core::ptr::read_volatile(copy_from as *const T)};
}

This works OK for small-ish types (as long as the whole operation can fit once into general purpose registers). However when T starts to grow beyond a certain size, this function starts to consume stack space roughly proportional to the size of T. In addition it looks like it is never implemented as a copy loop, but instead a long list of loads and stores (well, at least on the "production-level" optimization levels 3, "z", "s"). Here is a demonstration: Compiler Explorer

Obviously this operation can be done more efficiently, namely load a word from source to a register, write from the register to the destination, without involving the intermediate stack space. Also can be done in a loop and unrolled in accordance to the the general unrolling policy specific to the opt level.
Is this a missed optimization bug or something intentional? How would one implement (in stable rust) this operation without the extra overhead? The only way I can think of is to manually reinterpret T as array of words and copy them in a loop. But this doesn't sound like a proper solution.

5 posts - 3 participants

Read full topic

🏷️ Rust_feed