m4b / goblin

An impish, cross-platform binary parsing crate, written in Rust
MIT License
1.2k stars 160 forks source link

Remove unneeded unsafe in data_directories.rs #261

Closed nico-abram closed 3 years ago

nico-abram commented 3 years ago

This pull request replaces get_unchecked calls with direct indexing ( [] ). This removes a bunch of unsafe blocks

As far as I can tell, this should not affect optimized code: Since rust statically knows the size of the array, it should be able to elide the bounds checks

This godbolt link shows how both the version with and without the unsafe are compiled to the same function with opt-level=3. This is the same example without the derives adding noise to the output

What I found interesting is, according to this godbolt link when compiling without optimizations, rust also elides the bounds checks for the indexing version, and the get_unchecked version has to call the get_unchecked function which does not get inlined imagen

So I would expect the new version to outperform the unsafe version on debug builds, and match the unsafe version with optimizations