|
1 | 1 | //! Inplace iterate-and-collect specialization for `Vec` |
2 | 2 | //! |
| 3 | +//! Note: This documents Vec internals, some of the following sections explain implementation |
| 4 | +//! details and are best read together with the source of this module. |
| 5 | +//! |
3 | 6 | //! The specialization in this module applies to iterators in the shape of |
4 | 7 | //! `source.adapter().adapter().adapter().collect::<Vec<U>>()` |
5 | | -//! where `source` is an owning iterator obtained from [`Vec<T>`], [`Box<[T]>`] (by conversion to `Vec`) |
| 8 | +//! where `source` is an owning iterator obtained from [`Vec<T>`], [`Box<[T]>`][box] (by conversion to `Vec`) |
6 | 9 | //! or [`BinaryHeap<T>`], the adapters each consume one or more items per step |
7 | 10 | //! (represented by [`InPlaceIterable`]), provide transitive access to `source` (via [`SourceIter`]) |
8 | 11 | //! and thus the underlying allocation. And finally the layouts of `T` and `U` must |
9 | | -//! have the same size and alignment, this is currently ensured via const eval instead of trait |
10 | | -//! bounds. |
| 12 | +//! have the same size and alignment, this is currently ensured via const eval instead of trait bounds |
| 13 | +//! in the specialized [`SpecFromIter`] implementation. |
11 | 14 | //! |
12 | 15 | //! [`BinaryHeap<T>`]: crate::collections::BinaryHeap |
13 | | -//! [`Box<[T]>`]: crate::boxed::Box |
| 16 | +//! [box]: crate::boxed::Box |
14 | 17 | //! |
15 | | -//! By extension some other collections which use `collect::Vec<_>()` internally in their |
| 18 | +//! By extension some other collections which use `collect::<Vec<_>>()` internally in their |
16 | 19 | //! `FromIterator` implementation benefit from this too. |
17 | 20 | //! |
18 | 21 | //! Access to the underlying source goes through a further layer of indirection via the private |
|
27 | 30 | //! # Reading from and writing to the same allocation |
28 | 31 | //! |
29 | 32 | //! By its nature collecting in place means that the reader and writer side of the iterator |
30 | | -//! use the same allocation. Since `fold()` and co. take a reference to the iterator for the |
31 | | -//! duration of the iteration that means we can't interleave the step of reading a value |
32 | | -//! and getting a reference to write to. Instead raw pointers must be used on the reader |
33 | | -//! and writer side. |
| 33 | +//! use the same allocation. Since `try_fold()` (used in [`SpecInPlaceCollect`]) takes a |
| 34 | +//! reference to the iterator for the duration of the iteration that means we can't interleave |
| 35 | +//! the step of reading a value and getting a reference to write to. Instead raw pointers must be |
| 36 | +//! used on the reader and writer side. |
34 | 37 | //! |
35 | 38 | //! That writes never clobber a yet-to-be-read item is ensured by the [`InPlaceIterable`] requirements. |
36 | 39 | //! |
|
49 | 52 | //! All those drops in turn can panic which then must either leak the allocation or abort to avoid |
50 | 53 | //! double-drops. |
51 | 54 | //! |
52 | | -//! These tasks are handled by [`InPlaceDrop`] and [`vec::IntoIter::forget_allocation_drop_remaining()`] |
| 55 | +//! This is handled by the [`InPlaceDrop`] guard for sink items (`U`) and by |
| 56 | +//! [`vec::IntoIter::forget_allocation_drop_remaining()`] for remaining source items (`T`). |
53 | 57 | //! |
54 | 58 | //! [`vec::IntoIter::forget_allocation_drop_remaining()`]: super::IntoIter::forget_allocation_drop_remaining() |
55 | 59 | //! |
56 | 60 | //! # O(1) collect |
57 | 61 | //! |
58 | 62 | //! The main iteration itself is further specialized when the iterator implements |
59 | 63 | //! [`TrustedRandomAccessNoCoerce`] to let the optimizer see that it is a counted loop with a single |
60 | | -//! induction variable. This can turn some iterators into a noop, i.e. it reduces them from O(n) to |
| 64 | +//! [induction variable]. This can turn some iterators into a noop, i.e. it reduces them from O(n) to |
61 | 65 | //! O(1). This particular optimization is quite fickle and doesn't always work, see [#79308] |
62 | 66 | //! |
63 | 67 | //! [#79308]: https://github.com/rust-lang/rust/issues/79308 |
| 68 | +//! [induction variable]: https://en.wikipedia.org/wiki/Induction_variable |
64 | 69 | //! |
65 | 70 | //! Since unchecked accesses through that trait do not advance the read pointer of `IntoIter` |
66 | 71 | //! this would interact unsoundly with the requirements about dropping the tail described above. |
67 | 72 | //! But since the normal `Drop` implementation of `IntoIter` would suffer from the same problem it |
68 | 73 | //! is only correct for `TrustedRandomAccessNoCoerce` to be implemented when the items don't |
69 | 74 | //! have a destructor. Thus that implicit requirement also makes the specialization safe to use for |
70 | 75 | //! in-place collection. |
| 76 | +//! Note that this safety concern is about the correctness of `impl Drop for IntoIter`, |
| 77 | +//! not the guarantees of `InPlaceIterable`. |
71 | 78 | //! |
72 | 79 | //! # Adapter implementations |
73 | 80 | //! |
|
76 | 83 | //! For example `InPlaceIterable` would be valid to implement for [`Peekable`], except |
77 | 84 | //! that it is stateful, cloneable and `IntoIter`'s clone implementation shortens the underlying |
78 | 85 | //! allocation which means if the iterator has been peeked and then gets cloned there no longer is |
79 | | -//! enough room, thus breaking an invariant (#85322). |
| 86 | +//! enough room, thus breaking an invariant ([#85322]). |
80 | 87 | //! |
81 | 88 | //! [#85322]: https://github.com/rust-lang/rust/issues/85322 |
82 | 89 | //! [`Peekable`]: core::iter::Peekable |
|
0 commit comments