-
-
Notifications
You must be signed in to change notification settings - Fork 17.4k
Vec::from_iter specialization is slower on TrustedLen iterators from thin-vec #160048
Copy link
Copy link
Open
Labels
A-iteratorsArea: IteratorsArea: IteratorsA-specializationArea: Trait impl specializationArea: Trait impl specializationI-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.This issue may need triage. Remove it if it has been sufficiently triaged.
Description
Activity
Metadata
Metadata
Assignees
Labels
A-iteratorsArea: IteratorsArea: IteratorsA-specializationArea: Trait impl specializationArea: Trait impl specializationI-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.This issue may need triage. Remove it if it has been sufficiently triaged.
In #159974, #159975, we found out that updating and enabling
"unstable"feature flag inthin-vecmakes some new-solver benchmarks slower.I tried to measure this in isolation on the old version in #160010, where the old "unstable" flag only adds
TrustedLenimpl tothin_vec::IntoIterandthin_vec::Drain. The results for only adding the flag are here and they reproduce the regression: #160010 (comment)Based on the cachegrind diffs, the culprit is mostly in this iterator chain:
rust/compiler/rustc_trait_selection/src/solve/fulfill.rs
Lines 185 to 198 in d3ea035
pending.drain()createsthin_vec::Drainiterator. The rest of it are iterators fromstd.Adding no-op
skip(0)before thecollect()call disables the specialization (becauseSkipdoesn't implementTrustedLen) and fixes most of the regression: #160010 (comment), but not all of it, so it looks like the problem is not specific to this chain.If I understand the specialization resolution correctly, the problematic
from_iterimpl is this one:rust/library/alloc/src/vec/spec_from_iter_nested.rs
Lines 50 to 62 in d3ea035
Note that #160005 optimizes some of the next-solver data structures, so it's possible the issue will no longer reproduce on this code on main as visibly, but the issue might still exist.
I currently don't have time to research this further and create smaller reproducer, but maybe somebody who has more experience with these specializations has a better idea for what's going on.