I've not used Rust much and I'm getting stuck a bit using the type Box<[u32;1024]>, I can't figure out how I'm supposed to directly allocate and zero initialise this without copying it from the stack or using a vector or something which feels wrong.
I essentially just want the equivalent to:
Just checking and It's not a massive deal for this small size but does this allocate the argument on stack and copy it in debug builds? Or is this effectively always optimised out even without release mode
I'm not too opposed to this but I was hoping there would've been a simpler way. I'm guessing I can count on this always being optimised out to effectively produce the result I'm looking for?
unless it causes real problems, I will just use Box::new([0u32; 1024]). it would probably be optimized away in release build.
but even without optimization, what's wrong with Vec? the equivalent to the C code is:
let page = vec![0u32; 1024];
you can convert it into a Box<[u32]> using Vec::into_boxed_slice() if the length will never change after creation.
well, it's mostly equivalent, but you do pay the little overhead of the extra length (and capacity, in the case of Vec) compared to a thin pointer. if you really want a thin pointer, currently you'll need to make a wrapper yourself, since the Box<[T]>::into_array() api is unstable yet.
So yeah the main reason I'd want to avoid using a vector here is that I'm potentially paying the price of those length and capacity fields a couple thousand times over. Same reasoning for not wanting the length field that comes with slice so I'm looking to only keep the size information at compile time.
PS: I wonder why can't Rust compiler guarantee this type of optimization and use alloc_zeroed all the time. We wouldn't need a separate crate then. Is it because nobody really cares about some extra bytes in "fat" vector pointer?
yeah I mean based on the contents of this crate it seems fairly trivial to just do this unsafely with new_zeroed and assume it was initialised correctly since the chances that a given heap allocation fails is already pretty low and leaking 4KiB isn't exactly the end of the world. I was just hoping that kind of thing would have a safe api considering the optimiser can already come up with seemingly similar stuff which it guarantees to be safe
You either need to implement such method for each type separately, or introduce an unsafe trait for types which can be initialized by zeros (something like zerocopy::FromZeros). The latter could be nice to have, while the former would be pretty inelegant.
UPD: BTW let b: Box<[u32; 1024]> = FromZeros::new_box_zeroed().unwrap(); does exactly what you want.
Because guaranteeing that a certain ad-hoc transformation ("when an array literal expression is the sole argument of a function call expression where the function value is Box::new, replace it...") always happens, and defining exactly what conditions it happens in, becomes a burden on the definition of the language and on every implementation.
Optimizations have the freedom that every unclear case can always be implemented using the non-optimized form. “Guaranteed optimization” is poor terminology: if something is guaranteed, it is no longer an optimization.
There are extensive discussions going on about adding a general “in-place init” feature to Rust, which would provide the guarantee you want, and work for more things than just zeroed arrays.
Once thing came to my mind: in C++ they have copy elision: return value optimization and other things like
foo do_all() {
// Can be created directly in the caller stack, no need to copy it
// and copy could be heavy in C++ due to logic it ctor, it is not plain memcpy
return foo{..};
}
Because copying could have observable behaviour (e.g. ctor called) they promoted it from "nice to have" to "mandatory" at some point.
So, optimizations can sometimes become a part of specification.
Yes, but if this happens, it will no longer be called an optimization. And the guarantee will probably have narrower applicability than the optimization. (For a current example, there are guarantees about the layout of Option, which there are not for arbitrary enums, even though other enums are subject to the same “optimization”.)
My (possibly faulty) memory is that this was not optimized to use alloc_zeroed, so I was happy to know that it is. I played around with it in godbolt and it seems to work well!