Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

Launcher: read frame time recordings off the main thread

v0lumehi0a0125b51b9a
diff --git a/frontend/Sources/NSPortBuilder/FrameTimesView.swift b/frontend/Sources/NSPortBuilder/FrameTimesView.swift
index a89dd29c..b52e41e3 100644
--- a/frontend/Sources/NSPortBuilder/FrameTimesView.swift
+++ b/frontend/Sources/NSPortBuilder/FrameTimesView.swift
@@ -2,10 +2,12 @@ import Charts
import SwiftUI
/// One recording (FrameTimes.swift): its numbers in a line and its frame times over the session, the hitches marked.
+/// A recording can be several MB, so it is read and counted off the main thread (FrameTimesChart): the launch options
+/// sheet opens at once, and the chart fills in when the numbers are ready.
struct FrameTimesRecordingView: View {
let file: URL
let t: (String) -> String
- @State private var frames: [FrameTimes.Frame] = []
+ @State private var chart: FrameTimesChart?
var body: some View {
VStack(alignment: .leading, spacing: 8) {
@@ -13,32 +15,60 @@ struct FrameTimesRecordingView: View {
Text(file.deletingPathExtension().lastPathComponent).font(.manrope(13, .medium))
Spacer()
}
- if let s = FrameTimes.Summary(frames) {
- Text(String(format: t("launch.frameTimesSummary"), s.frames, s.fps, s.frameP50, s.frameP99, s.frameMax, s.hitches, s.dropped))
- .font(.manrope(12)).foregroundStyle(.secondary).monospacedDigit()
- Chart {
- ForEach(thinned, id: \.number) { f in
- LineMark(x: .value("s", f.time), y: .value("ms", f.frameMs)).interpolationMethod(.linear)
+ if let chart {
+ if let s = chart.summary {
+ Text(String(format: t("launch.frameTimesSummary"), s.frames, s.fps, s.frameP50, s.frameP99, s.frameMax, s.hitches, s.dropped))
+ .font(.manrope(12)).foregroundStyle(.secondary).monospacedDigit()
+ Chart {
+ ForEach(chart.line, id: \.number) { f in
+ LineMark(x: .value("s", f.time), y: .value("ms", f.frameMs)).interpolationMethod(.linear)
+ }
+ ForEach(chart.hitches, id: \.number) { f in
+ PointMark(x: .value("s", f.time), y: .value("ms", f.frameMs)).foregroundStyle(.red).symbolSize(14)
+ }
+ RuleMark(y: .value("ms", s.frameP50)).foregroundStyle(.secondary.opacity(0.5)).lineStyle(StrokeStyle(lineWidth: 1, dash: [3, 3]))
}
- ForEach(frames.filter { $0.frameMs > FrameTimes.Summary.hitchMs }, id: \.number) { f in
- PointMark(x: .value("s", f.time), y: .value("ms", f.frameMs)).foregroundStyle(.red).symbolSize(14)
- }
- RuleMark(y: .value("ms", s.frameP50)).foregroundStyle(.secondary.opacity(0.5)).lineStyle(StrokeStyle(lineWidth: 1, dash: [3, 3]))
+ .chartXScale(domain: chart.timeSpan)
+ .chartXAxisLabel("s").chartYAxisLabel("ms")
+ .frame(height: 120)
+ } else {
+ Text(t("launch.frameTimesEmpty")).font(.manrope(12)).foregroundStyle(.secondary)
}
- .chartXScale(domain: (frames.first?.time ?? 0)...max((frames.last?.time ?? 1), (frames.first?.time ?? 0) + 1))
- .chartXAxisLabel("s").chartYAxisLabel("ms")
- .frame(height: 120)
} else {
- Text(t("launch.frameTimesEmpty")).font(.manrope(12)).foregroundStyle(.secondary)
+ // While the file is read: the summary line's and the chart's room, so nothing moves when they arrive
+ Text(" ").font(.manrope(12))
+ ProgressView().controlSize(.small).frame(maxWidth: .infinity).frame(height: 120)
}
}
.padding(.horizontal, 18).padding(.vertical, 12)
- .task(id: file) { frames = FrameTimes.parse((try? String(contentsOf: file, encoding: .utf8)) ?? "") }
+ .task(id: file) {
+ chart = nil
+ let file = file
+ chart = await Task.detached(priority: .userInitiated) { FrameTimesChart(file: file) }.value
+ }
+ }
+}
+
+/// What the chart of one recording draws, worked out once from the file.
+struct FrameTimesChart {
+ let summary: FrameTimes.Summary?
+ let line: [FrameTimes.Frame]
+ let hitches: [FrameTimes.Frame]
+ let timeSpan: ClosedRange<Double>
+
+ init(file: URL) { self.init(frames: FrameTimes.parse((try? String(contentsOf: file, encoding: .utf8)) ?? "")) }
+
+ init(frames: [FrameTimes.Frame]) {
+ summary = FrameTimes.Summary(frames)
+ line = Self.thinned(frames)
+ hitches = frames.filter { $0.frameMs > FrameTimes.Summary.hitchMs }
+ let first = frames.first?.time ?? 0
+ timeSpan = first...max(frames.last?.time ?? 1, first + 1)
}
/// At most about 2,000 points on the line (a long session has hundreds of thousands of frames): every n-th frame,
/// each one the slowest of its stretch, so a hitch is never thinned away.
- private var thinned: [FrameTimes.Frame] {
+ static func thinned(_ frames: [FrameTimes.Frame]) -> [FrameTimes.Frame] {
let step = max(1, frames.count / 2000)
guard step > 1 else { return frames }
return stride(from: 0, to: frames.count, by: step).map { i in
diff --git a/frontend/Tests/NSPortBuilderTests/FrameTimesTests.swift b/frontend/Tests/NSPortBuilderTests/FrameTimesTests.swift
index e30a9b39..434111cc 100644
--- a/frontend/Tests/NSPortBuilderTests/FrameTimesTests.swift
+++ b/frontend/Tests/NSPortBuilderTests/FrameTimesTests.swift
@@ -46,6 +46,33 @@ final class FrameTimesTests: XCTestCase {
XCTAssertNil(FrameTimes.Summary([]))
}
+ func testTheChartIsWorkedOutOnceFromTheFrames() {
+ let chart = FrameTimesChart(frames: FrameTimes.parse(tsv))
+ XCTAssertEqual(chart.summary, FrameTimes.Summary(FrameTimes.parse(tsv)))
+ XCTAssertEqual(chart.line.map(\.number), [1, 2, 3, 4], "a short recording is drawn whole")
+ XCTAssertEqual(chart.hitches.map(\.number), [3])
+ XCTAssertEqual(chart.timeSpan, 0...1, "at least a second wide")
+ XCTAssertNil(FrameTimesChart(frames: []).summary)
+ }
+
+ func testALongRecordingIsThinnedWithoutLosingAHitch() {
+ let frames = (1...10_000).map { FrameTimes.Frame(number: $0, time: Double($0) / 60, frameMs: $0 == 4_321 ? 120 : 16.7) }
+ let chart = FrameTimesChart(frames: frames)
+ XCTAssertLessThanOrEqual(chart.line.count, 2_000)
+ XCTAssertTrue(chart.line.contains { $0.number == 4_321 }, "the slowest frame of its stretch stays on the line")
+ XCTAssertEqual(chart.hitches.map(\.number), [4_321])
+ }
+
+ func testReadingARecordingFileGivesTheSameChart() throws {
+ let dir = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
+ try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
+ defer { try? FileManager.default.removeItem(at: dir) }
+ let file = dir.appendingPathComponent("2026-10-31 00-30-34.tsv")
+ try tsv.write(to: file, atomically: true, encoding: .utf8)
+ XCTAssertEqual(FrameTimesChart(file: file).line, FrameTimesChart(frames: FrameTimes.parse(tsv)).line)
+ XCTAssertNil(FrameTimesChart(file: dir.appendingPathComponent("missing.tsv")).summary)
+ }
+
func testNearestRankPercentile() {
XCTAssertEqual(FrameTimes.percentile([5, 1, 3, 2, 4], 50), 3)
XCTAssertEqual(FrameTimes.percentile([5, 1, 3, 2, 4], 99), 5)