Skip to content

Commit

Permalink
Squashed 'library/' content from commit f461edda8cb
Browse files Browse the repository at this point in the history
git-subtree-dir: library
git-subtree-split: f461edda8cb79b32ec1d2ba26988c3de8061a77a
  • Loading branch information
jaisnan committed May 31, 2024
0 parents commit 2faab31
Show file tree
Hide file tree
Showing 1,269 changed files with 402,919 additions and 0 deletions.
54 changes: 54 additions & 0 deletions alloc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[package]
name = "alloc"
version = "0.0.0"
license = "MIT OR Apache-2.0"
repository = "https://github.com/rust-lang/rust.git"
description = "The Rust core allocation and collections library"
autotests = false
autobenches = false
edition = "2021"

[dependencies]
core = { path = "../core" }
compiler_builtins = { version = "0.1.40", features = ['rustc-dep-of-std'] }

[dev-dependencies]
rand = { version = "0.8.5", default-features = false, features = ["alloc"] }
rand_xorshift = "0.3.0"

[[test]]
name = "alloctests"
path = "tests/lib.rs"

[[bench]]
name = "allocbenches"
path = "benches/lib.rs"
test = true

[[bench]]
name = "vec_deque_append_bench"
path = "benches/vec_deque_append.rs"
harness = false

[features]
compiler-builtins-mem = ['compiler_builtins/mem']
compiler-builtins-c = ["compiler_builtins/c"]
compiler-builtins-no-asm = ["compiler_builtins/no-asm"]
compiler-builtins-mangled-names = ["compiler_builtins/mangled-names"]
compiler-builtins-weak-intrinsics = ["compiler_builtins/weak-intrinsics"]
# Make panics and failed asserts immediately abort without formatting any message
panic_immediate_abort = ["core/panic_immediate_abort"]
# Choose algorithms that are optimized for binary size instead of runtime performance
optimize_for_size = ["core/optimize_for_size"]

[lints.rust.unexpected_cfgs]
level = "warn"
# x.py uses beta cargo, so `check-cfg` entries do not yet take effect
# for rust-lang/rust. But for users of `-Zbuild-std` it does.
# The unused warning is waiting for rust-lang/cargo#13925 to reach beta.
check-cfg = [
'cfg(bootstrap)',
'cfg(no_global_oom_handling)',
'cfg(no_rc)',
'cfg(no_sync)',
]
91 changes: 91 additions & 0 deletions alloc/benches/binary_heap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use std::collections::BinaryHeap;

use rand::seq::SliceRandom;
use test::{black_box, Bencher};

#[bench]
fn bench_find_smallest_1000(b: &mut Bencher) {
let mut rng = crate::bench_rng();
let mut vec: Vec<u32> = (0..100_000).collect();
vec.shuffle(&mut rng);

b.iter(|| {
let mut iter = vec.iter().copied();
let mut heap: BinaryHeap<_> = iter.by_ref().take(1000).collect();

for x in iter {
let mut max = heap.peek_mut().unwrap();
// This comparison should be true only 1% of the time.
// Unnecessary `sift_down`s will degrade performance
if x < *max {
*max = x;
}
}

heap
})
}

#[bench]
fn bench_peek_mut_deref_mut(b: &mut Bencher) {
let mut bheap = BinaryHeap::from(vec![42]);
let vec: Vec<u32> = (0..1_000_000).collect();

b.iter(|| {
let vec = black_box(&vec);
let mut peek_mut = bheap.peek_mut().unwrap();
// The compiler shouldn't be able to optimize away the `sift_down`
// assignment in `PeekMut`'s `DerefMut` implementation since
// the loop might not run.
for &i in vec.iter() {
*peek_mut = i;
}
// Remove the already minimal overhead of the sift_down
std::mem::forget(peek_mut);
})
}

#[bench]
fn bench_from_vec(b: &mut Bencher) {
let mut rng = crate::bench_rng();
let mut vec: Vec<u32> = (0..100_000).collect();
vec.shuffle(&mut rng);

b.iter(|| BinaryHeap::from(vec.clone()))
}

#[bench]
fn bench_into_sorted_vec(b: &mut Bencher) {
let bheap: BinaryHeap<i32> = (0..10_000).collect();

b.iter(|| bheap.clone().into_sorted_vec())
}

#[bench]
fn bench_push(b: &mut Bencher) {
let mut bheap = BinaryHeap::with_capacity(50_000);
let mut rng = crate::bench_rng();
let mut vec: Vec<u32> = (0..50_000).collect();
vec.shuffle(&mut rng);

b.iter(|| {
for &i in vec.iter() {
bheap.push(i);
}
black_box(&mut bheap);
bheap.clear();
})
}

#[bench]
fn bench_pop(b: &mut Bencher) {
let mut bheap = BinaryHeap::with_capacity(10_000);

b.iter(|| {
bheap.extend((0..10_000).rev());
black_box(&mut bheap);
while let Some(elem) = bheap.pop() {
black_box(elem);
}
})
}
Loading

0 comments on commit 2faab31

Please sign in to comment.