The best way to control Claude Code, Codex and more from any computer or phone
perf(web): terminal snapshots reuse WASM memory views (#26717)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Hieu Le committed
Nov 1, 2026 at 03:35 UTC
d1049c4435ac0d763dafbc09c20d089adacf4c10
2 files changed
+55
-37
apps/web/src/terminal/ghostty/core.ts
+33
-32
@@ -177,15 +177,19 @@ function sameColor(left: GhosttyColor, right: GhosttyColor): boolean {
177
}
178
179
/** Bounds String.fromCodePoint arguments for cells with enormous combining-mark sequences. */
180
-export function ghosttyCellText(codepointView: DataView, graphemeLength: number): string {
181
- if (graphemeLength === 1) return String.fromCodePoint(codepointView.getUint32(0, true));
180
+export function ghosttyCellText(
181
+ codepointView: DataView,
182
+ graphemeLength: number,
183
+ byteOffset = 0,
184
+): string {
185
+ if (graphemeLength === 1) return String.fromCodePoint(codepointView.getUint32(byteOffset, true));
186
const CHUNK_SIZE = 4_096;
187
let text = "";
188
for (let start = 0; start < graphemeLength; start += CHUNK_SIZE) {
189
const count = Math.min(CHUNK_SIZE, graphemeLength - start);
190
const codes = new Array<number>(count);
191
for (let index = 0; index < count; index += 1) {
188
- codes[index] = codepointView.getUint32((start + index) * 4, true);
192
+ codes[index] = codepointView.getUint32(byteOffset + (start + index) * 4, true);
193
}
194
text += String.fromCodePoint(...codes);
195
}
@@ -853,12 +857,12 @@ export class GhosttyTerminalCore {
857
if (rowDirty) {
858
this.rows[rowIndex] = this.readRow(iterator, cols, foreground, background);
859
dirtyRows.add(rowIndex);
856
- this.runtime.bytes(this.scratch, 1)[0] = 0;
860
+ this.runtime.memoryBytes()[this.scratch] = 0;
861
this.runtime.call("ghostty_render_state_row_set", iterator, 0, this.scratch);
862
}
863
rowIndex += 1;
864
}
861
- this.runtime.view(this.scratch, 4).setUint32(0, 0, true);
865
+ this.runtime.memoryDataView().setUint32(this.scratch, 0, true);
866
this.runtime.call("ghostty_render_state_set", this.renderState, 0, this.scratch);
867
}
868
@@ -1009,19 +1013,19 @@ export class GhosttyTerminalCore {
1013
"ghostty_render_state_row_get(raw)",
1014
this.runtime.call("ghostty_render_state_row_get", iterator, ROW_DATA.raw, this.scratch),
1015
);
1012
- const rawRow = this.runtime.view(this.scratch, 8).getBigUint64(0, true);
1013
- this.runtime.bytes(this.scratch + 8, 1)[0] = 0;
1016
+ const rawRow = this.runtime.memoryDataView().getBigUint64(this.scratch, true);
1017
+ this.runtime.memoryBytes()[this.scratch + 8] = 0;
1018
this.assertSuccess(
1019
"ghostty_row_get(wrap continuation)",
1020
this.runtime.call("ghostty_row_get", rawRow, 2, this.scratch + 8),
1021
);
1018
- const isWrapContinuation = this.runtime.bytes(this.scratch + 8, 1)[0] !== 0;
1019
- this.runtime.bytes(this.scratch + 8, 1)[0] = 0;
1022
+ const isWrapContinuation = this.runtime.memoryBytes()[this.scratch + 8] !== 0;
1023
+ this.runtime.memoryBytes()[this.scratch + 8] = 0;
1024
this.assertSuccess(
1025
"ghostty_row_get(wrap)",
1026
this.runtime.call("ghostty_row_get", rawRow, 1, this.scratch + 8),
1027
);
1024
- const wrapsToNext = this.runtime.bytes(this.scratch + 8, 1)[0] !== 0;
1028
+ const wrapsToNext = this.runtime.memoryBytes()[this.scratch + 8] !== 0;
1029
1030
this.assertSuccess(
1031
"ghostty_render_state_row_get(cells)",
@@ -1034,15 +1038,13 @@ export class GhosttyTerminalCore {
1038
);
1039
const cellsIterator = this.runtime.readPointer(this.rowCellsSlot);
1040
const { size: styleSize, fields: styleFields } = this.runtime.layout("GhosttyStyle");
1041
+ const cellsNext = this.runtime.fn("ghostty_render_state_row_cells_next");
1042
+ const cellsGetMulti = this.runtime.fn("ghostty_render_state_row_cells_get_multi");
1043
const cells: GhosttyCell[] = [];
1038
- while (
1039
- cells.length < cols &&
1040
- this.runtime.call("ghostty_render_state_row_cells_next", cellsIterator) !== 0
1041
- ) {
1042
- this.runtime.bytes(this.style, styleSize).fill(0);
1044
+ while (cells.length < cols && cellsNext(cellsIterator) !== 0) {
1045
+ this.runtime.memoryBytes().fill(0, this.style, this.style + styleSize);
1046
this.runtime.setField(this.style, "GhosttyStyle", "size", styleSize);
1044
- const result = this.runtime.call(
1045
- "ghostty_render_state_row_cells_get_multi",
1047
+ const result = cellsGetMulti(
1048
cellsIterator,
1049
5,
1050
this.cellQuery + CELL_QUERY.keys,
@@ -1081,8 +1083,7 @@ export class GhosttyTerminalCore {
1083
) === GHOSTTY_SUCCESS
1084
) {
1085
// Byte allocations need not be 4-byte aligned; use DataView instead of Uint32Array.
1084
- const codepointView = this.runtime.view(this.graphemes, bufferSize);
1085
- text = ghosttyCellText(codepointView, graphemeLength);
1086
+ text = ghosttyCellText(this.runtime.memoryDataView(), graphemeLength, this.graphemes);
1087
}
1088
}
1089
let wide = 0;
@@ -1097,13 +1098,13 @@ export class GhosttyTerminalCore {
1098
this.scratch,
1099
),
1100
);
1100
- const rawCell = this.runtime.view(this.scratch, 8).getBigUint64(0, true);
1101
- this.runtime.view(this.scratch + 8, 4).setUint32(0, 0, true);
1101
+ const rawCell = this.runtime.memoryDataView().getBigUint64(this.scratch, true);
1102
+ this.runtime.memoryDataView().setUint32(this.scratch + 8, 0, true);
1103
this.assertSuccess(
1104
"ghostty_cell_get(wide)",
1105
this.runtime.call("ghostty_cell_get", rawCell, RAW_CELL_DATA.wide, this.scratch + 8),
1106
);
1106
- wide = this.runtime.view(this.scratch + 8, 4).getUint32(0, true);
1107
+ wide = this.runtime.memoryDataView().getUint32(this.scratch + 8, true);
1108
}
1109
// Read the style after allocation and ABI calls, which can grow WASM memory.
1110
const styleView = this.runtime.memoryDataView();
@@ -1228,34 +1229,34 @@ export class GhosttyTerminalCore {
1229
}
1230
1231
private getU16(data: number): number {
1231
- this.runtime.bytes(this.scratch, 2).fill(0);
1232
+ this.runtime.memoryDataView().setUint16(this.scratch, 0, true);
1233
this.assertSuccess(
1234
"ghostty_render_state_get",
1235
this.runtime.call("ghostty_render_state_get", this.renderState, data, this.scratch),
1236
);
1236
- return this.runtime.view(this.scratch, 2).getUint16(0, true);
1237
+ return this.runtime.memoryDataView().getUint16(this.scratch, true);
1238
}
1239
1240
private getU32(data: number): number {
1240
- this.runtime.bytes(this.scratch, 4).fill(0);
1241
+ this.runtime.memoryDataView().setUint32(this.scratch, 0, true);
1242
this.assertSuccess(
1243
"ghostty_render_state_get",
1244
this.runtime.call("ghostty_render_state_get", this.renderState, data, this.scratch),
1245
);
1245
- return this.runtime.view(this.scratch, 4).getUint32(0, true);
1246
+ return this.runtime.memoryDataView().getUint32(this.scratch, true);
1247
}
1248
1249
private getBool(data: number): boolean {
1249
- this.runtime.bytes(this.scratch, 1)[0] = 0;
1250
+ this.runtime.memoryBytes()[this.scratch] = 0;
1251
this.assertSuccess(
1252
"ghostty_render_state_get",
1253
this.runtime.call("ghostty_render_state_get", this.renderState, data, this.scratch),
1254
);
1254
- return this.runtime.bytes(this.scratch, 1)[0] !== 0;
1255
+ return this.runtime.memoryBytes()[this.scratch] !== 0;
1256
}
1257
1258
private getColor(data: number, fallback: GhosttyColor): GhosttyColor {
1258
- this.runtime.bytes(this.scratch, 3).fill(0);
1259
+ this.runtime.memoryBytes().fill(0, this.scratch, this.scratch + 3);
1260
const result = this.runtime.call(
1261
"ghostty_render_state_get",
1262
this.renderState,
@@ -1266,15 +1267,15 @@ export class GhosttyTerminalCore {
1267
}
1268
1269
private getRowBool(iterator: number, data: number): boolean {
1269
- this.runtime.bytes(this.scratch, 1)[0] = 0;
1270
+ this.runtime.memoryBytes()[this.scratch] = 0;
1271
return (
1272
this.runtime.call("ghostty_render_state_row_get", iterator, data, this.scratch) ===
1272
- GHOSTTY_SUCCESS && this.runtime.bytes(this.scratch, 1)[0] !== 0
1273
+ GHOSTTY_SUCCESS && this.runtime.memoryBytes()[this.scratch] !== 0
1274
);
1275
}
1276
1277
private getCellColor(iterator: number, data: number, fallback: GhosttyColor): GhosttyColor {
1277
- this.runtime.bytes(this.scratch, 3).fill(0);
1278
+ this.runtime.memoryBytes().fill(0, this.scratch, this.scratch + 3);
1279
const result = this.runtime.call(
1280
"ghostty_render_state_row_cells_get",
1281
iterator,
apps/web/src/terminal/ghostty/runtime.ts
+22
-5
@@ -24,6 +24,7 @@ export class GhosttyRuntime {
24
readonly layouts: TypeLayouts;
25
private readonly exports: WebAssembly.Exports;
26
private memoryView: DataView;
27
+ private memoryU8: Uint8Array;
28
private readonly ptyWriters = new Map<number, (data: string) => void>();
29
private nextPtyWriterId = 1;
30
private writePtyFunctionIndex = 0;
@@ -36,6 +37,7 @@ export class GhosttyRuntime {
37
}
38
this.memory = memory;
39
this.memoryView = new DataView(memory.buffer);
40
+ this.memoryU8 = new Uint8Array(memory.buffer);
41
const jsonPointer = this.call("ghostty_type_json");
42
const bytes = new Uint8Array(memory.buffer);
43
let end = jsonPointer;
@@ -68,11 +70,15 @@ export class GhosttyRuntime {
70
}
71
72
call(name: string, ...args: Array<number | bigint>): number {
73
+ return this.fn(name)(...args);
74
+ }
75
+
76
+ fn(name: string): WasmFunction {
77
const fn = this.exports[name];
78
if (typeof fn !== "function") {
79
throw new Error(`libghostty-vt export is unavailable: ${name}`);
80
}
75
- return (fn as WasmFunction)(...args);
81
+ return fn as WasmFunction;
82
}
83
84
layout(name: string): TypeLayout {
@@ -126,18 +132,29 @@ export class GhosttyRuntime {
132
}
133
134
view(pointer: number, size?: number): DataView {
129
- return new DataView(this.memory.buffer, pointer, size);
135
+ return new DataView(this.memoryDataView().buffer, pointer, size);
136
}
137
138
bytes(pointer: number, size: number): Uint8Array {
133
- return new Uint8Array(this.memory.buffer, pointer, size);
139
+ return new Uint8Array(this.memoryDataView().buffer, pointer, size);
140
}
141
142
memoryDataView(): DataView {
137
- if (this.memoryView.buffer !== this.memory.buffer) {
143
+ this.refreshMemoryViews();
144
+ return this.memoryView;
145
+ }
146
+
147
+ memoryBytes(): Uint8Array {
148
+ this.refreshMemoryViews();
149
+ return this.memoryU8;
150
+ }
151
+
152
+ private refreshMemoryViews(): void {
153
+ // Growing memory detaches the old buffer. Reading `memory.buffer` costs far more than this check.
154
+ if (this.memoryU8.byteLength === 0) {
155
this.memoryView = new DataView(this.memory.buffer);
156
+ this.memoryU8 = new Uint8Array(this.memory.buffer);
157
}
140
- return this.memoryView;
158
}
159
160
setField(pointer: number, structName: string, fieldName: string, value: number): void {