I recently learned that most of the methods I had assumed were implemented on array are actually implemented on slices, and that even though arrays coerce to slices they don't impl
Deref. Instead the coercion appears to happen through a separate built-in language rule specific to arrays.
I am wondering to what degree this is an artifact of history (const generics not originally being available) and whether it makes any sense to revisit the design.
How I imagined it to work before reading the docs is that there would be a trait abstracting over contiguous blocks of
T, call it
Contiguous<T>. Both arrays of
T and the slice type
[T] would impl the trait. The trait would have a size method, which in the array implementation would just be returning a constant, and for slices would return the stored sliced length. Most of the method implementations that are currently slice methods would be cut and pasted to instead be the default implementations for the trait methods.
What this buys you is that when you are working with arrays, since the size method would return a constant it would pretty much always be inlined, and so you would get generated array methods that are able to assume specific constant sizes. If I understand right in the current implementation only when a slice method is small enough that it itself gets inlined into its caller is there a chance that the compiler will figure out that you are actually dealing with an array of a specific size and potentially take advantage of that for optimizations, like completely unrolling a loop.
I haven't benchmarked anything, and there is the potential for emitting length specific versions of methods to bloat binaries more than the current approach which only emits type specific versions. But it does seem like maybe some optimization is being left on the table?
Also possibly going this route would make it so that arrays could just implement Deref, instead of needing a separate language rule for the coercion. I'm going to guess that there are other unintended effects from doing that though...?