From 9b0f54b111fc7aef2cca68c118b1621da2c61029 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:12:56 -0300 Subject: [PATCH] chore: allocate sprite array on heap Each Sprite optional is 10 bytes meaning I'm allocating 1.28Kb on the stack which isn't necessary. --- src/core/ppu.zig | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/core/ppu.zig b/src/core/ppu.zig index 1465045..6211c43 100644 --- a/src/core/ppu.zig +++ b/src/core/ppu.zig @@ -43,13 +43,16 @@ pub const Ppu = struct { framebuf: FrameBuffer, alloc: Allocator, - scanline_sprites: [128]?Sprite, + scanline_sprites: *[128]?Sprite, scanline: Scanline, pub fn init(allocator: Allocator, sched: *Scheduler) !Self { // Queue first Hblank sched.push(.Draw, 240 * 4); + const sprites = try allocator.create([128]?Sprite); + sprites.* = [_]?Sprite{null} ** 128; + return Self{ .vram = try Vram.init(allocator), .palette = try Palette.init(allocator), @@ -70,11 +73,12 @@ pub const Ppu = struct { .bldy = .{ .raw = 0x0000 }, .scanline = try Scanline.init(allocator), - .scanline_sprites = [_]?Sprite{null} ** 128, + .scanline_sprites = sprites, }; } pub fn deinit(self: *Self) void { + self.alloc.destroy(self.scanline_sprites); self.framebuf.deinit(); self.scanline.deinit(); self.vram.deinit(); @@ -394,7 +398,7 @@ pub const Ppu = struct { // Reset Current Scanline Pixel Buffer and list of fetched sprites // in prep for next scanline self.scanline.reset(); - std.mem.set(?Sprite, &self.scanline_sprites, null); + std.mem.set(?Sprite, self.scanline_sprites, null); }, 0x1 => { const fb_base = framebuf_pitch * @as(usize, scanline); @@ -421,7 +425,7 @@ pub const Ppu = struct { // Reset Current Scanline Pixel Buffer and list of fetched sprites // in prep for next scanline self.scanline.reset(); - std.mem.set(?Sprite, &self.scanline_sprites, null); + std.mem.set(?Sprite, self.scanline_sprites, null); }, 0x2 => { const fb_base = framebuf_pitch * @as(usize, scanline); @@ -447,7 +451,7 @@ pub const Ppu = struct { // Reset Current Scanline Pixel Buffer and list of fetched sprites // in prep for next scanline self.scanline.reset(); - std.mem.set(?Sprite, &self.scanline_sprites, null); + std.mem.set(?Sprite, self.scanline_sprites, null); }, 0x3 => { const vram_base = width * @sizeOf(u16) * @as(usize, scanline);