Skip to content

Commit 420f65d

Browse files
committed
art: avoid constructing boxed arrays on the stack
I was hoping for copy elision here, but it seems like this might overflow on Windows platforms. Until rust-lang/rust#53827 gets a proper fix, this may be the best we can do, even if it's uglier. (We still construct the inner dimension on the stack, which seems fine.)
1 parent dcd26fc commit 420f65d

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/art.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,14 @@ impl FlowField {
552552
ff
553553
}
554554

555+
fn constant_flow_field(theta: f64) -> Box<[[f64; FLOW_FIELD_ROWS]; FLOW_FIELD_COLS]> {
556+
let vec_of_arrays = vec![[theta; FLOW_FIELD_ROWS]; FLOW_FIELD_COLS];
557+
let slice_of_arrays = vec_of_arrays.into_boxed_slice();
558+
slice_of_arrays.try_into().unwrap()
559+
}
560+
555561
fn raw_linear(default_theta: f64) -> Self {
556-
FlowField(Box::new(
557-
[[default_theta; FLOW_FIELD_ROWS]; FLOW_FIELD_COLS],
558-
))
562+
FlowField(Self::constant_flow_field(default_theta))
559563
}
560564

561565
fn raw_circular(
@@ -574,7 +578,7 @@ impl FlowField {
574578
}
575579
rot = pi(rot);
576580

577-
let mut flow_points = Box::new([[0.0; FLOW_FIELD_ROWS]; FLOW_FIELD_COLS]);
581+
let mut flow_points = Self::constant_flow_field(0.0);
578582

579583
let cx = {
580584
let fst = rng.uniform(w(0.0), w(1.0));

src/sectors.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ impl Sectors {
4141
pub fn new(config: &Config, left: f64, right: f64, top: f64, bottom: f64) -> Self {
4242
let ix = Indexer::new(f64::min(left, right), f64::max(left, right));
4343
let iy = Indexer::new(f64::min(top, bottom), f64::max(top, bottom));
44-
let sectors = Box::new(std::array::from_fn(|_| std::array::from_fn(|_| Vec::new())));
44+
let sectors = {
45+
const EMPTY_SECTOR: Vec<Collider> = Vec::new();
46+
let vec_of_arrays = vec![[EMPTY_SECTOR; NUM_SECTORS]; NUM_SECTORS];
47+
let slice_of_arrays = vec_of_arrays.into_boxed_slice();
48+
slice_of_arrays.try_into().unwrap()
49+
};
4550
Sectors {
4651
fast_collisions: config.fast_collisions,
4752
ix,

0 commit comments

Comments
 (0)