From ef29520cf567d53c2b8cc1bc8336fa55c7406a99 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 19 Jul 2024 21:51:51 +0200 Subject: [PATCH] Avoid ptr2int2ptr casts in GraphicScreen::sync Also use pointer arithmetic on *mut u32 over manual multiplication by 4. The ptr2int2ptr casts pessimize optimizations. --- graphics/vesad/src/screen.rs | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/graphics/vesad/src/screen.rs b/graphics/vesad/src/screen.rs index 636fdfec42..2369aafae8 100644 --- a/graphics/vesad/src/screen.rs +++ b/graphics/vesad/src/screen.rs @@ -52,11 +52,7 @@ impl GraphicScreen { for _y in 0..cmp::min(height, self.height) { unsafe { - ptr::copy( - old_ptr as *const u8, - new_ptr as *mut u8, - cmp::min(width, self.width) * 4, - ); + ptr::copy(old_ptr, new_ptr, cmp::min(width, self.width)); if width > self.width { ptr::write_bytes( new_ptr.offset(self.width as isize), @@ -145,22 +141,22 @@ impl GraphicScreen { let end_y = cmp::min(self.height, y + h); let start_x = cmp::min(self.width, x); - let len = (cmp::min(self.width, x + w) - start_x) * 4; + let row_pixel_count = cmp::min(self.width, x + w) - start_x; - let mut offscreen_ptr = self.offscreen.as_mut_ptr() as usize; - let mut onscreen_ptr = onscreen.as_mut_ptr() as usize; + let mut offscreen_ptr = self.offscreen.as_mut_ptr(); + let mut onscreen_ptr = onscreen.as_mut_ptr(); - offscreen_ptr += (y * self.width + start_x) * 4; - onscreen_ptr += (y * stride + start_x) * 4; + unsafe { + offscreen_ptr = offscreen_ptr.add(y * self.width + start_x); + onscreen_ptr = onscreen_ptr.add(y * stride + start_x); - let mut rows = end_y - start_y; - while rows > 0 { - unsafe { - ptr::copy(offscreen_ptr as *const u8, onscreen_ptr as *mut u8, len); + let mut rows = end_y - start_y; + while rows > 0 { + ptr::copy(offscreen_ptr, onscreen_ptr, row_pixel_count); + offscreen_ptr = offscreen_ptr.add(self.width); + onscreen_ptr = onscreen_ptr.add(stride); + rows -= 1; } - offscreen_ptr += self.width * 4; - onscreen_ptr += stride * 4; - rows -= 1; } } }