rust/src/libcore
bors 09ee9b723a Auto merge of #44856 - cuviper:more-fold, r=dtolnay
Add more custom folding to `core::iter` adaptors

Many of the iterator adaptors will perform faster folds if they forward
to their inner iterator's folds, especially for inner types like `Chain`
which are optimized too.  The following types are newly specialized:

| Type        | `fold` | `rfold` |
| ----------- | ------ | ------- |
| `Enumerate` | ✓      | ✓       |
| `Filter`    | ✓      | ✓       |
| `FilterMap` | ✓      | ✓       |
| `FlatMap`   | exists | ✓       |
| `Fuse`      | ✓      | ✓       |
| `Inspect`   | ✓      | ✓       |
| `Peekable`  | ✓      | N/A¹    |
| `Skip`      | ✓      | N/A²    |
| `SkipWhile` | ✓      | N/A¹    |

¹ not a `DoubleEndedIterator`

² `Skip::next_back` doesn't pull skipped items at all, but this couldn't
be avoided if `Skip::rfold` were to call its inner iterator's `rfold`.

Benchmarks
----------

In the following results, plain `_sum` computes the sum of a million
integers -- note that `sum()` is implemented with `fold()`.  The
`_ref_sum` variants do the same on a `by_ref()` iterator, which is
limited to calling `next()` one by one, without specialized `fold`.

The `chain` variants perform the same tests on two iterators chained
together, to show a greater benefit of forwarding `fold` internally.

    test iter::bench_enumerate_chain_ref_sum  ... bench:   2,216,264 ns/iter (+/- 29,228)
    test iter::bench_enumerate_chain_sum      ... bench:     922,380 ns/iter (+/- 2,676)
    test iter::bench_enumerate_ref_sum        ... bench:     476,094 ns/iter (+/- 7,110)
    test iter::bench_enumerate_sum            ... bench:     476,438 ns/iter (+/- 3,334)

    test iter::bench_filter_chain_ref_sum     ... bench:   2,266,095 ns/iter (+/- 6,051)
    test iter::bench_filter_chain_sum         ... bench:     745,594 ns/iter (+/- 2,013)
    test iter::bench_filter_ref_sum           ... bench:     889,696 ns/iter (+/- 1,188)
    test iter::bench_filter_sum               ... bench:     667,325 ns/iter (+/- 1,894)

    test iter::bench_filter_map_chain_ref_sum ... bench:   2,259,195 ns/iter (+/- 353,440)
    test iter::bench_filter_map_chain_sum     ... bench:   1,223,280 ns/iter (+/- 1,972)
    test iter::bench_filter_map_ref_sum       ... bench:     611,607 ns/iter (+/- 2,507)
    test iter::bench_filter_map_sum           ... bench:     611,610 ns/iter (+/- 472)

    test iter::bench_fuse_chain_ref_sum       ... bench:   2,246,106 ns/iter (+/- 22,395)
    test iter::bench_fuse_chain_sum           ... bench:     634,887 ns/iter (+/- 1,341)
    test iter::bench_fuse_ref_sum             ... bench:     444,816 ns/iter (+/- 1,748)
    test iter::bench_fuse_sum                 ... bench:     316,954 ns/iter (+/- 2,616)

    test iter::bench_inspect_chain_ref_sum    ... bench:   2,245,431 ns/iter (+/- 21,371)
    test iter::bench_inspect_chain_sum        ... bench:     631,645 ns/iter (+/- 4,928)
    test iter::bench_inspect_ref_sum          ... bench:     317,437 ns/iter (+/- 702)
    test iter::bench_inspect_sum              ... bench:     315,942 ns/iter (+/- 4,320)

    test iter::bench_peekable_chain_ref_sum   ... bench:   2,243,585 ns/iter (+/- 12,186)
    test iter::bench_peekable_chain_sum       ... bench:     634,848 ns/iter (+/- 1,712)
    test iter::bench_peekable_ref_sum         ... bench:     444,808 ns/iter (+/- 480)
    test iter::bench_peekable_sum             ... bench:     317,133 ns/iter (+/- 3,309)

    test iter::bench_skip_chain_ref_sum       ... bench:   1,778,734 ns/iter (+/- 2,198)
    test iter::bench_skip_chain_sum           ... bench:     761,850 ns/iter (+/- 1,645)
    test iter::bench_skip_ref_sum             ... bench:     478,207 ns/iter (+/- 119,252)
    test iter::bench_skip_sum                 ... bench:     315,614 ns/iter (+/- 3,054)

    test iter::bench_skip_while_chain_ref_sum ... bench:   2,486,370 ns/iter (+/- 4,845)
    test iter::bench_skip_while_chain_sum     ... bench:     633,915 ns/iter (+/- 5,892)
    test iter::bench_skip_while_ref_sum       ... bench:     666,926 ns/iter (+/- 804)
    test iter::bench_skip_while_sum           ... bench:     444,405 ns/iter (+/- 571)
