Skip to content

Commit 2faab31

Browse files
committed
Squashed 'library/' content from commit f461edda8cb
git-subtree-dir: library git-subtree-split: f461edda8cb79b32ec1d2ba26988c3de8061a77a
0 parents  commit 2faab31

File tree

1,269 files changed

+402919
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,269 files changed

+402919
-0
lines changed

alloc/Cargo.toml

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
[package]
2+
name = "alloc"
3+
version = "0.0.0"
4+
license = "MIT OR Apache-2.0"
5+
repository = "https://github.com/rust-lang/rust.git"
6+
description = "The Rust core allocation and collections library"
7+
autotests = false
8+
autobenches = false
9+
edition = "2021"
10+
11+
[dependencies]
12+
core = { path = "../core" }
13+
compiler_builtins = { version = "0.1.40", features = ['rustc-dep-of-std'] }
14+
15+
[dev-dependencies]
16+
rand = { version = "0.8.5", default-features = false, features = ["alloc"] }
17+
rand_xorshift = "0.3.0"
18+
19+
[[test]]
20+
name = "alloctests"
21+
path = "tests/lib.rs"
22+
23+
[[bench]]
24+
name = "allocbenches"
25+
path = "benches/lib.rs"
26+
test = true
27+
28+
[[bench]]
29+
name = "vec_deque_append_bench"
30+
path = "benches/vec_deque_append.rs"
31+
harness = false
32+
33+
[features]
34+
compiler-builtins-mem = ['compiler_builtins/mem']
35+
compiler-builtins-c = ["compiler_builtins/c"]
36+
compiler-builtins-no-asm = ["compiler_builtins/no-asm"]
37+
compiler-builtins-mangled-names = ["compiler_builtins/mangled-names"]
38+
compiler-builtins-weak-intrinsics = ["compiler_builtins/weak-intrinsics"]
39+
# Make panics and failed asserts immediately abort without formatting any message
40+
panic_immediate_abort = ["core/panic_immediate_abort"]
41+
# Choose algorithms that are optimized for binary size instead of runtime performance
42+
optimize_for_size = ["core/optimize_for_size"]
43+
44+
[lints.rust.unexpected_cfgs]
45+
level = "warn"
46+
# x.py uses beta cargo, so `check-cfg` entries do not yet take effect
47+
# for rust-lang/rust. But for users of `-Zbuild-std` it does.
48+
# The unused warning is waiting for rust-lang/cargo#13925 to reach beta.
49+
check-cfg = [
50+
'cfg(bootstrap)',
51+
'cfg(no_global_oom_handling)',
52+
'cfg(no_rc)',
53+
'cfg(no_sync)',
54+
]

alloc/benches/binary_heap.rs

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use std::collections::BinaryHeap;
2+
3+
use rand::seq::SliceRandom;
4+
use test::{black_box, Bencher};
5+
6+
#[bench]
7+
fn bench_find_smallest_1000(b: &mut Bencher) {
8+
let mut rng = crate::bench_rng();
9+
let mut vec: Vec<u32> = (0..100_000).collect();
10+
vec.shuffle(&mut rng);
11+
12+
b.iter(|| {
13+
let mut iter = vec.iter().copied();
14+
let mut heap: BinaryHeap<_> = iter.by_ref().take(1000).collect();
15+
16+
for x in iter {
17+
let mut max = heap.peek_mut().unwrap();
18+
// This comparison should be true only 1% of the time.
19+
// Unnecessary `sift_down`s will degrade performance
20+
if x < *max {
21+
*max = x;
22+
}
23+
}
24+
25+
heap
26+
})
27+
}
28+
29+
#[bench]
30+
fn bench_peek_mut_deref_mut(b: &mut Bencher) {
31+
let mut bheap = BinaryHeap::from(vec![42]);
32+
let vec: Vec<u32> = (0..1_000_000).collect();
33+
34+
b.iter(|| {
35+
let vec = black_box(&vec);
36+
let mut peek_mut = bheap.peek_mut().unwrap();
37+
// The compiler shouldn't be able to optimize away the `sift_down`
38+
// assignment in `PeekMut`'s `DerefMut` implementation since
39+
// the loop might not run.
40+
for &i in vec.iter() {
41+
*peek_mut = i;
42+
}
43+
// Remove the already minimal overhead of the sift_down
44+
std::mem::forget(peek_mut);
45+
})
46+
}
47+
48+
#[bench]
49+
fn bench_from_vec(b: &mut Bencher) {
50+
let mut rng = crate::bench_rng();
51+
let mut vec: Vec<u32> = (0..100_000).collect();
52+
vec.shuffle(&mut rng);
53+
54+
b.iter(|| BinaryHeap::from(vec.clone()))
55+
}
56+
57+
#[bench]
58+
fn bench_into_sorted_vec(b: &mut Bencher) {
59+
let bheap: BinaryHeap<i32> = (0..10_000).collect();
60+
61+
b.iter(|| bheap.clone().into_sorted_vec())
62+
}
63+
64+
#[bench]
65+
fn bench_push(b: &mut Bencher) {
66+
let mut bheap = BinaryHeap::with_capacity(50_000);
67+
let mut rng = crate::bench_rng();
68+
let mut vec: Vec<u32> = (0..50_000).collect();
69+
vec.shuffle(&mut rng);
70+
71+
b.iter(|| {
72+
for &i in vec.iter() {
73+
bheap.push(i);
74+
}
75+
black_box(&mut bheap);
76+
bheap.clear();
77+
})
78+
}
79+
80+
#[bench]
81+
fn bench_pop(b: &mut Bencher) {
82+
let mut bheap = BinaryHeap::with_capacity(10_000);
83+
84+
b.iter(|| {
85+
bheap.extend((0..10_000).rev());
86+
black_box(&mut bheap);
87+
while let Some(elem) = bheap.pop() {
88+
black_box(elem);
89+
}
90+
})
91+
}

0 commit comments

Comments
 (0)