From a29b39fc876e1830a46e9005f0d954a4b0ea670f Mon Sep 17 00:00:00 2001 From: "louis.pontoise" Date: Wed, 12 Feb 2020 20:20:44 +0900 Subject: [PATCH] feat: make system calls more parallel (closes #160) --- alt-tab-macos/api-wrappers/HelperExtensions.swift | 15 +++++++++++++++ alt-tab-macos/logic/Windows.swift | 13 ++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/alt-tab-macos/api-wrappers/HelperExtensions.swift b/alt-tab-macos/api-wrappers/HelperExtensions.swift index db2bc0b1..2a1eb5e1 100644 --- a/alt-tab-macos/api-wrappers/HelperExtensions.swift +++ b/alt-tab-macos/api-wrappers/HelperExtensions.swift @@ -107,3 +107,18 @@ extension NSGridView { } } } + +extension Array { + // forEach with each iteration run concurrently on the global queue + func forEachAsync(fn: @escaping (Element) -> Void) { + let group = DispatchGroup() + for element in self { + group.enter() + DispatchQueue.global(qos: .userInteractive).async(group: group) { + fn(element) + group.leave() + } + } + group.wait() + } +} diff --git a/alt-tab-macos/logic/Windows.swift b/alt-tab-macos/logic/Windows.swift index 7192ed7d..b6656858 100644 --- a/alt-tab-macos/logic/Windows.swift +++ b/alt-tab-macos/logic/Windows.swift @@ -26,17 +26,16 @@ class Windows { static func updateSpaces() { let spacesMap = Spaces.allIdsAndIndexes() - for window in list { + list.forEachAsync { window in let spaceIds = window.cgWindowId.spaces() - guard spaceIds.count > 0 else { continue } - if spaceIds.count > 1 { + if spaceIds.count == 1 { + window.spaceId = spaceIds.first! + window.spaceIndex = spacesMap.first { $0.0 == spaceIds.first! }!.1 + } else if spaceIds.count > 1 { window.spaceId = Spaces.currentSpaceId window.spaceIndex = Spaces.currentSpaceIndex window.isOnAllSpaces = true - continue } - window.spaceId = spaceIds.first! - window.spaceIndex = spacesMap.first { $0.0 == spaceIds.first! }!.1 } } @@ -59,7 +58,7 @@ class Windows { } static func refreshAllThumbnails() { - for window in list { + list.forEachAsync { window in window.refreshThumbnail() } }