about summary refs log tree commit diff
path: root/src/libcollections
AgeCommit message (Collapse)AuthorLines
2017-01-05Add a tracking issueSteven Fackler-1/+1
2017-01-01Add PeekMut::popSteven Fackler-4/+19
A fairly common workflow is to put a bunch of stuff into a binary heap and then mutate the top value until its empty. This both makes that a bit more convenient (no need to save a boolean off and pop after to avoid borrowck issues), and a bit more efficient since you only shift once.
2016-12-29Remove not(stage0) from deny(warnings)Alex Crichton-1/+1
Historically this was done to accommodate bugs in lints, but there hasn't been a bug in a lint since this feature was added which the warnings affected. Let's completely purge warnings from all our stages by denying warnings in all stages. This will also assist in tracking down `stage0` code to be removed whenever we're updating the bootstrap compiler.
2016-12-29Fallout from updating bootstrap CargoAlex Crichton-3/+4
2016-12-28Replace uses of `#[unsafe_destructor_blind_to_params]` with `#[may_dangle]`Andrew Paseltiner-11/+7
CC #34761
2016-12-26Clarify behavior of `VecDeque::insert`.Corey Farwell-8/+12
Fixes https://github.com/rust-lang/rust/issues/37046.
2016-12-24Rollup merge of #38297 - matklad:linked-lists-are-not-cool, r=GuillaumeGomezSteve Klabnik-3/+17
Advertise Vec in LinkedList docs r? @steveklabnik Hi! We already [advise](https://doc.rust-lang.org/std/collections/#use-a-linkedlist-when) to use `Vec` instead of `LinkedList` in the top-level collections documentation. But I think it may be missed by someone who just directly finds `LinkedList`. What do you feel about advertising `Vec` directly in `LinkedList` docs as well?
2016-12-23Implement placement-in protocol for `Vec`Andrew Paseltiner-1/+74
2016-12-21Auto merge of #38488 - srinivasreddy:rf_collections, r=aturonbors-95/+103
run rustfmt on libcollections folder
2016-12-20Rollup merge of #38432 - stjepang:fix-sort-comment, r=blussAlex Crichton-3/+3
Minor fix in the merge_sort comments There was an off-by-one error discovered by @tbelaire. So, to repeat... the two invariants we are enforcing are: 1. Run lengths are decreasing. 2. Sum of lengths of any two adjacent runs is less than the length of their predecessor. This commit changes the relevant comment to be clearer and have correct bounds on `i`. r? @bluss
2016-12-20Rollup merge of #38208 - frewsxcv:btreesetdocs, r=alexcrichtonAlex Crichton-11/+58
Improvements to `BTreeSet` documentation. None
2016-12-20Indicate that `BTreeSet::iter` returns values in ascending order.Corey Farwell-1/+14
Fixes https://github.com/rust-lang/rust/issues/38204.
2016-12-20run rustfmt on libcollections folderSrinivas Reddy Thatiparthy-95/+103
2016-12-17Minor fix in the merge_sort commentsStjepan Glavina-3/+3
There was an off-by-one error discovered by @tbelaire. So, the two invariants we are enforcing are: 1. Run lengths are decreasing. 2. Sum of lengths of any two adjacent runs is less than the length of their predecessor. This commit changes the comment to be clearer and have correct bounds on `i`.
2016-12-16Advertise Vec in LinkedList docsAleksey Kladov-3/+17
2016-12-15Stabilize std::vec::IntoIter::{as_slice, as_mut_slice}Aaron Turon-4/+2
2016-12-15Simplify `BTreeSet::iter` doc example.Corey Farwell-8/+6
2016-12-15Document how `BTreeSet` iterator structures are created.Corey Farwell-0/+36
2016-12-14Indicate `BTreeSet` in docs is code-like.Corey Farwell-5/+5
2016-12-12Auto merge of #38049 - frewsxcv:libunicode, r=alexcrichtonbors-8/+8
Rename 'librustc_unicode' crate to 'libstd_unicode'. Fixes https://github.com/rust-lang/rust/issues/26554.
2016-12-09Auto merge of #38192 - stjepang:faster-sort-algorithm, r=blussbors-182/+301
Implement a faster sort algorithm Hi everyone, this is my first PR. I've made some changes to the standard sort algorithm, starting out with a few tweaks here and there, but in the end this endeavour became a complete rewrite of it. #### Summary Changes: * Improved performance, especially on partially sorted inputs. * Performs less comparisons on both random and partially sorted inputs. * Decreased the size of temporary memory: the new sort allocates 4x less. Benchmark: ``` name out1 ns/iter out2 ns/iter diff ns/iter diff % slice::bench::sort_large_ascending 85,323 (937 MB/s) 8,970 (8918 MB/s) -76,353 -89.49% slice::bench::sort_large_big_ascending 2,135,297 (599 MB/s) 355,955 (3595 MB/s) -1,779,342 -83.33% slice::bench::sort_large_big_descending 2,266,402 (564 MB/s) 416,479 (3073 MB/s) -1,849,923 -81.62% slice::bench::sort_large_big_random 3,053,031 (419 MB/s) 1,921,389 (666 MB/s) -1,131,642 -37.07% slice::bench::sort_large_descending 313,181 (255 MB/s) 14,725 (5432 MB/s) -298,456 -95.30% slice::bench::sort_large_mostly_ascending 287,706 (278 MB/s) 243,204 (328 MB/s) -44,502 -15.47% slice::bench::sort_large_mostly_descending 415,078 (192 MB/s) 271,028 (295 MB/s) -144,050 -34.70% slice::bench::sort_large_random 545,872 (146 MB/s) 521,559 (153 MB/s) -24,313 -4.45% slice::bench::sort_large_random_expensive 30,321,770 (2 MB/s) 23,533,735 (3 MB/s) -6,788,035 -22.39% slice::bench::sort_medium_ascending 616 (1298 MB/s) 155 (5161 MB/s) -461 -74.84% slice::bench::sort_medium_descending 1,952 (409 MB/s) 202 (3960 MB/s) -1,750 -89.65% slice::bench::sort_medium_random 3,646 (219 MB/s) 3,421 (233 MB/s) -225 -6.17% slice::bench::sort_small_ascending 39 (2051 MB/s) 34 (2352 MB/s) -5 -12.82% slice::bench::sort_small_big_ascending 96 (13333 MB/s) 96 (13333 MB/s) 0 0.00% slice::bench::sort_small_big_descending 248 (5161 MB/s) 243 (5267 MB/s) -5 -2.02% slice::bench::sort_small_big_random 501 (2554 MB/s) 490 (2612 MB/s) -11 -2.20% slice::bench::sort_small_descending 95 (842 MB/s) 63 (1269 MB/s) -32 -33.68% slice::bench::sort_small_random 372 (215 MB/s) 354 (225 MB/s) -18 -4.84% ``` #### Background First, let me just do a quick brain dump to discuss what I learned along the way. The official documentation says that the standard sort in Rust is a stable sort. This constraint is thus set in stone and immediately rules out many popular sorting algorithms. Essentially, the only algorithms we might even take into consideration are: 1. [Merge sort](https://en.wikipedia.org/wiki/Merge_sort) 2. [Block sort](https://en.wikipedia.org/wiki/Block_sort) (famous implementations are [WikiSort](https://github.com/BonzaiThePenguin/WikiSort) and [GrailSort](https://github.com/Mrrl/GrailSort)) 3. [TimSort](https://en.wikipedia.org/wiki/Timsort) Actually, all of those are just merge sort flavors. :) The current standard sort in Rust is a simple iterative merge sort. It has three problems. First, it's slow on partially sorted inputs (even though #29675 helped quite a bit). Second, it always makes around `log(n)` iterations copying the entire array between buffers, no matter what. Third, it allocates huge amounts of temporary memory (a buffer of size `2*n`, where `n` is the size of input). The problem of auxilliary memory allocation is a tough one. Ideally, it would be best for our sort to allocate `O(1)` additional memory. This is what block sort (and it's variants) does. However, it's often very complicated (look at [this](https://github.com/BonzaiThePenguin/WikiSort/blob/master/WikiSort.cpp)) and even then performs rather poorly. The author of WikiSort claims good performance, but that must be taken with a grain of salt. It performs well in comparison to `std::stable_sort` in C++. It can even beat `std::sort` on partially sorted inputs, but on random inputs it's always far worse. My rule of thumb is: high performance, low memory overhead, stability - choose two. TimSort is another option. It allocates a buffer of size `n/2`, which is not great, but acceptable. Performs extremelly well on partially sorted inputs. However, it seems pretty much all implementations suck on random inputs. I benchmarked implementations in [Rust](https://github.com/notriddle/rust-timsort), [C++](https://github.com/gfx/cpp-TimSort), and [D](https://github.com/dlang/phobos/blob/fd518eb310a9494cccf28c54892542b052c49669/std/algorithm/sorting.d#L2062). The results were a bit disappointing. It seems bad performance is due to complex galloping procedures in hot loops. Galloping noticeably improves performance on partially sorted inputs, but worsens it on random ones. #### The new algorithm Choosing the best algorithm is not easy. Plain merge sort is bad on partially sorted inputs. TimSort is bad on random inputs and block sort is even worse. However, if we take the main ideas from TimSort (intelligent merging strategy of sorted runs) and drop galloping, then we'll have great performance on random inputs and it won't be bad on partially sorted inputs either. That is exactly what this new algorithm does. I can't call it TimSort, since it steals just a few of it's ideas. Complete TimSort would be a much more complex and elaborate implementation. In case we in the future figure out how to incorporate more of it's ideas into this implementation without crippling performance on random inputs, it's going to be very easy to extend. I also did several other minor improvements, like reworked insertion sort to make it faster. There are also new, more thorough benchmarks and panic safety tests. The final code is not terribly complex and has less unsafe code than I anticipated, but there's still plenty of it that should be carefully reviewed. I did my best at documenting non-obvious code. I'd like to notify several people of this PR, since they might be interested and have useful insights: 1. @huonw because he wrote the [original merge sort](https://github.com/rust-lang/rust/pull/11064). 2. @alexcrichton because he was involved in multiple discussions of it. 3. @veddan because he wrote [introsort](https://github.com/veddan/rust-introsort) in Rust. 4. @notriddle because he wrote [TimSort](https://github.com/notriddle/rust-timsort) in Rust. 5. @bluss because he had an attempt at writing WikiSort in Rust. 6. @gnzlbg, @rkruppe, and @mark-i-m because they were involved in discussion #36318. **P.S.** [quickersort](https://github.com/notriddle/quickersort) describes itself as being universally [faster](https://github.com/notriddle/quickersort/blob/master/perf.txt) than the standard sort, which is true. However, if this PR gets merged, things might [change](https://gist.github.com/stjepang/b9f0c3eaa0e1f1280b61b963dae19a30) a bit. ;)
2016-12-08Inline nested fn collapseStjepan Glavina-0/+1
Since merge_sort is generic and collapse isn't, that means calls to collapse won't be inlined. inlined. Therefore, we must stick an `#[inline]` above `fn collapse`.
2016-12-08Auto merge of #38182 - bluss:more-vec-extend, r=alexcrichtonbors-6/+33
Specialization for Extend<&T> for vec Specialize to use copy_from_slice when extending a Vec with &[T] where T: Copy. This specialization results in `.clone()` not being called in `extend_from_slice` and `extend` when the element is `Copy`. Fixes #38021
2016-12-07Implement a faster sort algorithmStjepan Glavina-182/+300
This is a complete rewrite of the standard sort algorithm. The new algorithm is a simplified variant of TimSort. In summary, the changes are: * Improved performance, especially on partially sorted inputs. * Performs less comparisons on both random and partially sorted inputs. * Decreased the size of temporary memory: the new sort allocates 4x less.
2016-12-07Auto merge of #38149 - bluss:is-empty, r=alexcrichtonbors-7/+31
Forward more ExactSizeIterator methods and `is_empty` edits - Forward ExactSizeIterator methods in more places, like `&mut I` and `Box<I>` iterator impls. - Improve `VecDeque::is_empty` itself (see commit 4) - All the collections iterators now have `len` or `is_empty` forwarded if doing so is a benefit. In the remaining cases, they already use a simple size hint (using something like a stored `usize` value), which is sufficient for the default implementation of len and is_empty.
2016-12-06vec: More specialization for Extend<&T> for vecUlrik Sverdrup-6/+33
Specialize to use copy_from_slice when extending a Vec with &[T] where T: Copy.
2016-12-04binary_heap: Forward ExactSizeIterator::is_emptyUlrik Sverdrup-3/+15
2016-12-04collections: Simplify VecDeque::is_emptyUlrik Sverdrup-4/+16
Improve is_empty on the VecDeque and its iterators by just comparing tail and head; this saves a few instructions (to be able to remove the `& (size - 1)` computation, it would have to know that size is a power of two).
2016-12-03Remove redundant assertion near is_char_boundary.Clar Charr-4/+0
2016-11-30Add String::split_off.Clar Charr-0/+33
2016-11-30Rename 'librustc_unicode' crate to 'libstd_unicode'.Corey Farwell-8/+8
Fixes #26554.
2016-11-26Auto merge of #36340 - sfackler:slice-get-slice, r=alexcrichtonbors-4/+15
Implement RFC 1679 cc #35729 r? @alexcrichton
2016-11-26Auto merge of #38008 - bluss:rustbuild-benches, r=alexcrichtonbors-0/+4
Add rustbuild command `bench` Add command bench to rustbuild, so that `./x.py bench <path>` can compile and run benchmarks. `./x.py bench --stage 1 src/libcollections` and `./x.py bench --stage 1 src/libstd` should both compile well. Just `./x.py bench` runs all benchmarks for the libstd crates. Fixes #37897
2016-11-26Overload get{,_mut}{,_unchecked}Steven Fackler-4/+15
2016-11-26Rollup merge of #37967 - sfackler:enumset-issue, r=sfacklerSeo Sanghyeon-1/+1
Add a tracking issue for enum_set I totally forgot this even existed!
2016-11-25rustbuild: Point to core and collections's external benchmarks.Ulrik Sverdrup-0/+4
2016-11-23Add a tracking issue for enum_setSteven Fackler-1/+1
2016-11-23core, collections: Implement better .is_empty() for slice and vec iteratorsUlrik Sverdrup-2/+11
These iterators can use a pointer comparison instead of computing the length.
2016-11-13vec: Use less code bloat specialized Vec::from_iterUlrik Sverdrup-20/+31
Vec::from_iter's general case allocates the vector up front; this is redundant for the TrustedLen case, and can then be avoided to reduce the size of the code.
2016-11-13Restore Vec::from_iter() specializationUlrik Sverdrup-1/+1
Since I said "no intentional functional change" in the previous commit, I guess it was inevitable there were unintentional changes. Not functional, but optimization-wise. This restores the extend specialization's use in Vec::from_iter.
2016-11-11vec: Write the .extend() specialization in cleaner styleUlrik Sverdrup-30/+41
As far as possible, use regular `default fn` specialization in favour of ad-hoc conditionals.
2016-11-05Rollup merge of #37587 - ollie27:to_mut, r=alexcrichtonAlex Crichton-1/+4
Remove recursive call from Cow::to_mut It seems to prevent it from being inlined.
2016-11-05Rollup merge of #37585 - leodasvacas:change_into_to_from, r=alexcrichtonAlex Crichton-4/+4
Change `Into<Vec<u8>> for String` and `Into<OsString> for PathBuf` to From Fixes #37561. First contribution, happy with any and all feedback!
2016-11-05Rollup merge of #37574 - ollie27:cow_add, r=alexcrichtonAlex Crichton-24/+37
Fix issues with the Add/AddAssign impls for Cow<str> * Correct the stability attributes. * Make Add and AddAssign actually behave the same. * Use String::with_capacity when allocating a new string. * Fix the tests.
2016-11-04Remove recursive call from Cow::to_mutOliver Middleton-1/+4
It seems to prevent it from being inlined.
2016-11-04Change Into<Vec<u8>> for String and Into<OsString> for PathBuf to From implsleonardo.yvens-4/+4
2016-11-04Auto merge of #37306 - bluss:trusted-len, r=alexcrichtonbors-57/+44
Add Iterator trait TrustedLen to enable better FromIterator / Extend This trait attempts to improve FromIterator / Extend code by enabling it to trust the iterator to produce an exact number of elements, which means that reallocation needs to happen only once and is moved out of the loop. `TrustedLen` differs from `ExactSizeIterator` in that it attempts to include _more_ iterators by allowing for the case that the iterator's len does not fit in `usize`. Consumers must check for this case (for example they could panic, since they can't allocate a collection of that size). For example, chain can be TrustedLen and all numerical ranges can be TrustedLen. All they need to do is to report an exact size if it fits in `usize`, and `None` as the upper bound otherwise. The trait describes its contract like this: ``` An iterator that reports an accurate length using size_hint. The iterator reports a size hint where it is either exact (lower bound is equal to upper bound), or the upper bound is `None`. The upper bound must only be `None` if the actual iterator length is larger than `usize::MAX`. The iterator must produce exactly the number of elements it reported. This trait must only be implemented when the contract is upheld. Consumers of this trait must inspect `.size_hint()`’s upper bound. ``` Fixes #37232
2016-11-04Fix issues with the Add/AddAssign impls for Cow<str>Oliver Middleton-24/+37
* Correct the stability attributes. * Make Add and AddAssign actually behave the same. * Use String::with_capacity when allocating a new string. * Fix the tests.
2016-11-04Link the tracking issue for TrustedLenUlrik Sverdrup-1/+1
2016-10-31Changed most vec! invocations to use square bracesiirelu-4/+4
Most of the Rust community agrees that the vec! macro is clearer when called using square brackets [] instead of regular brackets (). Most of these ocurrences are from before macros allowed using different types of brackets. There is one left unchanged in a pretty-print test, as the pretty printer still wants it to have regular brackets.