This buffer is "push only" - once something is pushed to the buffer it never gets popped, so I can be sure that if at some point in program I pushed something it will maintain the index assigned.
To get access to the buffer's elements I use this:
fn get(i: Index) -> &i32 {
self.0[i.0]
}
As you remember, the slice::Index operator checks that i is a valid index and panics if it's not. In my case the index is always valid, the check is meaningless. Profiler tells me that this check takes about ~17% of the run time so I would be glad to move this check to compile time.
while keeping Buffer::get fn safe. To make this sound I need to ensure that Index returned from Buffer::push never outlives self, but it must not borrow anything because I need buffers to be mutable accessible.
Profiler tells me that this check takes about ~17% of the run time
In general, bounds checks are almost free, as they are trivially predictable not-taken branches. The case where they really hurt performance is that when the prevent auto-vectorization, but I doubt that profiler can display that info.
Do you actually get 17% increase in run-time performance if you just blindly use get_unchecked ?