Implementing data structures that make use of reading uninitialized memory

I came accross this interesting trick for speeding up initialization of sparse data structures: research!rsc: Using Uninitialized Memory for Fun and Profit.

I tried to implement it in Rust, but ran into a seemingly fundamental problem. Even with unsafe, Rust seems to have no way to read uninitialized memory without causing undefined behavior.

Is there a way to implement this trick in Rust (without undefined behavior)? If not, are there any upcoming proposals that would allow implementing this trick?

1 Like

The primary benefit of the data structure is the O(1) clear operation. You still get that if you zero-initialize the memory when you first allocate it. So I'd just do that, it shouldn't have that much overhead over allocating non-initialized memory in most applications.

1 Like

To date, that's true within the Rust abstract machine AFAIU. Some want the ability, others actively don't. You can search for LLVM freeze for more discussion. (Sadly you will have to wade through many false hits regarding the Freeze trait, which is something else.)

You can presumably roundtrip through something outside the abstract machine, like FFI. But I'm afraid I don't have an example handy.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.