-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Vec::splice() has noticeable overhead for some use cases #83266
Copy link
Copy link
Open
Labels
A-collectionsArea: `std::collections`Area: `std::collections`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.I-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-collectionsArea: `std::collections`Area: `std::collections`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.I-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.
As far as I know, Vec::splice() is the only way to extend the Vec from a slice in an arbitrary location. However, being a fairly universal api makes it slower than a hypothetical dedicated
insert_from_slicecould be.In fact, according to
cargo bench, for small vectors (let's say under 10) and number of elements to insert, it can be faster to insert them in a loop:than to call
splice:splicealso seems to optimize badly in presence of constants. For example, looking at Godbolt, the optimizer can't seem to optimizeas good as
Or
as good as
It'd be nice if the function had lower overhead for small vectors/slices. Or maybe if another specialized method
insert_from_slice(idx, slice)was added that avoids all overhead for this particular use case.