From 910e14b4417e22adb2491a9694bcf5e56df67780 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Thu, 19 Mar 2020 22:22:58 -0700 Subject: [PATCH] =?UTF-8?q?Use=20a=20serial=20queue=20for=20RSImage.scaled?= =?UTF-8?q?ForIcon,=20so=20we=20don=E2=80=99t=20end=20up=20having=20a=20to?= =?UTF-8?q?n=20of=20threads=20and=20doing=20too=20much=20at=20once,=20whic?= =?UTF-8?q?h=20hurts=20performance.=20Fix=20#1489.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Shared/Extensions/RSImage-Extensions.swift | 33 +++++++++++++++++----- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/Shared/Extensions/RSImage-Extensions.swift b/Shared/Extensions/RSImage-Extensions.swift index e6f515270..94ae44892 100644 --- a/Shared/Extensions/RSImage-Extensions.swift +++ b/Shared/Extensions/RSImage-Extensions.swift @@ -6,6 +6,7 @@ // Copyright © 2019 Ranchero Software. All rights reserved. // +import RSCore #if os(macOS) import AppKit #else @@ -18,13 +19,8 @@ extension RSImage { static let maxIconSize = 48 - static func scaledForIcon(_ data: Data, imageResultBlock: @escaping (RSImage?) -> Void) { - DispatchQueue.global(qos: .default).async { - let image = RSImage.scaledForIcon(data) - DispatchQueue.main.async { - imageResultBlock(image) - } - } + static func scaledForIcon(_ data: Data, imageResultBlock: @escaping ImageResultBlock) { + IconScalerQueue.shared.scaledForIcon(data, imageResultBlock) } static func scaledForIcon(_ data: Data) -> RSImage? { @@ -41,3 +37,26 @@ extension RSImage { #endif } } + +// MARK: - IconScalerQueue + +private class IconScalerQueue { + + static let shared = IconScalerQueue() + + private let queue: DispatchQueue = { + let q = DispatchQueue(label: "IconScaler", attributes: .initiallyInactive) + q.setTarget(queue: DispatchQueue.global(qos: .default)) + q.activate() + return q + }() + + func scaledForIcon(_ data: Data, _ imageResultBlock: @escaping ImageResultBlock) { + queue.async { + let image = RSImage.scaledForIcon(data) + DispatchQueue.main.async { + imageResultBlock(image) + } + } + } +}