I can only assume the compiler is marking all the bounds checks as unlikely to fail, and correctly predicted branches must be more or less free in modern CPUs. It’s the only way I can explain it.
It is a common low level optimization to add a wide boundary check in the beginning to avoid successive smaller bounds checks.
And the “compile in the standard library” pattern will probably never be changed in rust due to monomorphization. Lots of types in the standard library need to be specialised based on the types of your structs. Even if rust had an ABI for dynamic linking (it doesn’t), structs and functions which take generic type parameters will probably never be able to be loaded from a shared library. Same as C++.
You can see this pretty easily in compiler explorer. This is a simple array lookup compiled in release mode:
[0] https://clang.llvm.org/docs/LanguageExtensions.html#builtin-...
Thanks!
I've always felt uncomfortable about PGO for more complex programs because of this.