Skip to content

Commit 76da4fd

Browse files
authored
perf: cache selectedTabIndex via UUID→Int map, add @ObservationIgnored to internal caches (#529)
1 parent 1be6c3f commit 76da4fd

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

TablePro/Models/Query/QueryTab.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -561,19 +561,32 @@ struct QueryTab: Identifiable, Equatable {
561561
/// Manager for query tabs
562562
@MainActor @Observable
563563
final class QueryTabManager {
564-
var tabs: [QueryTab] = []
564+
var tabs: [QueryTab] = [] {
565+
didSet { _tabIndexMapDirty = true }
566+
}
567+
565568
var selectedTabId: UUID?
566569

570+
@ObservationIgnored private var _tabIndexMap: [UUID: Int] = [:]
571+
@ObservationIgnored private var _tabIndexMapDirty = true
572+
573+
private func rebuildTabIndexMapIfNeeded() {
574+
guard _tabIndexMapDirty else { return }
575+
_tabIndexMap = Dictionary(uniqueKeysWithValues: tabs.enumerated().map { ($1.id, $0) })
576+
_tabIndexMapDirty = false
577+
}
578+
567579
var tabIds: [UUID] { tabs.map(\.id) }
568580

569581
var selectedTab: QueryTab? {
570-
guard let id = selectedTabId else { return tabs.first }
571-
return tabs.first { $0.id == id }
582+
if let index = selectedTabIndex { return tabs[index] }
583+
return selectedTabId == nil ? tabs.first : nil
572584
}
573585

574586
var selectedTabIndex: Int? {
575587
guard let id = selectedTabId else { return nil }
576-
return tabs.firstIndex { $0.id == id }
588+
rebuildTabIndexMapIfNeeded()
589+
return _tabIndexMap[id]
577590
}
578591

579592
init() {

TablePro/Views/Main/MainContentCoordinator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ final class MainContentCoordinator {
9797
var needsLazyLoad = false
9898

9999
/// Cache for async-sorted query tab rows (large datasets sorted on background thread)
100-
private(set) var querySortCache: [UUID: QuerySortCacheEntry] = [:]
100+
@ObservationIgnored private(set) var querySortCache: [UUID: QuerySortCacheEntry] = [:]
101101

102102
// MARK: - Internal State
103103

@@ -134,7 +134,7 @@ final class MainContentCoordinator {
134134

135135
/// True while a database switch is in progress. Guards against
136136
/// side-effect window creation during the switch cascade.
137-
var isSwitchingDatabase = false
137+
@ObservationIgnored var isSwitchingDatabase = false
138138

139139
/// True once the coordinator's view has appeared (onAppear fired).
140140
/// Coordinators that SwiftUI creates during body re-evaluation but never

0 commit comments

Comments
 (0)