Performance over safety

Hello everybody!

I have a question about performance over safety in Rust.
I'm a professional C++ video game dev. In game developpement we car about panic and backtrace only in debug or 'debug with optmisation', when we ship we want fastest code possible.
The point is with Rust when a use array like [i32; 4] even in release compilation with cargo I still have a panic when accessing out of bound. So somewhere there is a check if the bound is in correct range, a 'if' branch so...
Is there a way to say Rust don't check i know what i'm doing and if it's crap then let's be crap and remove the branch of the test?

Thank you

1 Like

Hi, to get a value in a slice you have get_unchecked it will not check if the index is in bound.
If you use a loop or iterators the branch may be optimized out by the compiler, it will understand you are not making out of bound access and just remove the branch.
If you access multiple values you can also hint the compiler to only make one branch at the beginning and not one for each access.

2 Likes

Thank you for the reply.
This is interesting, Rust is a very interesting and promising language!

1 Like

If you are into game dev you might find arewegameyet interesting, it collects many game related resources and show what the ecosystem looks like.

1 Like

Note that if you index [i32; 4] with a constant or with a value for which compiler can infer that it falls into the correct range (e.g. i in loop for i in 0..3 { .. }) bound check will be eliminated. You may use no-panic to ensure that bound checks got eliminated.

3 Likes

Yes I saw it in Rust Conf on youtube :wink:
Today I just evaluate the language with tiny scenarii and compare it with C and C++ ( mainly with https://godbolt.org/ ) and so far lot of think looks good :slight_smile:
I hope the best for the future :slight_smile:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.