Skip to content

Commit d1049c4

Browse files
hieu-leeclaude
andauthored
perf(web): terminal snapshots reuse WASM memory views (#26717)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 28bfae0 commit d1049c4

2 files changed

Lines changed: 55 additions & 37 deletions

File tree

‎apps/web/src/terminal/ghostty/core.ts‎

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,19 @@ function sameColor(left: GhosttyColor, right: GhosttyColor): boolean {
177177
}
178178

179179
/** 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));
182186
const CHUNK_SIZE = 4_096;
183187
let text = "";
184188
for (let start = 0; start < graphemeLength; start += CHUNK_SIZE) {
185189
const count = Math.min(CHUNK_SIZE, graphemeLength - start);
186190
const codes = new Array<number>(count);
187191
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);
189193
}
190194
text += String.fromCodePoint(...codes);
191195
}
@@ -853,12 +857,12 @@ export class GhosttyTerminalCore {
853857
if (rowDirty) {
854858
this.rows[rowIndex] = this.readRow(iterator, cols, foreground, background);
855859
dirtyRows.add(rowIndex);
856-
this.runtime.bytes(this.scratch, 1)[0] = 0;
860+
this.runtime.memoryBytes()[this.scratch] = 0;
857861
this.runtime.call("ghostty_render_state_row_set", iterator, 0, this.scratch);
858862
}
859863
rowIndex += 1;
860864
}
861-
this.runtime.view(this.scratch, 4).setUint32(0, 0, true);
865+
this.runtime.memoryDataView().setUint32(this.scratch, 0, true);
862866
this.runtime.call("ghostty_render_state_set", this.renderState, 0, this.scratch);
863867
}
864868

@@ -1009,19 +1013,19 @@ export class GhosttyTerminalCore {
10091013
"ghostty_render_state_row_get(raw)",
10101014
this.runtime.call("ghostty_render_state_row_get", iterator, ROW_DATA.raw, this.scratch),
10111015
);
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;
10141018
this.assertSuccess(
10151019
"ghostty_row_get(wrap continuation)",
10161020
this.runtime.call("ghostty_row_get", rawRow, 2, this.scratch + 8),
10171021
);
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;
10201024
this.assertSuccess(
10211025
"ghostty_row_get(wrap)",
10221026
this.runtime.call("ghostty_row_get", rawRow, 1, this.scratch + 8),
10231027
);
1024-
const wrapsToNext = this.runtime.bytes(this.scratch + 8, 1)[0] !== 0;
1028+
const wrapsToNext = this.runtime.memoryBytes()[this.scratch + 8] !== 0;
10251029

10261030
this.assertSuccess(
10271031
"ghostty_render_state_row_get(cells)",
@@ -1034,15 +1038,13 @@ export class GhosttyTerminalCore {
10341038
);
10351039
const cellsIterator = this.runtime.readPointer(this.rowCellsSlot);
10361040
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");
10371043
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);
10431046
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(
10461048
cellsIterator,
10471049
5,
10481050
this.cellQuery + CELL_QUERY.keys,
@@ -1081,8 +1083,7 @@ export class GhosttyTerminalCore {
10811083
) === GHOSTTY_SUCCESS
10821084
) {
10831085
// 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);
10861087
}
10871088
}
10881089
let wide = 0;
@@ -1097,13 +1098,13 @@ export class GhosttyTerminalCore {
10971098
this.scratch,
10981099
),
10991100
);
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);
11021103
this.assertSuccess(
11031104
"ghostty_cell_get(wide)",
11041105
this.runtime.call("ghostty_cell_get", rawCell, RAW_CELL_DATA.wide, this.scratch + 8),
11051106
);
1106-
wide = this.runtime.view(this.scratch + 8, 4).getUint32(0, true);
1107+
wide = this.runtime.memoryDataView().getUint32(this.scratch + 8, true);
11071108
}
11081109
// Read the style after allocation and ABI calls, which can grow WASM memory.
11091110
const styleView = this.runtime.memoryDataView();
@@ -1228,34 +1229,34 @@ export class GhosttyTerminalCore {
12281229
}
12291230

12301231
private getU16(data: number): number {
1231-
this.runtime.bytes(this.scratch, 2).fill(0);
1232+
this.runtime.memoryDataView().setUint16(this.scratch, 0, true);
12321233
this.assertSuccess(
12331234
"ghostty_render_state_get",
12341235
this.runtime.call("ghostty_render_state_get", this.renderState, data, this.scratch),
12351236
);
1236-
return this.runtime.view(this.scratch, 2).getUint16(0, true);
1237+
return this.runtime.memoryDataView().getUint16(this.scratch, true);
12371238
}
12381239

12391240
private getU32(data: number): number {
1240-
this.runtime.bytes(this.scratch, 4).fill(0);
1241+
this.runtime.memoryDataView().setUint32(this.scratch, 0, true);
12411242
this.assertSuccess(
12421243
"ghostty_render_state_get",
12431244
this.runtime.call("ghostty_render_state_get", this.renderState, data, this.scratch),
12441245
);
1245-
return this.runtime.view(this.scratch, 4).getUint32(0, true);
1246+
return this.runtime.memoryDataView().getUint32(this.scratch, true);
12461247
}
12471248

12481249
private getBool(data: number): boolean {
1249-
this.runtime.bytes(this.scratch, 1)[0] = 0;
1250+
this.runtime.memoryBytes()[this.scratch] = 0;
12501251
this.assertSuccess(
12511252
"ghostty_render_state_get",
12521253
this.runtime.call("ghostty_render_state_get", this.renderState, data, this.scratch),
12531254
);
1254-
return this.runtime.bytes(this.scratch, 1)[0] !== 0;
1255+
return this.runtime.memoryBytes()[this.scratch] !== 0;
12551256
}
12561257

12571258
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);
12591260
const result = this.runtime.call(
12601261
"ghostty_render_state_get",
12611262
this.renderState,
@@ -1266,15 +1267,15 @@ export class GhosttyTerminalCore {
12661267
}
12671268

12681269
private getRowBool(iterator: number, data: number): boolean {
1269-
this.runtime.bytes(this.scratch, 1)[0] = 0;
1270+
this.runtime.memoryBytes()[this.scratch] = 0;
12701271
return (
12711272
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
12731274
);
12741275
}
12751276

12761277
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);
12781279
const result = this.runtime.call(
12791280
"ghostty_render_state_row_cells_get",
12801281
iterator,

‎apps/web/src/terminal/ghostty/runtime.ts‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export class GhosttyRuntime {
2424
readonly layouts: TypeLayouts;
2525
private readonly exports: WebAssembly.Exports;
2626
private memoryView: DataView;
27+
private memoryU8: Uint8Array;
2728
private readonly ptyWriters = new Map<number, (data: string) => void>();
2829
private nextPtyWriterId = 1;
2930
private writePtyFunctionIndex = 0;
@@ -36,6 +37,7 @@ export class GhosttyRuntime {
3637
}
3738
this.memory = memory;
3839
this.memoryView = new DataView(memory.buffer);
40+
this.memoryU8 = new Uint8Array(memory.buffer);
3941
const jsonPointer = this.call("ghostty_type_json");
4042
const bytes = new Uint8Array(memory.buffer);
4143
let end = jsonPointer;
@@ -68,11 +70,15 @@ export class GhosttyRuntime {
6870
}
6971

7072
call(name: string, ...args: Array<number | bigint>): number {
73+
return this.fn(name)(...args);
74+
}
75+
76+
fn(name: string): WasmFunction {
7177
const fn = this.exports[name];
7278
if (typeof fn !== "function") {
7379
throw new Error(`libghostty-vt export is unavailable: ${name}`);
7480
}
75-
return (fn as WasmFunction)(...args);
81+
return fn as WasmFunction;
7682
}
7783

7884
layout(name: string): TypeLayout {
@@ -126,18 +132,29 @@ export class GhosttyRuntime {
126132
}
127133

128134
view(pointer: number, size?: number): DataView {
129-
return new DataView(this.memory.buffer, pointer, size);
135+
return new DataView(this.memoryDataView().buffer, pointer, size);
130136
}
131137

132138
bytes(pointer: number, size: number): Uint8Array {
133-
return new Uint8Array(this.memory.buffer, pointer, size);
139+
return new Uint8Array(this.memoryDataView().buffer, pointer, size);
134140
}
135141

136142
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) {
138155
this.memoryView = new DataView(this.memory.buffer);
156+
this.memoryU8 = new Uint8Array(this.memory.buffer);
139157
}
140-
return this.memoryView;
141158
}
142159

143160
setField(pointer: number, structName: string, fieldName: string, value: number): void {

0 commit comments

Comments
 (0)