From b0b3b0af2d7b73c9c0cb0e1d54b125360495892f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 19 Mar 2023 10:48:42 -0700 Subject: [PATCH] update config messages use pointers now to make messages small again --- src/Surface.zig | 25 +++++++++++++++++++------ src/renderer/Metal.zig | 4 ++-- src/renderer/OpenGL.zig | 4 ++-- src/renderer/Thread.zig | 3 ++- src/renderer/message.zig | 5 ++++- src/termio/Exec.zig | 5 ++--- src/termio/Thread.zig | 5 ++++- src/termio/message.zig | 8 ++++++-- 8 files changed, 41 insertions(+), 18 deletions(-) diff --git a/src/Surface.zig b/src/Surface.zig index 7a4710645..d19f61d11 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -569,17 +569,30 @@ fn changeConfig(self: *Surface, config: *const configpkg.Config) !void { self.config.deinit(); self.config = derived; + // We need to store our configs in a heap-allocated pointer so that + // our messages aren't huge. + var renderer_config_ptr = try self.alloc.create(Renderer.DerivedConfig); + errdefer self.alloc.destroy(renderer_config_ptr); + var termio_config_ptr = try self.alloc.create(termio.Impl.DerivedConfig); + errdefer self.alloc.destroy(termio_config_ptr); + // Update our derived configurations for the renderer and termio, // then send them a message to update. - var renderer_config = try Renderer.DerivedConfig.init(self.alloc, config); - errdefer renderer_config.deinit(); - var termio_config = try termio.Impl.DerivedConfig.init(self.alloc, config); - errdefer termio_config.deinit(); + renderer_config_ptr.* = try Renderer.DerivedConfig.init(self.alloc, config); + errdefer renderer_config_ptr.deinit(); + termio_config_ptr.* = try termio.Impl.DerivedConfig.init(self.alloc, config); + errdefer termio_config_ptr.deinit(); _ = self.renderer_thread.mailbox.push(.{ - .change_config = renderer_config, + .change_config = .{ + .alloc = self.alloc, + .ptr = renderer_config_ptr, + }, }, .{ .forever = {} }); _ = self.io_thread.mailbox.push(.{ - .change_config = termio_config, + .change_config = .{ + .alloc = self.alloc, + .ptr = termio_config_ptr, + }, }, .{ .forever = {} }); // With mailbox messages sent, we have to wake them up so they process it. diff --git a/src/renderer/Metal.zig b/src/renderer/Metal.zig index b5ee0a58c..ff55c1e59 100644 --- a/src/renderer/Metal.zig +++ b/src/renderer/Metal.zig @@ -725,8 +725,8 @@ fn drawCells( } /// Update the configuration. -pub fn changeConfig(self: *Metal, config: DerivedConfig) !void { - self.config = config; +pub fn changeConfig(self: *Metal, config: *DerivedConfig) !void { + self.config = config.*; } /// Resize the screen. diff --git a/src/renderer/OpenGL.zig b/src/renderer/OpenGL.zig index 77950fb99..e44b317e2 100644 --- a/src/renderer/OpenGL.zig +++ b/src/renderer/OpenGL.zig @@ -1233,8 +1233,8 @@ fn gridSize(self: *const OpenGL, screen_size: renderer.ScreenSize) renderer.Grid } /// Update the configuration. -pub fn changeConfig(self: *OpenGL, config: DerivedConfig) !void { - self.config = config; +pub fn changeConfig(self: *OpenGL, config: *DerivedConfig) !void { + self.config = config.*; } /// Set the screen size for rendering. This will update the projection diff --git a/src/renderer/Thread.zig b/src/renderer/Thread.zig index ba956dda0..a4eac3b1a 100644 --- a/src/renderer/Thread.zig +++ b/src/renderer/Thread.zig @@ -257,7 +257,8 @@ fn drainMailbox(self: *Thread) !void { }, .change_config => |config| { - try self.renderer.changeConfig(config); + defer config.alloc.destroy(config.ptr); + try self.renderer.changeConfig(config.ptr); }, } } diff --git a/src/renderer/message.zig b/src/renderer/message.zig index 80465a2e0..7441380da 100644 --- a/src/renderer/message.zig +++ b/src/renderer/message.zig @@ -24,5 +24,8 @@ pub const Message = union(enum) { screen_size: renderer.ScreenSize, /// The derived configuration to update the renderer with. - change_config: renderer.Renderer.DerivedConfig, + change_config: struct { + alloc: Allocator, + ptr: *renderer.Renderer.DerivedConfig, + }, }; diff --git a/src/termio/Exec.zig b/src/termio/Exec.zig index cb9b157e4..bc8582e77 100644 --- a/src/termio/Exec.zig +++ b/src/termio/Exec.zig @@ -218,9 +218,8 @@ pub fn threadExit(self: *Exec, data: ThreadData) void { } /// Update the configuration. -pub fn changeConfig(self: *Exec, config: DerivedConfig) !void { - var copy = config; - defer copy.deinit(); +pub fn changeConfig(self: *Exec, config: *DerivedConfig) !void { + defer config.deinit(); // Update the configuration that we know about. // diff --git a/src/termio/Thread.zig b/src/termio/Thread.zig index 72656f00e..d034ede3d 100644 --- a/src/termio/Thread.zig +++ b/src/termio/Thread.zig @@ -150,7 +150,10 @@ fn drainMailbox(self: *Thread) !void { log.debug("mailbox message={}", .{message}); switch (message) { - .change_config => |config| try self.impl.changeConfig(config), + .change_config => |config| { + defer config.alloc.destroy(config.ptr); + try self.impl.changeConfig(config.ptr); + }, .resize => |v| self.handleResize(v), .clear_screen => |v| try self.impl.clearScreen(v.history), .write_small => |v| try self.impl.queueWrite(v.data[0..v.len]), diff --git a/src/termio/message.zig b/src/termio/message.zig index 5399b3d9d..fe4fc535d 100644 --- a/src/termio/message.zig +++ b/src/termio/message.zig @@ -29,8 +29,12 @@ pub const Message = union(enum) { padding: renderer.Padding, }; - /// The derived configuration to update the implementation with. - change_config: termio.Impl.DerivedConfig, + /// The derived configuration to update the implementation with. This + /// is allocated via the allocator and is expected to be freed when done. + change_config: struct { + alloc: Allocator, + ptr: *termio.Impl.DerivedConfig, + }, /// Resize the window. resize: Resize,