fix-deviant(renderer): don't over-allocate Metal buffers by sizeof(T)

Intentional deviation from Ghostty 1.3.1 (DEVIANT_FIXES.md row 34).
When a Metal buffer grows, Zig's renderer/metal/buffer.zig:89 and :146
compute the new size as twice the required *byte* count and then
multiply by @sizeOf(T) again, so large cell buffers allocate
sizeof(T) times more GPU memory than intended. The Rust Metal backend
(since bab49ee) allocates exactly twice the required bytes; this commit
factors the computation into metal_layout::grown_buffer_bytes, adds a
regression test and records the deviation.

Test Plan:
- cargo test -p ghostty-renderer
  grown_buffer_bytes_is_double_the_required_bytes_not_scaled_by_size_of_t
- just check-macos
This commit is contained in:
ddidderr committed 2026-10-12 16:23:54 +02:00
1 parent 132f7c27ba
commit 2040677b05
3 files changed
+28 -3

No files matched your search

+4 -3
View File
@@ -9,7 +9,7 @@ use objc2_metal::{MTLBuffer, MTLDevice as _, MTLResourceOptions};
use super::{Device, Error};
use crate::{
graphics_api::{ApiError, GpuBuffer},
metal_layout::{ResourceOptions, StorageMode},
metal_layout::{ResourceOptions, StorageMode, grown_buffer_bytes},
};
/// Options for initializing a buffer.
@@ -124,8 +124,9 @@ impl<T: Pod> Buffer<T> {
// require (the old buffer is released when it is replaced).
//
// NOTE: Zig multiplies this size by `@sizeOf(T)` a second time,
// over-allocating; we allocate exactly double.
let size = req_bytes * 2;
// over-allocating; we allocate exactly double
// (see `grown_buffer_bytes`).
let size = grown_buffer_bytes(req_bytes);
self.buffer = self
.opts
.device