Commit a82af82
Rollup merge of rust-lang#133598 - ChayimFriedman2:get-many-mut-detailed-err, r=scottmcm
Change `GetManyMutError` to match T-libs-api decision
That is, differentiate between out-of-bounds and overlapping indices, and remove the generic parameter `N`.
I also exported `GetManyMutError` from `alloc` (and `std`), which was apparently forgotten.
Changing the error to carry additional details means LLVM no longer generates separate short-circuiting branches for the checks, instead it generates one branch at the end. I therefore changed the code to use early returns to make LLVM generate jumps. Benchmark results between the approaches are somewhat mixed, but I chose this approach because it is significantly faster with ranges and also faster with `unwrap()`.
Benchmark (`jumps` refer to short-circuiting, `acc` is not short-circuiting):
```rust
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use my_crate::{get_many_check_valid_acc, get_many_check_valid_jumps, GetManyMutError};
mod externs {
#[unsafe(no_mangle)]
fn foo() {}
#[unsafe(no_mangle)]
fn bar() {}
#[unsafe(no_mangle)]
fn baz() {}
}
unsafe extern "C" {
safe fn foo();
safe fn bar();
safe fn baz();
}
fn bench_method(c: &mut Criterion) {
c.bench_function("jumps two usize", |b| {
b.iter(|| get_many_check_valid_jumps(&[black_box(1), black_box(5)], black_box(10)))
});
c.bench_function("jumps two usize unwrap", |b| {
b.iter(|| get_many_check_valid_jumps(&[black_box(1), black_box(5)], black_box(10)).unwrap())
});
c.bench_function("jumps two usize ok", |b| {
b.iter(|| get_many_check_valid_jumps(&[black_box(1), black_box(5)], black_box(10)).ok())
});
c.bench_function("jumps three usize", |b| {
b.iter(|| {
get_many_check_valid_jumps(&[black_box(1), black_box(5), black_box(7)], black_box(10))
})
});
c.bench_function("jumps three usize match", |b| {
b.iter(|| {
match get_many_check_valid_jumps(
&[black_box(1), black_box(5), black_box(7)],
black_box(10),
) {
Err(GetManyMutError::IndexOutOfBounds) => foo(),
Err(GetManyMutError::OverlappingIndices) => bar(),
Ok(()) => baz(),
}
})
});
c.bench_function("jumps two Range", |b| {
b.iter(|| {
get_many_check_valid_jumps(
&[black_box(1)..black_box(5), black_box(7)..black_box(8)],
black_box(10),
)
})
});
c.bench_function("jumps two RangeInclusive", |b| {
b.iter(|| {
get_many_check_valid_jumps(
&[black_box(1)..=black_box(5), black_box(7)..=black_box(8)],
black_box(10),
)
})
});
c.bench_function("acc two usize", |b| {
b.iter(|| get_many_check_valid_acc(&[black_box(1), black_box(5)], black_box(10)))
});
c.bench_function("acc two usize unwrap", |b| {
b.iter(|| get_many_check_valid_acc(&[black_box(1), black_box(5)], black_box(10)).unwrap())
});
c.bench_function("acc two usize ok", |b| {
b.iter(|| get_many_check_valid_acc(&[black_box(1), black_box(5)], black_box(10)).ok())
});
c.bench_function("acc three usize", |b| {
b.iter(|| {
get_many_check_valid_acc(&[black_box(1), black_box(5), black_box(7)], black_box(10))
})
});
c.bench_function("acc three usize match", |b| {
b.iter(|| {
match get_many_check_valid_jumps(
&[black_box(1), black_box(5), black_box(7)],
black_box(10),
) {
Err(GetManyMutError::IndexOutOfBounds) => foo(),
Err(GetManyMutError::OverlappingIndices) => bar(),
Ok(()) => baz(),
}
})
});
c.bench_function("acc two Range", |b| {
b.iter(|| {
get_many_check_valid_acc(
&[black_box(1)..black_box(5), black_box(7)..black_box(8)],
black_box(10),
)
})
});
c.bench_function("acc two RangeInclusive", |b| {
b.iter(|| {
get_many_check_valid_acc(
&[black_box(1)..=black_box(5), black_box(7)..=black_box(8)],
black_box(10),
)
})
});
}
criterion_group!(benches, bench_method);
criterion_main!(benches);
```
Benchmark results:
```none
jumps two usize time: [586.44 ps 590.20 ps 594.50 ps]
jumps two usize unwrap time: [390.44 ps 393.63 ps 397.44 ps]
jumps two usize ok time: [585.52 ps 591.74 ps 599.38 ps]
jumps three usize time: [976.51 ps 983.79 ps 991.51 ps]
jumps three usize match time: [390.82 ps 393.80 ps 397.07 ps]
jumps two Range time: [1.2583 ns 1.2640 ns 1.2695 ns]
jumps two RangeInclusive time: [1.2673 ns 1.2770 ns 1.2877 ns]
acc two usize time: [592.63 ps 596.44 ps 600.52 ps]
acc two usize unwrap time: [582.65 ps 587.07 ps 591.90 ps]
acc two usize ok time: [581.59 ps 587.82 ps 595.71 ps]
acc three usize time: [894.69 ps 901.23 ps 908.24 ps]
acc three usize match time: [392.68 ps 395.73 ps 399.17 ps]
acc two Range time: [1.5531 ns 1.5617 ns 1.5711 ns]
acc two RangeInclusive time: [1.5746 ns 1.5840 ns 1.5939 ns]
```3 files changed
+34
-28
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
| 31 | + | |
30 | 32 | | |
31 | 33 | | |
32 | 34 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1076 | 1076 | | |
1077 | 1077 | | |
1078 | 1078 | | |
1079 | | - | |
| 1079 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4629 | 4629 | | |
4630 | 4630 | | |
4631 | 4631 | | |
4632 | | - | |
| 4632 | + | |
4633 | 4633 | | |
4634 | 4634 | | |
4635 | 4635 | | |
4636 | | - | |
4637 | | - | |
4638 | | - | |
| 4636 | + | |
4639 | 4637 | | |
4640 | 4638 | | |
4641 | 4639 | | |
| |||
4978 | 4976 | | |
4979 | 4977 | | |
4980 | 4978 | | |
4981 | | - | |
| 4979 | + | |
| 4980 | + | |
| 4981 | + | |
| 4982 | + | |
4982 | 4983 | | |
4983 | 4984 | | |
4984 | | - | |
4985 | 4985 | | |
4986 | | - | |
| 4986 | + | |
| 4987 | + | |
| 4988 | + | |
4987 | 4989 | | |
4988 | | - | |
| 4990 | + | |
| 4991 | + | |
| 4992 | + | |
4989 | 4993 | | |
4990 | 4994 | | |
4991 | | - | |
| 4995 | + | |
4992 | 4996 | | |
4993 | 4997 | | |
4994 | | - | |
| 4998 | + | |
4995 | 4999 | | |
4996 | 5000 | | |
4997 | 5001 | | |
4998 | | - | |
| 5002 | + | |
| 5003 | + | |
4999 | 5004 | | |
5000 | 5005 | | |
5001 | 5006 | | |
5002 | 5007 | | |
5003 | 5008 | | |
| 5009 | + | |
5004 | 5010 | | |
5005 | 5011 | | |
5006 | | - | |
5007 | | - | |
| 5012 | + | |
| 5013 | + | |
5008 | 5014 | | |
5009 | 5015 | | |
5010 | | - | |
5011 | | - | |
5012 | | - | |
5013 | | - | |
5014 | | - | |
| 5016 | + | |
| 5017 | + | |
| 5018 | + | |
| 5019 | + | |
| 5020 | + | |
| 5021 | + | |
5015 | 5022 | | |
5016 | 5023 | | |
5017 | 5024 | | |
5018 | | - | |
| 5025 | + | |
5019 | 5026 | | |
5020 | | - | |
5021 | | - | |
5022 | | - | |
5023 | | - | |
5024 | | - | |
5025 | | - | |
5026 | | - | |
5027 | | - | |
| 5027 | + | |
| 5028 | + | |
| 5029 | + | |
| 5030 | + | |
| 5031 | + | |
5028 | 5032 | | |
5029 | 5033 | | |
5030 | 5034 | | |
| |||
0 commit comments