about summary refs log tree commit diff
path: root/src/liballoc/tests
AgeCommit message (Collapse)AuthorLines
2020-05-19Auto merge of #72227 - nnethercote:tiny-vecs-are-dumb, r=Amanieubors-0/+115
Tiny Vecs are dumb. Currently, if you repeatedly push to an empty vector, the capacity growth sequence is 0, 1, 2, 4, 8, 16, etc. This commit changes the relevant code (the "amortized" growth strategy) to skip 1 and 2, instead using 0, 4, 8, 16, etc. (You can still get a capacity of 1 or 2 using the "exact" growth strategy, e.g. via `reserve_exact()`.) This idea (along with the phrase "tiny Vecs are dumb") comes from the "doubling" growth strategy that was removed from `RawVec` in #72013. That strategy was barely ever used -- only when a `VecDeque` was grown, oddly enough -- which is why it was removed in #72013. (Fun fact: until just a few days ago, I thought the "doubling" strategy was used for repeated push case. In other words, this commit makes `Vec`s behave the way I always thought they behaved.) This change reduces the number of allocations done by rustc itself by 10% or more. It speeds up rustc, and will also speed up any other Rust program that uses `Vec`s a lot. In theory, the change could increase memory usage, but in practice it doesn't. It would be an unusual program where very small `Vec`s having a capacity of 4 rather than 1 or 2 would make a difference. You'd need a *lot* of very small `Vec`s, and/or some very small `Vec`s with very large elements. r? @Amanieu
2020-05-19Auto merge of #71447 - cuviper:unsized_cow, r=dtolnaybors-0/+48
impl From<Cow> for Box, Rc, and Arc These forward `Borrowed`/`Owned` values to existing `From` impls. - `Box<T>` is a fundamental type, so it would be a breaking change to add a blanket impl. Therefore, `From<Cow>` is only implemented for `[T]`, `str`, `CStr`, `OsStr`, and `Path`. - For `Rc<T>` and `Arc<T>`, `From<Cow>` is implemented for everything that implements `From` the borrowed and owned types separately.
2020-05-18Tiny Vecs are dumb.Nicholas Nethercote-0/+115
Currently, if you repeatedly push to an empty vector, the capacity growth sequence is 0, 1, 2, 4, 8, 16, etc. This commit changes the relevant code (the "amortized" growth strategy) to skip 1 and 2 in most cases, instead using 0, 4, 8, 16, etc. (You can still get a capacity of 1 or 2 using the "exact" growth strategy, e.g. via `reserve_exact()`.) This idea (along with the phrase "tiny Vecs are dumb") comes from the "doubling" growth strategy that was removed from `RawVec` in #72013. That strategy was barely ever used -- only when a `VecDeque` was grown, oddly enough -- which is why it was removed in #72013. (Fun fact: until just a few days ago, I thought the "doubling" strategy was used for repeated push case. In other words, this commit makes `Vec`s behave the way I always thought they behaved.) This change reduces the number of allocations done by rustc itself by 10% or more. It speeds up rustc, and will also speed up any other Rust program that uses `Vec`s a lot.
2020-04-25Rollup merge of #71485 - arlopurcell:binary_heap_retain, r=AmanieuDylan DPC-0/+9
Add BinaryHeap::retain as suggested in #42849 This PR implements retain for BinaryHeap as suggested in #42849. This is my first PR for Rust, so please let me know if I should be doing anything differently, thanks!
2020-04-24Add BinaryHeap::retain as suggested in #42849arlo-0/+9
2020-04-23liballoc: more compact way to adjust test sizes for MiriRalf Jung-67/+33
2020-04-22Add tests from CowJosh Stone-0/+48
2020-04-09Disable try_reserve tests on AndroidAmanieu d'Antras-0/+6
2020-04-06Use usize::MAX as assoc const in liballocLinus Färnstrand-1/+1
2020-04-06Use assoc const f32::NAN in liballocLinus Färnstrand-2/+2
2020-04-05Rollup merge of #70777 - faern:use-assoc-int-consts2, r=dtolnayDylan DPC-3/+0
Don't import integer and float modules, use assoc consts Stop importing the standard library integer and float modules to reach the `MIN`, `MAX` and other constants. They are available directly on the primitive types now. This PR is a follow up of #69860 which made sure we use the new constants in documentation. This type of change touches a lot of files, and previously all my assoc int consts PRs had collisions and were accepted only after a long delay. So I'd prefer to do it in smaller steps now. Just removing these imports seem like a good next step. r? @dtolnay
2020-04-05Rollup merge of #70558 - RalfJung:vec-extend-aliasing, r=AmanieuDylan DPC-5/+66
Fix some aliasing issues in Vec `Vec::extend` and `Vec::truncate` invalidated references into the vector even without reallocation, because they (implicitly) created a mutable reference covering the *entire* initialized part of the vector. Fixes https://github.com/rust-lang/rust/issues/70301 I verified the fix by adding some new tests here that I ran in Miri.
2020-04-05Stop importing integer modules in liballocLinus Färnstrand-3/+0
2020-04-02Auto merge of #70362 - TimDiekmann:alloc-overhaul, r=Amanieubors-2/+8
Overhaul of the `AllocRef` trait to match allocator-wg's latest consens; Take 2 GitHub won't let me reopen #69889 so I make a new PR. In addition to #69889 this fixes the unsoundness of `RawVec::into_box` when using allocators supporting overallocating. Also it uses `MemoryBlock` in `AllocRef` to unify `_in_place` methods by passing `&mut MemoryBlock`. Additionally, `RawVec` now checks for `size_of::<T>()` again and ignore every ZST. The internal capacity of `RawVec` isn't used by ZSTs anymore, as `into_box` now requires a length to be specified. r? @Amanieu fixes rust-lang/wg-allocators#38 fixes rust-lang/wg-allocators#41 fixes rust-lang/wg-allocators#44 fixes rust-lang/wg-allocators#51
2020-04-01Rollup merge of #68770 - ssomers:btree_drain_filter, r=AmanieuDylan DPC-1/+340
BTreeMap/BTreeSet: implement drain_filter Provide an implementation of drain_filter for BTreeMap and BTreeSet. Should be optimal when the predicate picks only elements in leaf nodes with at least MIN_LEN remaining elements, which is a common case, at least when draining only a fraction of the map/set, and also when the predicate picks elements stored in internal nodes where the right subtree can easily let go of a replacement element. The first commit adds benchmarks with an external, naive implementation. to compare how much this claimed optimality-in-some-cases is actually worth.
2020-03-30fix and test aliasing in swap_removeRalf Jung-0/+5
2020-03-30fix aliasing in remove()Ralf Jung-1/+6
also add smoke test to detect relocation even in rustc runs
2020-03-30also cover next() path of draining iteratorsRalf Jung-4/+11
2020-03-30test more mutating vector methodsRalf Jung-0/+30
2020-03-30fix BTreeMap test compilation with MiriRalf Jung-1/+1
2020-03-30add some testsRalf Jung-4/+18
2020-03-29BTreeMap/BTreeSet: implement and test drain_filterStein Somers-1/+340
2020-03-28BTreeMap testing: introduce symbolic constants and refer to height consistently.Stein Somers-41/+49
2020-03-28Make fields in `MemoryBlock` publicTim Diekmann-1/+1
2020-03-26Remove alignment from `MemoryBlock`Tim Diekmann-5/+2
2020-03-26Fix issues from review and unsoundness of `RawVec::into_box`Tim Diekmann-3/+6
2020-03-26Overhaul of the `AllocRef` trait to match allocator-wg's latest consensTim Diekmann-2/+8
2020-03-24must_use on split_offKornel-2/+2
#70194
2020-03-20Update test commentary for shared root removalMark Rousskov-9/+2
2020-03-08Rollup merge of #69776 - ssomers:fix69769, r=Mark-SimulacrumMazdak Farrokhzad-1/+25
Fix & test leak of some BTreeMap nodes on panic during `into_iter` Fixes #69769
2020-03-06Fix & test leak of some BTreeMap nodes on panic during `into_iter`Stein Somers-1/+25
2020-03-05reduce test size for MiriRalf Jung-2/+2
2020-03-03Rollup merge of #69609 - TimDiekmann:excess, r=AmanieuYuki Okushi-1/+1
Remove `usable_size` APIs This removes the usable size APIs: - remove `usable_size` (obv) - change return type of allocating methods to include the allocated size - remove `_excess` API r? @Amanieu closes rust-lang/wg-allocators#17
2020-03-03Remove `usable_size` APIsTim Diekmann-1/+1
2020-02-26Auto merge of #67290 - jonas-schievink:leak-audit, r=KodrAusbors-2/+267
Audit liballoc for leaks in `Drop` impls when user destructor panics Inspired by https://github.com/rust-lang/rust/pull/67243 and https://github.com/rust-lang/rust/pull/67235, this audits and hopefully fixes the remaining `Drop` impls in liballoc for resource leaks in the presence of panics in destructors called by the affected `Drop` impl. This does not touch `Hash{Map,Set}` since they live in hashbrown. They have similar issues though. r? @KodrAus
2020-02-22Auto merge of #67330 - golddranks:split_inclusive, r=kodrausbors-0/+124
Implement split_inclusive for slice and str # Overview * Implement `split_inclusive` for `slice` and `str` and `split_inclusive_mut` for `slice` * `split_inclusive` is a substring/subslice splitting iterator that includes the matched part in the iterated substrings as a terminator. * EDIT: The behaviour has now changed, as per @KodrAus 's input, to the same semantics with the `split_terminator` function. I updated the examples below. * Two examples below: ```Rust let data = "\nMäry häd ä little lämb\nLittle lämb\n"; let split: Vec<&str> = data.split_inclusive('\n').collect(); assert_eq!(split, ["\n", "Märy häd ä little lämb\n", "Little lämb\n"]); ``` ```Rust let uppercase_separated = "SheePSharKTurtlECaT"; let mut first_char = true; let split: Vec<&str> = uppercase_separated.split_inclusive(|c: char| { let split = !first_char && c.is_uppercase(); first_char = split; split }).collect(); assert_eq!(split, ["SheeP", "SharK", "TurtlE", "CaT"]); ``` # Justification for the API * I was surprised to find that stdlib currently only has splitting iterators that leave out the matched part. In my experience, wanting to leave a substring terminator as a part of the substring is a pretty common usecase. * This API is strictly more expressive than the standard `split` API: it's easy to get the behaviour of `split` by mapping a subslicing operation that drops the terminator. On the other hand it's impossible to derive this behaviour from `split` without using hacky and brittle `unsafe` code. The normal way to achieve this functionality would be implementing the iterator yourself. * Especially when dealing with mutable slices, the only way currently is to use `split_at_mut`. This API provides an ergonomic alternative that plays to the strengths of the iterating capabilities of Rust. (Using `split_at_mut` iteratively used to be a real pain before NLL, fortunately the situation is a bit better now.) # Discussion items * <s>Does it make sense to mimic `split_terminator` in that the final empty slice would be left off in case of the string/slice ending with a terminator? It might do, as this use case is naturally geared towards considering the matching part as a terminator instead of a separator.</s> * EDIT: The behaviour was changed to mimic `split_terminator`. * Does it make sense to have `split_inclusive_mut` for `&mut str`?
2020-02-16Lighten tests, in particular for Miri, yet test and explain moreStein Somers-20/+32
2020-02-09Rollup merge of #68738 - kennytm:derive-clone-eq-for-fromutf8error, r=sfacklerJonas Schievink-0/+4
Derive Clone + Eq for std::string::FromUtf8Error Implement `Clone` and `Eq` for `std::string::FromUtf8Error`. Both the inner `Vec<u8>` and `std::str::Utf8Error` are also `Clone + Eq`, so I don't see why we shouldn't derive them on `FromUtf8Error` as well. (impl are insta-stable, requiring FCP from T-libs.)
2020-02-09Don't return empty slice on last iteration with matched terminator. Test ↵Pyry Kontio-9/+94
reverse iteration.
2020-02-09Implement split_inclusive for slice and str, an splitting iterator that ↵Pyry Kontio-0/+39
includes the matched part in the iterated substrings as a terminator.
2020-02-04Fix and test implementation of BTreeMap's first_entry, last_entry, ↵Stein Somers-11/+21
pop_first, pop_last
2020-02-02Derive Clone + PartialEq + Eq for std::string::FromUtf8Errorkennytm-0/+4
2020-01-30Rollup merge of #66648 - crgl:btree-clone-from, r=AmanieuDylan DPC-0/+20
Implement clone_from for BTreeMap and BTreeSet See #28481. This results in up to 90% speedups on simple data types when `self` and `other` are the same size, and is generally comparable or faster. Some concerns: 1. This implementation requires an `Ord` bound on the `Clone` implementation for `BTreeMap` and `BTreeSet`. Since these structs can only be created externally for keys with `Ord` implemented, this should be fine? If not, there's certainly a less safe way to do this. 2. Changing `next_unchecked` on `RangeMut` to return mutable key references allows for replacing the entire overlapping portion of both maps without changing the external interface in any way. However, if `clone_from` fails it can leave the `BTreeMap` in an invalid state, which might be unacceptable. ~This probably needs an FCP since it changes a trait bound, but (as far as I know?) that change cannot break any external code.~
2020-01-28Include empty BTreeMap in clone_from testsCharles Gleason-2/+2
2020-01-27Rename `Alloc` to `AllocRef`Tim Diekmann-2/+2
2020-01-19FormatJonas Schievink-13/+9
2020-01-19Add test for panic in LL DrainFilter predicateJonas Schievink-0/+35
2020-01-19Avoid leak in DrainFilter when a drop panicsJonas Schievink-1/+35
2020-01-19Fix leak in vec::IntoIter when a destructor panicsJonas Schievink-0/+29
2020-01-19Fix leak in VecDeque::drain when drop panicsJonas Schievink-0/+38