Skip to content

renderer/metal: fix 2x sizeof over-allocation in Buffer.sync - #13490

Merged
mitchellh merged 1 commit into
ghostty-org:mainfrom
Uzaaft:uzaaft/rkquyvvzlrul
Jul 28, 2026
Merged

mitchellh merged 1 commit into
ghostty-org:mainfrom
Uzaaft:uzaaft/rkquyvvzlrul

Conversation

@Uzaaft

@Uzaaft Uzaaft commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Seems to me the grow path in Buffer.sync (and syncFromArrayLists) multiplies by @sizeOf(T) twice: req_bytes is already a byte count, it gets doubled into size, and then the newBufferWithLength: call does size * @sizeOf(T) on top of that. So every reallocation ends up being data.len × @sizeOf(T)^2 × 2 bytes instead of the intended data.len × @sizeOf(T) × 2.

The OpenGL version of this same helper does it what seems to be the intended way, so this looks like a mixup rather than a deliberate safety margin.

This makes the Metal implementation match the OpenGL one: track the new length in units of T (which also fixes self.len going stale after a grow. It's documented as the allocated element count but was never updated here)

Buffer.sync and Buffer.syncFromArrayLists computed the new buffer size
in bytes (req_bytes * 2) and then multiplied by @sizeof(T) again when
passing it to newBufferWithLength:, allocating data.len * sizeOf(T)^2 * 2
bytes. For the 32-byte CellText buffers this is a 64x over-allocation
and for the 4-byte CellBg buffers 8x, per swap-chain frame (e.g. ~9.4MB
instead of ~300KB per frame for a full 120x40 screen of text).

Match the OpenGL buffer implementation: track the new length in units
of T and multiply by @sizeof(T) exactly once.
@Uzaaft
Uzaaft requested a review from a team as a code owner July 27, 2026 21:45
@mitchellh
mitchellh merged commit a60cd15 into ghostty-org:main Jul 28, 2026
98 checks passed
@github-actions github-actions Bot added this to the 1.4.0 milestone Jul 28, 2026
@Uzaaft
Uzaaft deleted the uzaaft/rkquyvvzlrul branch July 30, 2026 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants