diff options
| author | Lukas Kalbertodt <lukas.kalbertodt@gmail.com> | 2021-07-30 00:08:48 +0200 |
|---|---|---|
| committer | Lukas Kalbertodt <lukas.kalbertodt@gmail.com> | 2021-07-30 00:08:48 +0200 |
| commit | 5cc7702bde47716c66734c488432850fab1a83dd (patch) | |
| tree | bde46924a1c4a0543589dc7aadff429f691a2b9c | |
| parent | a985d8e6c7f0519fa1e147854430a381ac4eadf8 (diff) | |
| download | rust-5cc7702bde47716c66734c488432850fab1a83dd.tar.gz rust-5cc7702bde47716c66734c488432850fab1a83dd.zip | |
Add docs about performance and `Iterator::map` to `[T; N]::map`
| -rw-r--r-- | library/core/src/array/mod.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 78b799cd709..3bc9f71375c 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -293,6 +293,28 @@ impl<T, const N: usize> [T; N] { /// Returns an array of the same size as `self`, with function `f` applied to each element /// in order. /// + /// If you don't necessarily need a new fixed-size array, consider using + /// [`Iterator::map`] instead. + /// + /// + /// # Note on performance and stack usage + /// + /// Unfortunately, usages of this method are currently not always optimized + /// as well as they could be. This mainly concerns large arrays, as mapping + /// over small arrays seem to be optimized just fine. Also note that in + /// debug mode (i.e. without any optimizations), this method can use a lot + /// of stack space (a few times the size of the array or more). + /// + /// Therefore, in performance-critical code, try to avoid using this method + /// on large arrays or check the emitted code. Also try to avoid chained + /// maps (e.g. `arr.map(...).map(...)`). + /// + /// In many cases, you can instead use [`Iterator::map`] by calling `.iter()` + /// or `.into_iter()` on your array. `[T; N]::map` is only necessary if you + /// really need a new array of the same size as the result. Rust's lazy + /// iterators tend to get optimized very well. + /// + /// /// # Examples /// /// ``` |
