Skip to content

Commit aee0a09

Browse files
committed
Reduce allocations using bytemuck's cast_vec
1 parent d543a97 commit aee0a09

3 files changed

Lines changed: 4 additions & 6 deletions

File tree

crates/dmm-tools/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ either = "1.13.0"
1616

1717
[dependencies.bytemuck]
1818
version = "1.19.0"
19-
features = ["derive"]
19+
features = ["derive", "extern_crate_alloc"]
2020

2121
[dependencies.bumpalo]
2222
version = "3.16.0"

crates/dmm-tools/src/dmi.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,8 @@ impl Image {
133133
width: bitmap.width as u32,
134134
height: bitmap.height as u32,
135135
data: {
136-
let cast_input = bytemuck::cast_slice(bitmap.buffer.as_slice());
137-
let mut arr = Array2::default((bitmap.width, bitmap.height));
138-
arr.as_slice_mut().unwrap().copy_from_slice(cast_input);
139-
arr
136+
let cast_input = bytemuck::allocation::cast_vec(bitmap.buffer);
137+
Array2::from_shape_vec((bitmap.width, bitmap.height), cast_input).unwrap()
140138
},
141139
}
142140
}

crates/dmm-tools/src/dmi/render.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl<'a> IconRenderer<'a> {
180180
for frame in range {
181181
self.render_dirs(icon_state, &mut canvas, frame as u32);
182182

183-
let mut pixels = bytemuck::cast_slice(canvas.data.as_slice().unwrap()).to_owned();
183+
let mut pixels = bytemuck::cast_slice_mut(canvas.data.as_slice_mut().unwrap());
184184
let mut gif_frame =
185185
gif::Frame::from_rgba(canvas.width as u16, canvas.height as u16, &mut pixels);
186186
// gif::Frame delays are measured in "Frame delay in units of 10 ms."

0 commit comments

Comments
 (0)