Skip to content

Commit 1091f24

Browse files
rbruenigPhilippe-Cholet
authored andcommitted
tree_reduce doc: explain benefit when building binary search tree
1 parent 46c6874 commit 1091f24

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,6 +2534,11 @@ pub trait Itertools: Iterator {
25342534
/// arguments. For example if `f` combines arguments using `format!`, then `tree_reduce` will
25352535
/// operate on average on shorter arguments resulting in less bytes being allocated overall.
25362536
///
2537+
/// Moreover, the output of `tree_reduce` is preferable to that of [`Iterator::reduce`] in
2538+
/// certain cases. For example, building a binary search tree using `tree_reduce` will result in
2539+
/// a balanced tree with height O(ln(n)), while [`Iterator::reduce`] will output a tree with
2540+
/// height O(n), essentially a linked list.
2541+
///
25372542
/// If 'f' does not benefit from such a reordering, like `u32::wrapping_add`, prefer the
25382543
/// normal [`Iterator::reduce`] instead since it will most likely result in the generation of
25392544
/// simpler code because the compiler is able to optimize it.

0 commit comments

Comments
 (0)