Skip to content

Commit 648110b

Browse files
perf(mobile): bound terminal replay compaction costs
1 parent d7daba1 commit 648110b

4 files changed

Lines changed: 24 additions & 16 deletions

File tree

‎apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalView.kt‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,13 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex
405405
fun writeOutput(data: String, reset: Boolean) {
406406
if (isCleanedUp) return
407407
val output = streamedOutput ?: TerminalOutputBuffer()
408-
output.write(data, reset)
408+
val bytes = data.toByteArray(Charsets.UTF_8)
409+
output.write(bytes, reset)
409410
streamedOutput = output
410411
if (reset) {
411412
recreateTerminal()
412413
} else if (terminalHandle != 0L) {
413-
feedOutput(data.toByteArray(Charsets.UTF_8))
414+
feedOutput(bytes)
414415
renderSnapshot()
415416
}
416417
}

‎apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/TerminalOutputBuffer.kt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ internal class TerminalOutputBuffer {
2727
return result
2828
}
2929

30-
fun write(text: String, reset: Boolean) {
31-
val incoming = text.toByteArray(Charsets.UTF_8)
30+
fun write(incoming: ByteArray, reset: Boolean) {
3231
if (reset || incoming.size >= REPLAY_LIMIT) clear()
3332
var start = maxOf(0, incoming.size - REPLAY_LIMIT)
3433
while (start < incoming.size && incoming[start].toInt() and 0xC0 == 0x80) start += 1

‎apps/mobile/modules/t3-terminal/ios/T3TerminalView.swift‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,14 +596,15 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
596596

597597
func writeOutput(_ data: String, reset: Bool) {
598598
let output = streamedOutput ?? TerminalOutputBuffer()
599-
output.write(data, reset: reset)
599+
let bytes = Data(data.utf8)
600+
output.write(bytes, reset: reset)
600601
streamedOutput = output
601602
if reset {
602603
refreshSurface()
603604
} else if surface == nil {
604605
createSurfaceIfPossible()
605606
} else {
606-
feedData(Data(data.utf8))
607+
feedData(bytes)
607608
}
608609
}
609610

‎apps/mobile/modules/t3-terminal/ios/TerminalOutputBuffer.swift‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,27 @@ import Foundation
22

33
// Keep only replay history for intentional surface recreation (font/theme changes).
44
final class TerminalOutputBuffer {
5-
private(set) var data = Data()
5+
private var storage = Data()
6+
private var retainedStart = 0
67
private let limit = 512 * 1024
78

8-
func write(_ text: String, reset: Bool) {
9-
if reset {
10-
data = Data(text.utf8)
9+
var data: Data { storage.subdata(in: retainedStart..<storage.count) }
10+
11+
func write(_ incoming: Data, reset: Bool) {
12+
if reset || incoming.count >= limit {
13+
storage = incoming.subdata(in: max(0, incoming.count - limit)..<incoming.count)
14+
retainedStart = 0
1115
} else {
12-
data.append(contentsOf: text.utf8)
16+
storage.append(incoming)
17+
retainedStart = max(retainedStart, storage.count - limit)
18+
}
19+
while retainedStart < storage.count && storage[retainedStart] & 0xC0 == 0x80 {
20+
retainedStart += 1
1321
}
14-
guard data.count > limit else { return }
15-
var start = data.count - limit
16-
while start < data.count && data[data.startIndex + start] & 0xC0 == 0x80 {
17-
start += 1
22+
// Compact once per history window, rather than copying 512 KiB on every append.
23+
if retainedStart >= limit {
24+
storage = storage.subdata(in: retainedStart..<storage.count)
25+
retainedStart = 0
1826
}
19-
data = Data(data.dropFirst(start))
2027
}
2128
}

0 commit comments

Comments
 (0)