Description
It regularly happens to me that I need to create an array not because that's the end product of my computation, but because that's a useful intermediate object when going from dynamic-sized entities like iterators, slices, or arbitrary generator functions, to static-sized entities like SIMD types or loops that must be unrolled.
In this scenario, I tend to use array::from_fn
in the hope that the intermediary array will be optimized out. And if everything gets inlined correctly, that works out as expected. But unfortunately, while array::from_fn
itself is marked #[inline]
, its implementation is based on <[(); N]>::map
, which is not marked #[inline]
. And since the code of that latter function is mildly complex before optimization, the part of rustc that splits crates into multiple codegen-units has an unpleasant tendency to outline it into a separate codegen unit, resulting in a performance disaster.
To avoid this, either array::map
should be marked inline, or the implementation of array::from_fn
should be changed not to rely on it but only on other inline functions.
You can find further discussion of this and a simple reproducer of array::from_fn
failing to inline at #102202 (comment) .