From 0f170b4764e0d3905e6c699b76013eaba19e7ee7 Mon Sep 17 00:00:00 2001 From: Peter Adams <63288215+PeterAdams-A@users.noreply.github.com> Date: Mon, 10 Jun 2024 14:17:43 +0100 Subject: [PATCH] Use Set to spot duplicated log handler warnings. (#306) Motivation: Feels like a better fit for the problem when a dictionary of bools. Modifications: Convert storage to set. Result: Fewer lines of code, less waste. --- Sources/Logging/Logging.swift | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index fdb1f13..7f4f524 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -1347,17 +1347,13 @@ extension Logger.MetadataValue: ExpressibleByArrayLiteral { /// Contains state to manage all kinds of "warn only once" warnings which the logging system may want to issue. private final class WarnOnceBox { private let lock: Lock = Lock() - private var warnOnceLogHandlerNotSupportedMetadataProviderPerType: [ObjectIdentifier: Bool] = [:] + private var warnOnceLogHandlerNotSupportedMetadataProviderPerType = Set() func warnOnceLogHandlerNotSupportedMetadataProvider(type: Handler.Type) -> Bool { self.lock.withLock { let id = ObjectIdentifier(type) - if warnOnceLogHandlerNotSupportedMetadataProviderPerType[id] ?? false { - return false // don't warn, it was already warned about - } else { - warnOnceLogHandlerNotSupportedMetadataProviderPerType[id] = true - return true // warn about this handler type, it is the first time we encountered it - } + let (inserted, _) = warnOnceLogHandlerNotSupportedMetadataProviderPerType.insert(id) + return inserted // warn about this handler type, it is the first time we encountered it } } }