Skip to content

Commit

Permalink
Auto merge of #111296 - Sp00ph:const_gcd, r=nagisa,Mark-Simulacrum
Browse files Browse the repository at this point in the history
Always const-evaluate the GCD in `slice::align_to_offsets`

Use an inline `const`-block to force the compiler to calculate the GCD at compile time, even in debug mode. This shouldn't affect the behavior of the program at all, but it drastically cuts down on the number of instructions emitted with optimizations disabled.

With the current implementation, a single `slice::align_to` instantiation (specifically `<[u8]>::align_to::<u128>()`) generates 676 instructions (on x86-64). Forcing the GCD computation to be const cuts it down to 327 instructions, so just over 50% less. This is obviously not representative of actual runtime gains, but I still see it as a significant win as long as it doesn't degrade compile times.

Not having to worry about LLVM const-evaluating the GCD function also allows it to use the textbook recursive euclidean algorithm instead of a much more complicated iterative implementation with multiple `unsafe`-blocks.
  • Loading branch information
bors committed May 8, 2023
2 parents 0b959a4 + 0769466 commit 0a492a8
Showing 0 changed files with 0 additions and 0 deletions.

0 comments on commit 0a492a8

Please sign in to comment.