Skip to content

Commit 003a219

Browse files
committed
Dial down detail of B-tree description
fixes 134088, though it is a shame to lose some of this wonderful detail.
1 parent 642e49b commit 003a219

File tree

1 file changed

+5
-24
lines changed
  • library/alloc/src/collections/btree

1 file changed

+5
-24
lines changed

library/alloc/src/collections/btree/map.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,10 @@ pub(super) const MIN_LEN: usize = node::MIN_LEN_AFTER_SPLIT;
4040

4141
/// An ordered map based on a [B-Tree].
4242
///
43-
/// B-Trees represent a fundamental compromise between cache-efficiency and actually minimizing
44-
/// the amount of work performed in a search. In theory, a binary search tree (BST) is the optimal
45-
/// choice for a sorted map, as a perfectly balanced BST performs the theoretical minimum amount of
46-
/// comparisons necessary to find an element (log<sub>2</sub>n). However, in practice the way this
47-
/// is done is *very* inefficient for modern computer architectures. In particular, every element
48-
/// is stored in its own individually heap-allocated node. This means that every single insertion
49-
/// triggers a heap-allocation, and every single comparison should be a cache-miss. Since these
50-
/// are both notably expensive things to do in practice, we are forced to, at the very least,
51-
/// reconsider the BST strategy.
52-
///
53-
/// A B-Tree instead makes each node contain B-1 to 2B-1 elements in a contiguous array. By doing
54-
/// this, we reduce the number of allocations by a factor of B, and improve cache efficiency in
55-
/// searches. However, this does mean that searches will have to do *more* comparisons on average.
56-
/// The precise number of comparisons depends on the node search strategy used. For optimal cache
57-
/// efficiency, one could search the nodes linearly. For optimal comparisons, one could search
58-
/// the node using binary search. As a compromise, one could also perform a linear search
59-
/// that initially only checks every i<sup>th</sup> element for some choice of i.
60-
///
61-
/// Currently, our implementation simply performs naive linear search. This provides excellent
62-
/// performance on *small* nodes of elements which are cheap to compare. However in the future we
63-
/// would like to further explore choosing the optimal search strategy based on the choice of B,
64-
/// and possibly other factors. Using linear search, searching for a random element is expected
65-
/// to take B * log(n) comparisons, which is generally worse than a BST. In practice,
66-
/// however, performance is excellent.
43+
/// A B-tree resembles a [binary search tree], but each leaf (node) contains
44+
/// an entire array (of unspecified size) of elements, instead of just a single element.
45+
/// A search first traverses the tree structure to find, in logarithmic time, the correct leaf.
46+
/// This leaf is then searched linearly, which is very fast on modern hardware.
6747
///
6848
/// It is a logic error for a key to be modified in such a way that the key's ordering relative to
6949
/// any other key, as determined by the [`Ord`] trait, changes while it is in the map. This is
@@ -77,6 +57,7 @@ pub(super) const MIN_LEN: usize = node::MIN_LEN_AFTER_SPLIT;
7757
/// amortized constant time per item returned.
7858
///
7959
/// [B-Tree]: https://en.wikipedia.org/wiki/B-tree
60+
/// [binary search tree]: https://en.wikipedia.org/wiki/Binary_search_tree
8061
/// [`Cell`]: core::cell::Cell
8162
/// [`RefCell`]: core::cell::RefCell
8263
///

0 commit comments

Comments
 (0)