3 ms·
Still that assert has to be checked at some point right? I would assume that maybe the compiler could do this optimization without having to be told.
by tudelo 7y ago
Still that assert has to be checked at some point right? I would assume that maybe the compiler could do this optimization without having to be told.
- FreeFull 7y agoIn practice a small function like this would be inlined, which gives room for further optimisations. At any point, if the compiler knows that all accesses are in bounds, it can remove the bounds checks. The trick is actually having it figure that out.
- deleted 7y ago[deleted]
- kzrdude 7y agoI believe the compiler can't do that, since the bounds check panic is a side effect that can be observed - the message tells which index that failed the check! For that reason, non-elided checks will not be reordered. However, I believe a future Rust RFC could turn that around and validate the idea that in some cases such things could change execution order, even if it has noticeable side effects. (No idea if it's technically feasible!)