Commit 76e3cf98ba
Verified · cmc
Layout: unified · split
Sources/OrgApp/AgendaModel.swift +26 −30
| @@ -177,39 +177,35 @@ public final class AgendaModel { | |||
| 177 | defer { interval.end("\(files.count) files") } | 177 | defer { interval.end("\(files.count) files") } |
| 178 | let defaults = workspace.settings.org | 178 | let defaults = workspace.settings.org |
| 179 | let blocking = options.dimBlockedTasks != .off && (options.enforceTodoDependencies || options.enforceTodoCheckboxDependencies) | 179 | let blocking = options.dimBlockedTasks != .off && (options.enforceTodoDependencies || options.enforceTodoCheckboxDependencies) |
| 180 | var reads: [(path: String, cached: AgendaSource?, mtime: Date, size: Int, text: String?)] = [] | 180 | let files = files |
| 181 | for path in files { | 181 | let (openCache, diskCache) = (cache.open, cache.entries) |
| 182 | if let text = open[path] { | ||
| 183 | // The same text parsed before, as the buffer or as the file on disk. | ||
| 184 | func reusable(_ source: AgendaSource?, _ parsedWith: OrgSettings?, _ parsedBlocking: Bool?) -> AgendaSource? { | ||
| 185 | guard let source, parsedWith == defaults, parsedBlocking == blocking, !source.readsSetupFiles, source.text == text else { return nil } | ||
| 186 | return source | ||
| 187 | } | ||
| 188 | let fromOpen = cache.open[path] | ||
| 189 | let fromDisk = cache.entries[path] | ||
| 190 | let cached = reusable(fromOpen?.source, fromOpen?.defaults, fromOpen?.blocking) ?? reusable(fromDisk?.source, fromDisk?.defaults, fromDisk?.blocking) | ||
| 191 | reads.append((path, cached, .distantPast, -1, text)) | ||
| 192 | continue | ||
| 193 | } | ||
| 194 | let attributes = try? FileManager.default.attributesOfItem(atPath: path) | ||
| 195 | let mtime = attributes?[.modificationDate] as? Date ?? .distantPast | ||
| 196 | let size = (attributes?[.size] as? NSNumber)?.intValue ?? -1 | ||
| 197 | // A file reading setup files is read again: they may have changed. | ||
| 198 | let cached = cache.entries[path].flatMap { entry in | ||
| 199 | entry.mtime == mtime && entry.size == size && entry.defaults == defaults && entry.blocking == blocking && !entry.source.readsSetupFiles ? entry.source : nil | ||
| 200 | } | ||
| 201 | reads.append((path, cached, mtime, size, nil)) | ||
| 202 | } | ||
| 203 | let loaded = await Task.detached { | 182 | let loaded = await Task.detached { |
| 204 | var sources: [(path: String, mtime: Date, size: Int, source: AgendaSource, fromDisk: Bool)] = [] | 183 | var sources: [(path: String, mtime: Date, size: Int, source: AgendaSource, fromDisk: Bool)] = [] |
| 205 | for read in reads { | 184 | for path in files { |
| 206 | if let cached = read.cached { | 185 | if let text = open[path] { |
| 207 | sources.append((read.path, read.mtime, read.size, cached, read.text == nil)) | 186 | // The same text parsed before, as the buffer or as the file on disk. |
| 208 | } else if let text = read.text { | 187 | func reusable(_ source: AgendaSource?, _ parsedWith: OrgSettings?, _ parsedBlocking: Bool?) -> AgendaSource? { |
| 209 | sources.append((read.path, read.mtime, read.size, AgendaSource(path: read.path, text: text, defaults: defaults.resolvingSetupFiles(in: text, path: read.path, read: OrgSettings.readFile), checksBlocking: blocking), false)) | 188 | guard let source, parsedWith == defaults, parsedBlocking == blocking, !source.readsSetupFiles, source.text == text else { return nil } |
| 210 | } else if let data = FileManager.default.contents(atPath: read.path) { | 189 | return source |
| 190 | } | ||
| 191 | let fromOpen = openCache[path] | ||
| 192 | let fromDisk = diskCache[path] | ||
| 193 | let cached = reusable(fromOpen?.source, fromOpen?.defaults, fromOpen?.blocking) ?? reusable(fromDisk?.source, fromDisk?.defaults, fromDisk?.blocking) | ||
| 194 | sources.append((path, .distantPast, -1, cached ?? AgendaSource(path: path, text: text, defaults: defaults.resolvingSetupFiles(in: text, path: path, read: OrgSettings.readFile), checksBlocking: blocking), false)) | ||
| 195 | continue | ||
| 196 | } | ||
| 197 | let attributes = try? FileManager.default.attributesOfItem(atPath: path) | ||
| 198 | let mtime = attributes?[.modificationDate] as? Date ?? .distantPast | ||
| 199 | let size = (attributes?[.size] as? NSNumber)?.intValue ?? -1 | ||
| 200 | // A file reading setup files is read again: they may have changed. | ||
| 201 | let cached = diskCache[path].flatMap { entry in | ||
| 202 | entry.mtime == mtime && entry.size == size && entry.defaults == defaults && entry.blocking == blocking && !entry.source.readsSetupFiles ? entry.source : nil | ||
| 203 | } | ||
| 204 | if let cached { | ||
| 205 | sources.append((path, mtime, size, cached, true)) | ||
| 206 | } else if let data = FileManager.default.contents(atPath: path) { | ||
| 211 | let text = SourceText(bytes: [UInt8](data)).text | 207 | let text = SourceText(bytes: [UInt8](data)).text |
| 212 | sources.append((read.path, read.mtime, read.size, AgendaSource(path: read.path, text: text, defaults: defaults.resolvingSetupFiles(in: text, path: read.path, read: OrgSettings.readFile), checksBlocking: blocking), true)) | 208 | sources.append((path, mtime, size, AgendaSource(path: path, text: text, defaults: defaults.resolvingSetupFiles(in: text, path: path, read: OrgSettings.readFile), checksBlocking: blocking), true)) |
| 213 | } | 209 | } |
| 214 | } | 210 | } |
| 215 | return sources | 211 | return sources |
Tests/OrgAppTests/AppTests.swift +28
| @@ -595,6 +595,34 @@ struct AgendaModelTests { | |||
| 595 | #expect(agenda.list.map(\.text) == ["TODO One"]) | 595 | #expect(agenda.list.map(\.text) == ["TODO One"]) |
| 596 | } | 596 | } |
| 597 | 597 | ||
| 598 | /// A refresh over cached files leaves the main actor free while it checks them on disk. | ||
| 599 | @Test func checksFilesOffTheMainActor() async throws { | ||
| 600 | let notes = try Folder() | ||
| 601 | for file in 0..<1_500 { try notes.write("f\(file).org", "* TODO Item\n") } | ||
| 602 | let model = try WorkspaceModel(directory: nil) | ||
| 603 | try await model.addRoot(notes.url) | ||
| 604 | let agenda = AgendaModel(workspace: model) | ||
| 605 | agenda.mode = .todo(keywords: nil) | ||
| 606 | await agenda.refresh(open: [:]) | ||
| 607 | let clock = ContinuousClock() | ||
| 608 | var longest = Duration.zero | ||
| 609 | let beat = Task { @MainActor in | ||
| 610 | var last = clock.now | ||
| 611 | while !Task.isCancelled { | ||
| 612 | await Task.yield() | ||
| 613 | longest = max(longest, clock.now - last) | ||
| 614 | last = clock.now | ||
| 615 | } | ||
| 616 | } | ||
| 617 | await Task.yield() | ||
| 618 | let start = clock.now | ||
| 619 | await agenda.refresh(open: [:]) | ||
| 620 | let took = clock.now - start | ||
| 621 | beat.cancel() | ||
| 622 | #expect(agenda.list.count == 1_500) | ||
| 623 | #expect(longest < took / 2, "main actor held for \(longest) of \(took)") | ||
| 624 | } | ||
| 625 | |||
| 598 | @Test func entriesKnowTheirDoneKeyword() async throws { | 626 | @Test func entriesKnowTheirDoneKeyword() async throws { |
| 599 | let notes = try Folder() | 627 | let notes = try Folder() |
| 600 | try notes.write("setup.org", "#+TODO: NEXT | FINISHED\n") | 628 | try notes.write("setup.org", "#+TODO: NEXT | FINISHED\n") |