For example, is there any problems with doing this?

fn main() {  
	static mut BUF: [u8; 0x400] = [0; 0x400];  
	let buf = &mut unsafe { BUF };  
}  

and is this code the same as just using an array directly? From my understanding local variables get put on the stack but do the static variables do too?

I’m essentially trying to find the most performant way to get a simple read/write buffer.

  • Val@anarchist.nexusOP
    link
    fedilink
    English
    arrow-up
    6
    ·
    1 day ago

    Thanks! That’s exactly the answer I was looking for.

    The premature optimisation quote at the end of your blog post is very relevant to me. I try do find the most efficient way right off the bat so very frequently the first questions I have in a project are like this one. Which in turn lead me to understand the underlying basics, which make me want to implement those basics myself, which sends me down a spiral to wanting to write my own kernel. All the while the project I started with gets forgotten, until I pick it up a month later and the whole thing starts again.

    Maybe I should just try and learn C… or zig. And try and hold myself to the higher quality standard they demand. Especially since it feels like I’m doing that already.

      • Val@anarchist.nexusOP
        link
        fedilink
        English
        arrow-up
        1
        ·
        14 hours ago

        My logic was simply: I need a buffer that is only initialised once no matter how many times the function is called. statics are initialised at program start so they seemed like a good fit. and since I wasn’t planning for the function to me called multiple times simultaneously it seemed like the UB didn’t matter. (which I think was correct)

        • BB_C@programming.dev
          link
          fedilink
          arrow-up
          1
          ·
          9 hours ago

          UB didn’t matter.

          There is no such a thing.

          If you really must pretend this matters performance wise, look up MaybeUninit. It still requires unsafe{}, but it’s a lot less trouble.