Storing A Smaller Length Of A Slice And Accessing Beyond The Stored Length (But Inside The Actual Length)

Interisting analysis. My next question: Would that mean it’s not a valid optimization for a compiler when given code like

let x = [0; 10000];
let y = &x[0..10]
// ... rest of code only uses y, not x

to argue, “only the first 10 entries of x are ever used, let’s save loads of stack space and turn this into

let x = [0; 10];
let y = &x[0..10]
// ... rest of code only uses y, not x

”?

1 Like