2017-09-29 12:56:24 +00:00
..
benches Add more custom folding to core::iter adaptors 2017-09-25 20:53:08 -07:00
fmt Less confusing placeholder when RefCell is exclusively borrowed 2017-09-21 21:53:04 -07:00
hash impl Hasher for {&mut Hasher, Box<Hasher>} 2017-09-12 17:28:07 +08:00
iter Auto merge of #44856 - cuviper:more-fold, r=dtolnay 2017-09-29 12:56:24 +00:00
num change #![feature(const_fn)] to specific gates 2017-09-16 15:53:02 +00:00
ops Add support for ..= syntax 2017-09-22 22:05:18 +02:00
prelude Run rustfmt on libcore/prelude folder 2016-10-16 22:13:04 +05:30
slice Substitute ... with the expanded form 2017-09-22 22:05:18 +02:00
str TrustedRandomAccess specialisation for Cloned. 2017-09-23 15:30:53 -04:00
sync Added more text from unstable-book to compiler_fence docs 2017-09-16 22:18:46 +02:00
tests Auto merge of #44856 - cuviper:more-fold, r=dtolnay 2017-09-29 12:56:24 +00:00
any.rs Various fixes to wording consistency in the docs 2017-03-22 17:19:52 +01:00
array.rs Update bootstrap compiler 2017-08-31 06:58:58 -07:00
borrow.rs Update older URLs pointing to the first edition of the Book 2017-06-15 00:04:00 +09:00
Cargo.toml Integrate jobserver support to parallel codegen 2017-06-21 07:16:43 -07:00
cell.rs change #![feature(const_fn)] to specific gates 2017-09-16 15:53:02 +00:00
char.rs std: Stabilize char_escape_debug 2017-07-25 07:09:31 -07:00
char_private.rs Reduce a table used for Debug impl of str. 2017-04-05 09:13:19 -07:00
clone.rs Update bootstrap compiler 2017-08-31 06:58:58 -07:00
cmp.rs stabilized ord_max_min (fixes #25663) 2017-09-15 12:54:03 +02:00
convert.rs Update older URLs pointing to the first edition of the Book 2017-06-15 00:04:00 +09:00
default.rs Fix "Quasi-quoting is inefficient" warning in incremental rustbuild. 2017-07-18 01:49:40 +08:00
internal_macros.rs Fix a few impl stability attributes 2017-01-29 13:31:47 +00:00
intrinsics.rs Add <*const T>::align_offset and use it in memchr 2017-09-17 21:30:58 +02:00
iter_private.rs Various fixes to wording consistency in the docs 2017-03-22 17:19:52 +01:00
lib.rs change #![feature(const_fn)] to specific gates 2017-09-16 15:53:02 +00:00
macros.rs Use rvalue promotion to 'static instead of static items. 2017-09-10 11:20:27 +03:00
marker.rs Removed as many "```ignore" as possible. 2017-06-23 15:31:53 +08:00
mem.rs Rollup merge of #44648 - Havvy:doc-size_of, r=dtolnay 2017-09-23 00:29:10 -04:00
nonzero.rs change #![feature(const_fn)] to specific gates 2017-09-16 15:53:02 +00:00
option.rs Rollup merge of #43705 - panicbit:option_ref_mut_cloned, r=aturon 2017-08-29 21:40:53 +00:00
panicking.rs Bump master to 1.21.0 2017-07-25 07:03:19 -07:00
ptr.rs Add <*const T>::align_offset and use it in memchr 2017-09-17 21:30:58 +02:00
raw.rs Fix up various links 2017-03-20 10:10:16 -04:00
result.rs Add missing urls for Result struct 2017-08-18 16:48:07 +02:00
tuple.rs Update bootstrap compiler 2017-08-31 06:58:58 -07:00