Open zhongyi51 opened 5 months ago
An &Vec<Vec<T>>
requires 3 pointer reads to access a T
: dereferencing the reference, reading the outer Vec
s pointer to get the inner vec, and reading the inner Vec
s 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
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>
.
I just hope this under-optimization is intentional.
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.
rustc version: 1.78
See Godbolt,l:'5',n:'0',o:'Rust+source+%231',t:'0')),k:45.59356256309758,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:r1780,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'1',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:rust,libs:!(),options:'-C+opt-level%3D2',overrides:!(),selection:(endColumn:41,endLineNumber:6,positionColumn:41,positionLineNumber:6,selectionStartColumn:41,selectionStartLineNumber:6,startColumn:41,startLineNumber:6),source:1),l:'5',n:'0',o:'+rustc+1.78.0+(Editor+%231)',t:'0')),header:(),k:54.406437436902436,l:'4',m:100,n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4) .
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 callingas_slice()
oras_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?