rust-itertools / itertools

Extra iterator adaptors, iterator methods, free functions, and macros.
https://docs.rs/itertools/
Apache License 2.0
2.62k stars 298 forks source link

Prevent exponentially large type sizes in `tuple_combinations` #945

Closed lcnr closed 1 month ago

lcnr commented 1 month ago

This slightly improves the performance of cargo build --release on stable, and prevents a hang[^1] when using the next-generation trait solver -Znext-solver.

Long term such manually optimizations should be unnecessary, but for now it unfortunately requires some manual effort.

[^1]: it would finish at some point, but the sun will have probably consumed the earth before then

lcnr commented 1 month ago

after fixing https://github.com/rust-lang/rust/issues/125460 the following causes a compiler error before this PR:

use itertools::Itertools;

fn main() {
    (0..7).tuple_combinations().fold(1, |_, (t0, t1, t2, t3, t4, t5, t6, t7)| 1);
}
error: reached the type-length limit while instantiating `<Map<Fuse<std::ops::Range<i32>>, {closure@<... as Iterator>::fold<..., ...>::{closure#0}}> as Iterator>::fold::<..., ...>`
   --> /home/gh-lcnr/rust/library/core/src/iter/adapters/map.rs:125:5
    |
125 | /     fn fold<Acc, G>(self, init: Acc, g: G) -> Acc
126 | |     where
127 | |         G: FnMut(Acc, Self::Item) -> Acc,
    | |_________________________________________^
    |
    = help: consider adding a `#![type_length_limit="1536444"]` attribute to your crate