Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential Compiler Optimization Issue with &Vec<Vec<T>> vs &[Vec<T>]? #124925

Open
zhongyi51 opened this issue May 9, 2024 · 3 comments
Open
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@zhongyi51
Copy link

zhongyi51 commented May 9, 2024

rustc version: 1.78

See Godbolt .

In my understanding, the function that accepts &Vec<Vec> should have the same optimization as the function that accepts &[Vec], because &Vec<Vec> can be turned into &[Vec] by simply calling as_slice() or as_mut_slice().
However, I found that &Vec<Vec> may hinder further vectorization in the nested loop, as shown in the above link.

I am wondering whether this is a compiler bug or an intended action?

@zhongyi51 zhongyi51 added the C-bug Category: This is a bug. label May 9, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 9, 2024
@clubby789
Copy link
Contributor

An &Vec<Vec<T>> requires 3 pointer reads to access a T: dereferencing the reference, reading the outer Vecs pointer to get the inner vec, and reading the inner Vecs pointer to access the element
&[Vec<T>] only requires 2 reads - reading the vec from the slice, and reading the element from within that vec
Calling as_slice on &Vec<...> requires 2 reads (reading the pointer and length fields from behind the reference) so it is not a free conversion
https://godbolt.org/z/rYaY4W5sv

In general, you should always accept &[T] rather than &Vec<T> for both performance and ergonomics. There's a Clippy lint explaining the reasoning

@clubby789 clubby789 closed this as not planned Won't fix, can't repro, duplicate, stale May 9, 2024
@clubby789 clubby789 removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 9, 2024
@zhongyi51
Copy link
Author

zhongyi51 commented May 9, 2024

@clubby789
Thank you for your response! Indeed, I am curious about how &Vec<Vec> hinders vectorization in the nested loop, rather than the cost of reading &Vec<Vec>.

Here is an example: Godbolt. Even if I manually call as_slice() and as_mut_slice() before the nested loop, the lack of vectorization persists.

I just hope this under-optimization is intentional.

@clubby789 clubby789 reopened this May 9, 2024
@clubby789 clubby789 added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-bug Category: This is a bug. labels May 9, 2024
@scottmcm
Copy link
Member

The OP moved this to IRLO, so cc https://internals.rust-lang.org/t/potential-compiler-optimization-issue-with-vec-const-t-vs-const-t/20865?u=scottmcm.

I am wondering whether this is a compiler bug or an intended action?

How about neither? It's an imperfection in optimizations, but imperfect optimization in non-canonical code isn't really something I'd call a "bug", even if it's not "intended" either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

4 participants