ArrayRef vs SmallVectorImpl

It is suggested in the documentation that if you would have declared a function parameter as SmallVector<Foo,N>&, it is better to instead declare it as SmallVectorImpl&.

This makes sense, but it seems to me that it is better still to declare it as ArrayRef; a quick test suggests it compiles to the same (highly efficient) code, and adds a bit more flexibility in case e.g. you someday want to pass a std::vector.

By that logic, there is never any reason to take a SmallVectorImpl, because ArrayRef is strictly better. Am I missing anything?

You can’t append to an ArrayRef

The documentation might be specifically calling out the non-const
SmallVector ref case. That would be the case wheer you want to modify
the vector or its contents.

If you don't want to do any mutation and the container can't change
underneath you (eg: nothing happening inside the function might cause,
indirectly, the vector to be resized, etc) then ArrayRef is probably
the right tool.

If you want to modify the existing elements of the container (&
similarly, the vector won't be resized, etc) - MutableArrayRef would
be suitable.

If you need to be able to add or remove elements from the vector, or
reflect changes due to indirect modifications - SmallVectorImpl& would
be the most suitable.

Ah, so ArrayRef is strictly preferable only to const SmallVectorImpl&. Makes sense, thanks!