perf(menubar): 降低绘制驻留并复用原生进程表与缩略图

This commit is contained in:
2026-11-04 10:39:15 +08:00
parent 1c26fc18f3
commit 065c53622a
28 changed files with 964 additions and 160 deletions
+11 -5
View File
@@ -52,6 +52,7 @@ nonisolated struct NetworkMonitor {
guard getifaddrs(&head) == 0 else { return [:] }
defer { freeifaddrs(head) }
var result: [String: (index: UInt32, addresses: [String], active: Bool)] = [:]
var hostBuffer = [CChar](repeating: 0, count: Int(NI_MAXHOST))
var cursor = head
while let entry = cursor {
defer { cursor = entry.pointee.ifa_next }
@@ -60,15 +61,20 @@ nonisolated struct NetworkMonitor {
let active = flags & UInt32(IFF_UP | IFF_RUNNING) == UInt32(IFF_UP | IFF_RUNNING)
var item = result[name] ?? (if_nametoindex(entry.pointee.ifa_name), [], active)
if let address = entry.pointee.ifa_addr, [UInt8(AF_INET), UInt8(AF_INET6)].contains(address.pointee.sa_family) {
var host = [CChar](repeating: 0, count: Int(NI_MAXHOST))
if getnameinfo(address, socklen_t(address.pointee.sa_len), &host, socklen_t(host.count), nil, 0, NI_NUMERICHOST) == 0 {
item.addresses.append(String(cString: host))
if let host = numericAddress(address, buffer: &hostBuffer) {
item.addresses.append(host)
}
}
item.addresses.sort()
result[name] = item
}
return result
return result.mapValues { (index: $0.index, addresses: $0.addresses.sorted(), active: $0.active) }
}
static func numericAddress(_ address: UnsafePointer<sockaddr>, buffer: inout [CChar]) -> String? {
guard !buffer.isEmpty else { return nil }
let capacity = socklen_t(buffer.count)
guard getnameinfo(address, socklen_t(address.pointee.sa_len), &buffer, capacity, nil, 0, NI_NUMERICHOST) == 0 else { return nil }
return String(cString: buffer)
}
/// NET_RT_IFLIST2 exposes 64-bit counters, avoiding getifaddrs' 4 GB wrap.
+6 -1
View File
@@ -21,7 +21,7 @@ nonisolated enum SamplingDiagnostics {
try? FileHandle.standardOutput.write(contentsOf: data + Data([10]))
}
static func record(_ snapshot: MetricsSnapshot, event: String = "sample", hiddenHostAlive: Bool? = nil) {
static func record(_ snapshot: MetricsSnapshot, event: String = "sample", hiddenHostAlive: Bool? = nil, windowNumber: Int? = nil, hoveredPlots: Int? = nil, nativePlots: Int? = nil, pointerInsidePanel: Bool? = nil, plotBounds: [[Double]]? = nil) {
guard enabled else { return }
var payload: [String: Any] = [
"event": event, "timestamp": snapshot.date.timeIntervalSince1970,
@@ -31,6 +31,11 @@ nonisolated enum SamplingDiagnostics {
"load": snapshot.load
]
payload["hiddenHostAlive"] = hiddenHostAlive
payload["windowNumber"] = windowNumber
payload["hoveredPlots"] = hoveredPlots
payload["nativePlots"] = nativePlots
payload["pointerInsidePanel"] = pointerInsidePanel
payload["plotBounds"] = plotBounds
payload["cpu"] = snapshot.cpu?.usage
payload["cores"] = snapshot.cpu?.cores.count
payload["cpuTemperature"] = snapshot.cpuTemperature