File tree Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Original file line number Diff line number Diff line change @@ -2499,8 +2499,18 @@ pub trait Itertools: Iterator {
2499
2499
/// └─f─f─f─f─f─f
2500
2500
/// ```
2501
2501
///
2502
- /// If `f` is non-associative, prefer the normal [`Iterator::reduce`] instead to prevent
2503
- /// unexpected behavior unless you know what you're doing.
2502
+ /// If `f` is associative you should also decide carefully:
2503
+ ///
2504
+ /// - if `f` is a trivial operation like `u32::wrapping_add`, prefer the normal
2505
+ /// [`Iterator::reduce`] instead since it will most likely result in the generation of simpler
2506
+ /// code because the compiler is able to optimize it
2507
+ /// - otherwise if `f` is non-trivial like `format!`, you should use `tree_fold1` since it
2508
+ /// reduces the number of operations from `O(n)` to `O(ln(n))`
2509
+ ///
2510
+ /// Here "non-trivial" means:
2511
+ ///
2512
+ /// - any allocating operation
2513
+ /// - any function that is a composition of many operations
2504
2514
///
2505
2515
/// ```
2506
2516
/// use itertools::Itertools;
You can’t perform that action at this time.
0 commit comments