about summary refs log tree commit diff
path: root/src/liballoc
AgeCommit message (Collapse)AuthorLines
2018-04-01Stabilize String::replace_rangeThayne McCombs-21/+20
Fixes #44643
2018-04-02Add vec!['\0'; n] optimization, like vec![0; n]Mike Hommey-0/+2
Similarly to vec![ptr::null{,_mut}(); n] in previous change, this adds the optimization for vec!['\0'; n].
2018-04-02Add vec![ptr::null{,_mut}(); n] optimization, like vec![0; n]Mike Hommey-26/+53
vec![0; n], via implementations of SpecFromElem, has an optimization that uses with_capacity_zeroed instead of with_capacity, which will use calloc instead of malloc, and avoid an extra memset. This adds the same optimization for vec![ptr::null(); n] and vec![ptr::null_mut(); n], assuming their bit value is 0 (which is true on all currently supported platforms). This does so by adding an intermediate trait IsZero, which looks very much like nonzero::Zeroable, but that one is on the way out, and doesn't apply to pointers anyways. Adding such a trait allows to avoid repeating the logic using with_capacity_zeroed or with_capacity, or making the macro more complex to support generics.
2018-04-01Inline most of the code paths for conversions with boxed slicesAnthony Ramine-1/+11
This helps with the specific problem described in #49541, obviously without making any large change to how inlining works in the general case. Everything involved in the conversions is made `#[inline]`, except for the `<Vec<T>>::into_boxed_slice` entry point which is made `#[inline(always)]` after checking that duplicating the function mentioned in the issue prevented its inlining if I only annotate it with `#[inline]`. For the record, that function was: ```rust pub fn foo() -> Box<[u8]> { vec![0].into_boxed_slice() } ``` To help the inliner's job, we also hoist a `self.capacity() != self.len` check in `<Vec<T>>::shrink_to_fit` and mark it as `#[inline]` too.
2018-03-31Deprecate offset_to; switch core&alloc to using offset_from insteadScott McMurray-4/+5
Bonus: might make code than uses `.len()` on slice iterators faster
2018-03-31Auto merge of #49481 - SimonSapin:core-heap, r=alexcrichtonbors-1086/+5
Move the alloc::allocator module to core::heap This is the `Alloc` trait and its dependencies.
2018-03-30Add #[must_use] to a few standard library methodsScott McMurray-0/+1
Chosen to start a precedent of using it on ones that are potentially-expensive and where using it for side effects is particularly discouraged. Discuss :)
2018-03-30Rollup merge of #49468 - glandium:cleanup, r=pnkfelixkennytm-2/+2
Remove unnecessary use core::hash in liballoc/boxed.rs It' only used for hash::Hasher, but Hasher is also imported.
2018-03-30Rollup merge of #49466 - glandium:master, r=rkruppekennytm-2/+2
Use f{32,64}::to_bits for is_zero test in vec::SpecFromElem vec::SpecFromElem provides an optimization to use calloc to fill a Vec when the element given to fill the Vec is represented by 0. For floats, the test for that currently used is `x == 0. && x.is_sign_positive()`. When compiled in a standalone function, rustc generates the following assembly: ``` xorps xmm1, xmm1 ucomisd xmm0, xmm1 setnp al sete cl and cl, al movq rax, xmm0 test rax, rax setns al and al, cl ret ``` A simpler test telling us whether the value is represented by 0, is `x.to_bits() == 0`, which rustc compiles to: ``` movq rax, xmm0 test rax, rax sete al ret ``` Not that the test is hot in any way, but it also makes it clearer what the intent in the rust code is.
2018-03-29Move the alloc::allocator module to core::heapSimon Sapin-1086/+5
This is the `Alloc` trait and its dependencies.
2018-03-29Move RangeArguments to {core::std}::ops and rename to RangeBoundsSimon Sapin-171/+15
These unstable items are deprecated: * The `std::collections::range::RangeArgument` reexport * The `std::collections::range` module.
2018-03-29Move alloc::Bound to {core,std}::opsSimon Sapin-59/+8
The stable reexport `std::collections::Bound` is now deprecated. Another deprecated reexport could be added in `alloc`, but that crate is unstable.
2018-03-29Remove unnecessary use core::hash in liballoc/boxed.rsMike Hommey-2/+2
It' only used for hash::Hasher, but Hasher is also imported.
2018-03-29Use f{32,64}::to_bits for is_zero test in vec::SpecFromElemMike Hommey-2/+2
vec::SpecFromElem provides an optimization to use calloc to fill a Vec when the element given to fill the Vec is represented by 0. For floats, the test for that currently used is `x == 0. && x.is_sign_positive()`. When compiled in a standalone function, rustc generates the following assembly: ``` xorps xmm1, xmm1 ucomisd xmm0, xmm1 setnp al sete cl and cl, al movq rax, xmm0 test rax, rax setns al and al, cl ret ``` A simpler test telling us whether the value is represented by 0, is `x.to_bits() == 0`, which rustc compiles to: ``` movq rax, xmm0 test rax, rax sete al ret ``` Not that the test is hot in any way, but it also makes it clearer what the intent in the rust code is.
2018-03-28Rollup merge of #49452 - frewsxcv:frewsxcv-vec-cap-len, r=dtolnaykennytm-3/+4
Clarify "length" wording in `Vec::with_capacity`. Fixes https://github.com/rust-lang/rust/issues/49448.
2018-03-28Rollup merge of #49400 - Diggsey:shrink-to, r=joshtriplettkennytm-2/+113
Implement `shrink_to` method on collections Fixes #49385
2018-03-28Rollup merge of #49243 - murarth:stabilize-retain, r=BurntSushikennytm-3/+1
Stabilize method `String::retain` Closes #43874
2018-03-28Clarify "length" wording in `Vec::with_capacity`.Corey Farwell-3/+4
2018-03-28Add run-pass test for repeat-generic-slice featureGuillaume Gomez-0/+19
2018-03-28Add repeat method on sliceGuillaume Gomez-53/+72
2018-03-27Rollup merge of #49401 - alercah:format, r=cramertjkennytm-7/+8
Add missing '?' to format grammar.
2018-03-27Rollup merge of #49381 - withoutboats:str_unicode, r=SimonSapinkennytm-0/+42
Add is_whitespace and is_alphanumeric to str. The other methods from `UnicodeStr` are already stable inherent methods on str, but these have not been included. r? @SimonSapin
2018-03-27Rollup merge of #48639 - varkor:sort_by_key-cached, r=blusskennytm-13/+111
Add slice::sort_by_cached_key as a memoised sort_by_key At present, `slice::sort_by_key` calls its key function twice for each comparison that is made. When the key function is expensive (which can often be the case when `sort_by_key` is chosen over `sort_by`), this can lead to very suboptimal behaviour. To address this, I've introduced a new slice method, `sort_by_cached_key`, which has identical semantic behaviour to `sort_by_key`, except that it guarantees the key function will only be called once per element. Where there are `n` elements and the key function is `O(m)`: - `slice::sort_by_cached_key` time complexity is `O(m n log m n)`, compared to `slice::sort_by_key`'s `O(m n + n log n)`. - `slice::sort_by_cached_key` space complexity remains at `O(n + m)`. (Technically, it now reserves a slice of size `n`, whereas before it reserved a slice of size `n/2`.) `slice::sort_unstable_by_key` has not been given an analogue, as it is important that unstable sorts are in-place, which is not a property that is guaranteed here. However, this also means that `slice::sort_unstable_by_key` is likely to be slower than `slice::sort_by_cached_key` when the key function does not have negligible complexity. We might want to explore this trade-off further in the future. Benchmarks (for a vector of 100 `i32`s): ``` # Lexicographic: `|x| x.to_string()` test bench_sort_by_key ... bench: 112,638 ns/iter (+/- 19,563) test bench_sort_by_cached_key ... bench: 15,038 ns/iter (+/- 4,814) # Identity: `|x| *x` test bench_sort_by_key ... bench: 1,346 ns/iter (+/- 238) test bench_sort_by_cached_key ... bench: 1,839 ns/iter (+/- 765) # Power: `|x| x.pow(31)` test bench_sort_by_key ... bench: 3,624 ns/iter (+/- 738) test bench_sort_by_cached_key ... bench: 1,997 ns/iter (+/- 311) # Abs: `|x| x.abs()` test bench_sort_by_key ... bench: 1,546 ns/iter (+/- 174) test bench_sort_by_cached_key ... bench: 1,668 ns/iter (+/- 790) ``` (So it seems functions that are single operations do perform slightly worse with this method, but for pretty much any more complex key, you're better off with this optimisation.) I've definitely found myself using expensive keys in the past and wishing this optimisation was made (e.g. for https://github.com/rust-lang/rust/pull/47415). This feels like both desirable and expected behaviour, at the small cost of slightly more stack allocation and minute degradation in performance for extremely trivial keys. Resolves #34447.
2018-03-26Add missing '?' to format grammar.Alexis Hunt-7/+8
2018-03-27Implement `shrink_to` method on collectionsDiggory Blake-2/+113
2018-03-26Remove mentions of unstable sort_by_cached key from stable documentationvarkor-8/+0
2018-03-26Auto merge of #49101 - mark-i-m:stabilize_i128, r=nagisabors-2/+2
Stabilize 128-bit integers :tada: cc #35118 EDIT: This should be merged only after the following have been merged: - [x] https://github.com/rust-lang-nursery/compiler-builtins/pull/236 - [x] https://github.com/rust-lang/book/pull/1230
2018-03-26Stabilize i128_typeMark Mansi-2/+2
2018-03-26Add is_whitespace and is_alphanumeric to str.boats-0/+42
The other methods from `UnicodeStr` are already stable inherent methods on str, but these have not been included.
2018-03-25Implement get_key_value for HashMap, BTreeMapDiggory Blake-0/+27
2018-03-25Rollup merge of #49194 - Zoxc:unsafe-generator, r=cramertjkennytm-1/+1
Make resuming generators unsafe instead of the creation of immovable generators cc @withoutboats Fixes #47787
2018-03-25Rollup merge of #49121 - varkor:stabilise-from_utf8_error_as_bytes, r=blusskennytm-2/+1
Stabilise FromUtf8Error::as_bytes Closes #40895.
2018-03-23Rollup merge of #48265 - SimonSapin:nonzero, r=KodrAusAlex Crichton-16/+14
Add 12 num::NonZero* types for primitive integers, deprecate core::nonzero RFC: https://github.com/rust-lang/rfcs/pull/2307 Tracking issue: ~~https://github.com/rust-lang/rust/issues/27730~~ https://github.com/rust-lang/rust/issues/49137 Fixes https://github.com/rust-lang/rust/issues/27730
2018-03-22Rollup merge of #49117 - nivkner:fixme_fixup3, r=estebankkennytm-1/+1
address some FIXME whose associated issues were marked as closed part of #44366
2018-03-21Stabilize method `String::retain`Murarth-3/+1
2018-03-21Make resuming generators unsafe instead of the creation of immovable ↵John Kåre Alsaker-1/+1
generators. Fixes #47787
2018-03-20Auto merge of #49190 - kennytm:rollup, r=kennytmbors-1/+1
Rollup of 17 pull requests - Successful merges: #46518, #48810, #48834, #48902, #49004, #49092, #49096, #49099, #49104, #49125, #49139, #49152, #49157, #49161, #49166, #49176, #49184 - Failed merges:
2018-03-20Auto merge of #48516 - petrochenkov:stabsl, r=nikomatsakisbors-1/+0
Stabilize slice patterns without `..` And merge `feature(advanced_slice_patterns)` into `feature(slice_patterns)`. The detailed description can be found in https://github.com/rust-lang/rust/issues/48836. Slice patterns were unstable for long time since before 1.0 due to many bugs in the implementation, now this stabilization is possible primarily due to work of @arielb1 who [wrote the new MIR-based implementation of slice patterns](https://github.com/rust-lang/rust/pull/32202) and @mikhail-m1 who [fixed one remaining class of codegen issues](https://github.com/rust-lang/rust/pull/47926). Reference PR https://github.com/rust-lang-nursery/reference/pull/259 cc https://github.com/rust-lang/rust/issues/23121 fixes #48836
2018-03-20Stabilize slice patterns without `..`Vadim Petrochenkov-1/+0
Merge `feature(advanced_slice_patterns)` into `feature(slice_patterns)`
2018-03-19Auto merge of #49058 - withoutboats:pin, r=cramertjbors-2/+100
Pin, Unpin, PinBox Implementing rust-lang/rfcs#2349 (do not merge until RFC is merged) @bors r? @cramertj
2018-03-19Docs: fix incorrect copy-paste for new `X?` in formatting stringsSimon Sapin-1/+1
2018-03-19Auto merge of #48978 - SimonSapin:debug-hex, r=KodrAusbors-0/+2
Add hexadecimal formatting of integers with fmt::Debug This can be used for integers within a larger types which implements Debug (possibly through derive) but not fmt::UpperHex or fmt::LowerHex. ```rust assert!(format!("{:02x?}", b"Foo\0") == "[46, 6f, 6f, 00]"); assert!(format!("{:02X?}", b"Foo\0") == "[46, 6F, 6F, 00]"); ``` RFC: https://github.com/rust-lang/rfcs/pull/2226 The new formatting string syntax (`x?` and `X?`) is insta-stable in this PR because I don’t know how to change a built-in proc macro’s behavior based of a feature gate. I can look into adding that, but I also strongly suspect that keeping this feature unstable for a time period would not be useful as possibly no-one would use it during that time. This PR does not add the new (public) `fmt::Formatter` proposed in the API because: * There was some skepticism on response to this part of the RFC * It is not possible to implement as-is without larger changes to `fmt`, because `Formatter` at the moment has no easy way to tell apart for example `Octal` from `Binary`: it only has a function pointer for the relevant `fmt()` method. If some integer-like type outside of `std` want to implement this behavior, another RFC will likely need to propose a different public API for `Formatter`.
2018-03-19Add stability test for sort_by_cached_keyvarkor-3/+8
2018-03-18Update tracking issue.boats-12/+12
2018-03-18Add lexicographic sorting benchmarkvarkor-0/+16
2018-03-18Check that the size optimisation is not redundantvarkor-5/+10
2018-03-18Clarify time complexityvarkor-3/+3
2018-03-17Use NonNull<_> instead of NonZero<*const _> in btree internalsSimon Sapin-16/+14
2018-03-17Stabilise FromUtf8Error::as_bytesvarkor-2/+1
Closes #40895.
2018-03-17Improve and fix documentation for sort_by_cached_keyvarkor-8/+12