Cotton code
46 lines · 1.6 KB · SwiftRaw
#if COTTON_DEV// DevReads.swift — DEVELOPER TOOL, compiled into Cotton Dev only.//// Every call this process made into the core, with when it started and how// long it took, so the timings overlay (`CottonUI/Developer/DevTimings.swift`)// can say which reads a surface made before its first row. Noted from the one// door every call goes through (`CottonCore.call`), so nothing is missed and// no caller writes anything.import Foundationpublic enum DevReads {    /// One finished call.    public struct Call: Sendable, Equatable {        public let method: String        public let started: ContinuousClock.Instant        public let millis: Double    }    /// Enough for any one surface; the oldest go first.    static let capacity = 500    private final class Box: @unchecked Sendable {        let lock = NSLock()        var calls: [Call] = []    }    private static let box = Box()    static func note(_ method: String, started: ContinuousClock.Instant) {        let elapsed = ContinuousClock.now - started        let millis = Double(elapsed.components.seconds) * 1000            + Double(elapsed.components.attoseconds) / 1e15        box.lock.lock()        defer { box.lock.unlock() }        box.calls.append(Call(method: method, started: started, millis: millis))        if box.calls.count > capacity { box.calls.removeFirst(box.calls.count - capacity) }    }    /// The calls that started at or after `instant`, oldest first.    public static func since(_ instant: ContinuousClock.Instant) -> [Call] {        box.lock.lock()        defer { box.lock.unlock() }        return box.calls.filter { $0.started >= instant }    }}#endif