rust-lang / rfcs

RFCs for changes to Rust
https://rust-lang.github.io/rfcs/
Apache License 2.0
5.78k stars 1.55k forks source link

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

Closed zhongyi51 closed 1 month ago

zhongyi51 commented 1 month ago

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 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?

ChayimFriedman2 commented 1 month ago

They have different layouts, and LLVM may find it difficult to optimize &Vec because it needs to load its ptr,len from memory.