Skip to content

Commit 3c4590f

Browse files
committed
Auto merge of #68781 - ssomers:btree_miri_relief, r=RalfJung
BTree: lighten the load on Miri Reduce the amount of work Miri ploughs through in btree code, in particular on `test_clone_from` (which takes up 5 minutes on my machine). r? @crgl
2 parents a643ee8 + da226dd commit 3c4590f

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

src/liballoc/collections/btree/map.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl<K: Clone, V: Clone> BTreeClone for BTreeMap<K, V> {
227227
impl<K: Clone + Ord, V: Clone> BTreeClone for BTreeMap<K, V> {
228228
fn clone_from(&mut self, other: &Self) {
229229
// This truncates `self` to `other.len()` by calling `split_off` on
230-
// the first key after `other.len()` elements if it exists
230+
// the first key after `other.len()` elements if it exists.
231231
let split_off_key = if self.len() > other.len() {
232232
let diff = self.len() - other.len();
233233
if diff <= other.len() {
@@ -247,19 +247,18 @@ impl<K: Clone + Ord, V: Clone> BTreeClone for BTreeMap<K, V> {
247247
// After truncation, `self` is at most as long as `other` so this loop
248248
// replaces every key-value pair in `self`. Since `oiter` is in sorted
249249
// order and the structure of the `BTreeMap` stays the same,
250-
// the BTree invariants are maintained at the end of the loop
250+
// the BTree invariants are maintained at the end of the loop.
251251
while !siter.is_empty() {
252252
if let Some((ok, ov)) = oiter.next() {
253-
// SAFETY: This is safe because the `siter.front != siter.back` check
254-
// ensures that `siter` is nonempty
253+
// SAFETY: This is safe because `siter` is nonempty.
255254
let (sk, sv) = unsafe { siter.next_unchecked() };
256255
sk.clone_from(ok);
257256
sv.clone_from(ov);
258257
} else {
259258
break;
260259
}
261260
}
262-
// If `other` is longer than `self`, the remaining elements are inserted
261+
// If `other` is longer than `self`, the remaining elements are inserted.
263262
self.extend(oiter.map(|(k, v)| ((*k).clone(), (*v).clone())));
264263
}
265264
}

src/liballoc/tests/btree/map.rs

+32-20
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn test_basic_large() {
1515
#[cfg(not(miri))] // Miri is too slow
1616
let size = 10000;
1717
#[cfg(miri)]
18-
let size = 200;
18+
let size = 144; // to obtain height 3 tree (having edges to both kinds of nodes)
1919
assert_eq!(map.len(), 0);
2020

2121
for i in 0..size {
@@ -381,8 +381,8 @@ fn test_range_small() {
381381
}
382382

383383
#[test]
384-
fn test_range_depth_2() {
385-
// Assuming that node.CAPACITY is 11, having 12 pairs implies a depth 2 tree
384+
fn test_range_height_2() {
385+
// Assuming that node.CAPACITY is 11, having 12 pairs implies a height 2 tree
386386
// with 2 leaves. Depending on details we don't want or need to rely upon,
387387
// the single key at the root will be 6 or 7.
388388

@@ -524,7 +524,7 @@ fn test_range_1000() {
524524
#[cfg(not(miri))] // Miri is too slow
525525
let size = 1000;
526526
#[cfg(miri)]
527-
let size = 200;
527+
let size = 144; // to obtain height 3 tree (having edges to both kinds of nodes)
528528
let map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
529529

530530
fn test(map: &BTreeMap<u32, u32>, size: u32, min: Bound<&u32>, max: Bound<&u32>) {
@@ -561,14 +561,15 @@ fn test_range_borrowed_key() {
561561

562562
#[test]
563563
fn test_range() {
564-
#[cfg(not(miri))] // Miri is too slow
565564
let size = 200;
565+
#[cfg(not(miri))] // Miri is too slow
566+
let step = 1;
566567
#[cfg(miri)]
567-
let size = 30;
568+
let step = 66;
568569
let map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
569570

570-
for i in 0..size {
571-
for j in i..size {
571+
for i in (0..size).step_by(step) {
572+
for j in (i..size).step_by(step) {
572573
let mut kvs = map.range((Included(&i), Included(&j))).map(|(&k, &v)| (k, v));
573574
let mut pairs = (i..=j).map(|i| (i, i));
574575

@@ -583,14 +584,15 @@ fn test_range() {
583584

584585
#[test]
585586
fn test_range_mut() {
586-
#[cfg(not(miri))] // Miri is too slow
587587
let size = 200;
588+
#[cfg(not(miri))] // Miri is too slow
589+
let step = 1;
588590
#[cfg(miri)]
589-
let size = 30;
591+
let step = 66;
590592
let mut map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
591593

592-
for i in 0..size {
593-
for j in i..size {
594+
for i in (0..size).step_by(step) {
595+
for j in (i..size).step_by(step) {
594596
let mut kvs = map.range_mut((Included(&i), Included(&j))).map(|(&k, &mut v)| (k, v));
595597
let mut pairs = (i..=j).map(|i| (i, i));
596598

@@ -758,10 +760,7 @@ fn test_bad_zst() {
758760
#[test]
759761
fn test_clone() {
760762
let mut map = BTreeMap::new();
761-
#[cfg(not(miri))] // Miri is too slow
762-
let size = 100;
763-
#[cfg(miri)]
764-
let size = 30;
763+
let size = 12; // to obtain height 2 tree (having edges to leaf nodes)
765764
assert_eq!(map.len(), 0);
766765

767766
for i in 0..size {
@@ -788,24 +787,36 @@ fn test_clone() {
788787
assert_eq!(map.len(), size / 2 - i - 1);
789788
assert_eq!(map, map.clone());
790789
}
790+
791+
// Full 2-level and minimal 3-level tree (sizes 143, 144 -- the only ones we clone for).
792+
for i in 1..=144 {
793+
assert_eq!(map.insert(i, i), None);
794+
assert_eq!(map.len(), i);
795+
if i >= 143 {
796+
assert_eq!(map, map.clone());
797+
}
798+
}
791799
}
792800

793801
#[test]
794802
fn test_clone_from() {
795803
let mut map1 = BTreeMap::new();
796-
let size = 30;
804+
let max_size = 12; // to obtain height 2 tree (having edges to leaf nodes)
797805

798-
for i in 0..size {
806+
// Range to max_size inclusive, because i is the size of map1 being tested.
807+
for i in 0..=max_size {
799808
let mut map2 = BTreeMap::new();
800809
for j in 0..i {
801810
let mut map1_copy = map2.clone();
802-
map1_copy.clone_from(&map1);
811+
map1_copy.clone_from(&map1); // small cloned from large
803812
assert_eq!(map1_copy, map1);
804813
let mut map2_copy = map1.clone();
805-
map2_copy.clone_from(&map2);
814+
map2_copy.clone_from(&map2); // large cloned from small
806815
assert_eq!(map2_copy, map2);
807816
map2.insert(100 * j + 1, 2 * j + 1);
808817
}
818+
map2.clone_from(&map1); // same length
819+
assert_eq!(map2, map1);
809820
map1.insert(i, 10 * i);
810821
}
811822
}
@@ -956,6 +967,7 @@ create_append_test!(test_append_145, 145);
956967
// Tests for several randomly chosen sizes.
957968
create_append_test!(test_append_170, 170);
958969
create_append_test!(test_append_181, 181);
970+
#[cfg(not(miri))] // Miri is too slow
959971
create_append_test!(test_append_239, 239);
960972
#[cfg(not(miri))] // Miri is too slow
961973
create_append_test!(test_append_1700, 1700);

0 commit comments

Comments
 (0)