Non-literal constant objects are not well optimized comparing to literal constant objects #118557
Labels
C-optimization
Category: An issue highlighting optimization opportunities or PRs implementing such
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
The problem is originated from rust-lang/log#599.
Sometimes, multiple function calls can have the same constant arguments:
Rust recognizes that these arguments are the same value, so it would create a single constant value and pass it to each function:
But sometimes, these constant arguments have to be computed by some additional functions:
Then the compiler can’t optimize these constant objects well as the first example, additional copying operations are generated:
You can see the comparison here: https://godbolt.org/z/frj9a8TG6.
Additionally, using a const value as a proxy helps:
But some functions can’t be used to compute a const value, such as
std::panic::Location::caller
, so the method above does not always work.The text was updated successfully, but these errors were encountered: