Skip to content

Commit 9f862ad

Browse files
authored
Use EnumMap::from_fn (#1323)
The `enum-map` crate introduced `EnumMap::from_fn` in 2.7.0. We use that instead of constructing an array and then calling `EnumMap::from_array`. We also remove the utility function `rust_util::array_from_fn` because it is no longer needed. Related issue: #921
1 parent a592f87 commit 9f862ad

File tree

2 files changed

+2
-28
lines changed

2 files changed

+2
-28
lines changed

src/scheduler/scheduler.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::global_state::GcStatus;
1111
use crate::mmtk::MMTK;
1212
use crate::util::opaque_pointer::*;
1313
use crate::util::options::AffinityKind;
14-
use crate::util::rust_util::array_from_fn;
1514
use crate::vm::Collection;
1615
use crate::vm::VMBinding;
1716
use crate::Plan;
@@ -43,12 +42,10 @@ impl<VM: VMBinding> GCWorkScheduler<VM> {
4342
let worker_group = WorkerGroup::new(num_workers);
4443

4544
// Create work buckets for workers.
46-
// TODO: Replace `array_from_fn` with `std::array::from_fn` after bumping MSRV.
47-
let mut work_buckets = EnumMap::from_array(array_from_fn(|stage_num| {
48-
let stage = WorkBucketStage::from_usize(stage_num);
45+
let mut work_buckets = EnumMap::from_fn(|stage| {
4946
let active = stage == WorkBucketStage::Unconstrained;
5047
WorkBucket::new(active, worker_monitor.clone())
51-
}));
48+
});
5249

5350
// Set the open condition of each bucket.
5451
{

src/util/rust_util/mod.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -105,29 +105,6 @@ impl<T> std::ops::Deref for InitializeOnce<T> {
105105

106106
unsafe impl<T> Sync for InitializeOnce<T> {}
107107

108-
/// This implements `std::array::from_fn` introduced in Rust 1.63.
109-
/// We should replace this with the standard counterpart after bumping MSRV,
110-
/// but we also need to evaluate whether it would use too much stack space (see code comments).
111-
pub(crate) fn array_from_fn<T, const N: usize, F>(mut cb: F) -> [T; N]
112-
where
113-
F: FnMut(usize) -> T,
114-
{
115-
// Note on unsafety: An alternative to the unsafe implementation below is creating a fixed
116-
// array of `[0, 1, 2, ..., N-1]` and using the `.map(cb)` method to create the result.
117-
// However, the `array::map` method implemented in the standard library consumes a surprisingly
118-
// large amount of stack space. For VMs that run on confined stacks, such as JikesRVM, that
119-
// would cause stack overflow. Therefore we implement it manually using unsafe primitives.
120-
let mut result_array: MaybeUninit<[T; N]> = MaybeUninit::zeroed();
121-
let array_ptr = result_array.as_mut_ptr();
122-
for index in 0..N {
123-
let item = cb(index);
124-
unsafe {
125-
std::ptr::addr_of_mut!((*array_ptr)[index]).write(item);
126-
}
127-
}
128-
unsafe { result_array.assume_init() }
129-
}
130-
131108
/// Create a formatted string that makes the best effort idenfying the current process and thread.
132109
pub fn debug_process_thread_id() -> String {
133110
let pid = unsafe { libc::getpid() };

0 commit comments

Comments
 (0)