-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
array::from_fn should be fully inlined #108765
Copy link
Copy link
Closed
Labels
A-arrayArea: `[T; N]`Area: `[T; N]`A-codegenArea: Code generationArea: Code generationI-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-arrayArea: `[T; N]`Area: `[T; N]`A-codegenArea: Code generationArea: Code generationI-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
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_fnin the hope that the intermediary array will be optimized out. And if everything gets inlined correctly, that works out as expected. But unfortunately, whilearray::from_fnitself 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::mapshould be marked inline, or the implementation ofarray::from_fnshould 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_fnfailing to inline at #102202 (comment) .