Skip to content

Commit 421d981

Browse files
committed
rendy: Avoid calling from_raw_parts in favor of transmute
Now instead of repeatedly calling core::slice::from_raw_parts when drawing a character to the framebuffer, which as it turns out is a performance concern in debug mode, we transmute the included bytes of a font into the right format at compile time, so there is no runtime penalty for looking up the character (other than bounds checking of course, but that happens with *every* slice in Rust)
1 parent d2e6fd6 commit 421d981

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

src/aero_kernel/src/rendy.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ use stivale_boot::v2::StivaleFramebufferTag;
3535

3636
use crate::utils::sync::Mutex;
3737

38-
static FONT: &[u8] = include_bytes!("../../font.bin");
38+
static FONT: &[[u8; FONT_HEIGHT]; FONT_GLYPHS] =
39+
unsafe { &core::mem::transmute(*include_bytes!("../../font.bin")) };
3940

4041
// This is an example of how the rendered screen will look like:
4142
//
@@ -53,6 +54,7 @@ static FONT: &[u8] = include_bytes!("../../font.bin");
5354

5455
const FONT_WIDTH: usize = 8;
5556
const FONT_HEIGHT: usize = 16;
57+
const FONT_GLYPHS: usize = 256;
5658

5759
const DEFAULT_MARGIN: usize = 64 / 2;
5860
const TAB_SIZE: usize = 4;
@@ -553,12 +555,7 @@ impl<'this> DebugRendy<'this> {
553555

554556
let x = self.offset_x + x * FONT_WIDTH;
555557
let y = self.offset_y + y * FONT_HEIGHT;
556-
let glyph = unsafe {
557-
core::slice::from_raw_parts(
558-
FONT.as_ptr().offset(ch as isize * FONT_HEIGHT as isize) as *const u8,
559-
FONT_HEIGHT,
560-
)
561-
};
558+
let glyph = &FONT[ch as usize];
562559

563560
// naming: fx, fy for font coordinates and gx, gy for glyph coordinates
564561
for gy in 0..FONT_HEIGHT {

0 commit comments

Comments
 (0)