From 3b31d6bbcdeb36fc5dd4021c5f6b8338c0896367 Mon Sep 17 00:00:00 2001 From: Ali <> Date: Mon, 9 Jan 2023 21:54:58 +0400 Subject: [PATCH] Experimental: roll back performance-related changes --- .../Display/Source/DisplayLinkAnimator.swift | 78 ++++++++++++++++--- .../Postbox/Sources/TimeBasedCleanup.swift | 7 +- 2 files changed, 74 insertions(+), 11 deletions(-) diff --git a/submodules/Display/Source/DisplayLinkAnimator.swift b/submodules/Display/Source/DisplayLinkAnimator.swift index a9c09cd0c7..ea96b27d53 100644 --- a/submodules/Display/Source/DisplayLinkAnimator.swift +++ b/submodules/Display/Source/DisplayLinkAnimator.swift @@ -1,10 +1,20 @@ import Foundation import UIKit +public protocol SharedDisplayLinkDriverLink: AnyObject { + var isPaused: Bool { get set } + + func invalidate() +} + public final class SharedDisplayLinkDriver { + public typealias Link = SharedDisplayLinkDriverLink + public static let shared = SharedDisplayLinkDriver() - public final class Link { + private let useNative: Bool + + public final class LinkImpl: Link { private let driver: SharedDisplayLinkDriver public let needsHighestFramerate: Bool let update: () -> Void @@ -28,10 +38,52 @@ public final class SharedDisplayLinkDriver { } } - private final class RequestContext { - weak var link: Link? + public final class NativeLinkImpl: Link { + private var displayLink: CADisplayLink? - init(link: Link) { + public var isPaused: Bool = false { + didSet { + self.displayLink?.isPaused = self.isPaused + } + } + + init(needsHighestFramerate: Bool, update: @escaping () -> Void) { + let displayLink = CADisplayLink(target: DisplayLinkTarget { + update() + }, selector: #selector(DisplayLinkTarget.event)) + + if #available(iOS 15.0, *) { + let maxFps = Float(UIScreen.main.maximumFramesPerSecond) + if maxFps > 61.0 { + let frameRateRange: CAFrameRateRange + if needsHighestFramerate { + frameRateRange = CAFrameRateRange(minimum: 30.0, maximum: 120.0, preferred: 120.0) + } else { + frameRateRange = .default + } + if displayLink.preferredFrameRateRange != frameRateRange { + displayLink.preferredFrameRateRange = frameRateRange + } + } + } + + self.displayLink = displayLink + displayLink.add(to: .main, forMode: .common) + } + + deinit { + self.displayLink?.invalidate() + } + + public func invalidate() { + self.displayLink?.invalidate() + } + } + + private final class RequestContext { + weak var link: LinkImpl? + + init(link: LinkImpl) { self.link = link } } @@ -43,6 +95,8 @@ public final class SharedDisplayLinkDriver { private var isInForeground: Bool = false private init() { + self.useNative = true + let _ = NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: nil, using: { [weak self] _ in guard let self else { return @@ -146,12 +200,16 @@ public final class SharedDisplayLinkDriver { } public func add(needsHighestFramerate: Bool = true, _ update: @escaping () -> Void) -> Link { - let link = Link(driver: self, needsHighestFramerate: needsHighestFramerate, update: update) - self.requests.append(RequestContext(link: link)) - - self.update() - - return link + if self.useNative { + return NativeLinkImpl(needsHighestFramerate: needsHighestFramerate, update: update) + } else { + let link = LinkImpl(driver: self, needsHighestFramerate: needsHighestFramerate, update: update) + self.requests.append(RequestContext(link: link)) + + self.update() + + return link + } } } diff --git a/submodules/Postbox/Sources/TimeBasedCleanup.swift b/submodules/Postbox/Sources/TimeBasedCleanup.swift index e541be9216..521422f22a 100644 --- a/submodules/Postbox/Sources/TimeBasedCleanup.swift +++ b/submodules/Postbox/Sources/TimeBasedCleanup.swift @@ -317,6 +317,11 @@ private final class TimeBasedCleanupImpl { } private func resetScan(general: Int32, shortLived: Int32, gigabytesLimit: Int32) { + if "".isEmpty { + //TODO:remove debugging + return + } + let generalPaths = self.generalPaths let totalSizeBasedPath = self.totalSizeBasedPath let shortLivedPaths = self.shortLivedPaths @@ -376,7 +381,7 @@ private final class TimeBasedCleanupImpl { } var performSizeMapping = true - if totalApproximateSize <= gigabytesLimit { + if totalApproximateSize <= bytesLimit { performSizeMapping = false }