Hello!
Both of the examples you made above (y=x[1:5] and z=x[[5,6,7]]) are essentially calling the same function, so there is no real difference there. As you have noticed, this can become a bottleneck in code generation.
While there are good chances that this will be improved in the future, the best option is to try to avoid the the slicing altogether and use the "vertsplit" operation instead.
This works if you want to divide an operation (for example corresponding to the free variables of your NLP) into a lot of blocks and later use all of the blocks. The operation is in some sense the "inverse" of a vertcat operation and works like this:
x_block1, x_block2, x_block3 = vertsplit(x,[0,n1,n1+n2,n1+n2+n3])
where n1, n2, n3 are the number of rows in block 1, 2 and 3 respectively. I hope you understand the idea. The "vertsplit" operation has efficient derivative calculation rules and compact code generation.
Note that it may be hard to avoid "slicing operations" (a.k.a. getNonzeros, setNonzeros) in your code completely since they also arise when assembling complete (sparse) Jacobians and Hessian. The code needed for this is usually moderate.
I hope this helps you further!
Joel