Skip to content

Commit c459f51

Browse files
authored
Clarify documentation labelling and definitions for std::collections
Page affected: https://doc.rust-lang.org/std/collections/index.html#performance Changes: - bulleted conventions - expanded definitions on terms used - more accessible language.
1 parent a48861a commit c459f51

File tree

1 file changed

+40
-24
lines changed
  • library/std/src/collections

1 file changed

+40
-24
lines changed

library/std/src/collections/mod.rs

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -79,34 +79,50 @@
7979
//! see each type's documentation, and note that the names of actual methods may
8080
//! differ from the tables below on certain collections.
8181
//!
82-
//! Throughout the documentation, we will follow a few conventions. For all
83-
//! operations, the collection's size is denoted by n. If another collection is
84-
//! involved in the operation, it contains m elements. Operations which have an
85-
//! *amortized* cost are suffixed with a `*`. Operations with an *expected*
86-
//! cost are suffixed with a `~`.
87-
//!
88-
//! All amortized costs are for the potential need to resize when capacity is
89-
//! exhausted. If a resize occurs it will take *O*(*n*) time. Our collections never
90-
//! automatically shrink, so removal operations aren't amortized. Over a
91-
//! sufficiently large series of operations, the average cost per operation will
92-
//! deterministically equal the given cost.
93-
//!
94-
//! Only [`HashMap`] has expected costs, due to the probabilistic nature of hashing.
95-
//! It is theoretically possible, though very unlikely, for [`HashMap`] to
96-
//! experience worse performance.
97-
//!
98-
//! ## Sequences
99-
//!
100-
//! | | get(i) | insert(i) | remove(i) | append | split_off(i) |
101-
//! |----------------|------------------------|-------------------------|------------------------|-----------|------------------------|
102-
//! | [`Vec`] | *O*(1) | *O*(*n*-*i*)* | *O*(*n*-*i*) | *O*(*m*)* | *O*(*n*-*i*) |
103-
//! | [`VecDeque`] | *O*(1) | *O*(min(*i*, *n*-*i*))* | *O*(min(*i*, *n*-*i*)) | *O*(*m*)* | *O*(min(*i*, *n*-*i*)) |
104-
//! | [`LinkedList`] | *O*(min(*i*, *n*-*i*)) | *O*(min(*i*, *n*-*i*)) | *O*(min(*i*, *n*-*i*)) | *O*(1) | *O*(min(*i*, *n*-*i*)) |
82+
//! ## Conventions
83+
//! Throughout the documentation, we will adhere to the following conventions for
84+
//! operation notation:
85+
//!
86+
//! * The collection's size is denoted by `n`.
87+
//! * If a second collection is involved, it's size is denoted by `m`.
88+
//! * Item indices are denoted by `i`.
89+
//! * Operations which have an *amortized* cost are suffixed with a `*`.
90+
//! * Operations with an *expected* cost are suffixed with a `~`.
91+
//!
92+
//! ### Amortized Costs
93+
//!
94+
//! Calling operations that add to a collection will occasionally require a
95+
//! collection to be resized - an extra operation that takes *O*(*n*) time.
96+
//!
97+
//! Amortized costs are calculated to account for the time cost of this resize
98+
//! *over a sufficiently large series of operations*. An individual operation
99+
//! may be slower or faster due to the sporadic nature of collection resizing,
100+
//! however the average cost per operation will eventually equal the amortized
101+
//! cost.
102+
//!
103+
//! Rust's collections never automatically shrink, so removal operations aren't
104+
//! amortized.
105+
//!
106+
//! ### Expected Costs
107+
//!
108+
//! [`HashMap`] uses expected costs to denote that it is theoretically possible,
109+
//! though very unlikely, for [`HashMap`] to experience worse performance than
110+
//! the expected cost. This is due to the probablilistic nature of hashing -
111+
//! i.e. it is possible to generate a duplicate hash given some input key that
112+
//! requires extra computation to correct, but exceptionally unlikely.
113+
//!
114+
//! ## Cost of Sequence Operations
115+
//!
116+
//! | | get(i) | insert(i) | remove(i) | append(Vec(m)) | split_off(i) |
117+
//! |----------------|------------------------|-------------------------|------------------------|-------------------|------------------------|
118+
//! | [`Vec`] | *O*(1) | *O*(*n*-*i*)* | *O*(*n*-*i*) | *O*(*m*)* | *O*(*n*-*i*) |
119+
//! | [`VecDeque`] | *O*(1) | *O*(min(*i*, *n*-*i*))* | *O*(min(*i*, *n*-*i*)) | *O*(*m*)* | *O*(min(*i*, *n*-*i*)) |
120+
//! | [`LinkedList`] | *O*(min(*i*, *n*-*i*)) | *O*(min(*i*, *n*-*i*)) | *O*(min(*i*, *n*-*i*)) | *O*(1) | *O*(min(*i*, *n*-*i*)) |
105121
//!
106122
//! Note that where ties occur, [`Vec`] is generally going to be faster than [`VecDeque`], and
107123
//! [`VecDeque`] is generally going to be faster than [`LinkedList`].
108124
//!
109-
//! ## Maps
125+
//! ## Cost of Map Operations
110126
//!
111127
//! For Sets, all operations have the cost of the equivalent Map operation.
112128
//!

0 commit comments

Comments
 (0)