From 7a0bc7cf11d6956bf8008c7b46b227672878ad28 Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari Date: Wed, 8 May 2024 20:51:32 -0400 Subject: [PATCH 01/19] feat: Implement Copy-On-Write (CoW) behavior for Logger struct --- Sources/Logging/Logging.swift | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index 8bcf243d..da18ea6d 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -39,20 +39,41 @@ import WASILibc /// logger.info("Hello World!") /// ``` public struct Logger { - @usableFromInline - var handler: LogHandler + /// A private property to hold the boxed `LogHandler`. + private var _handler: Box /// An identifier of the creator of this `Logger`. public let label: String + + /// A computed property to access the `LogHandler`. + public var handler: LogHandler { + get { + return _handler.value + } + set { + if !(isKnownUniquelyReferenced(&_handler)) { + _handler = Box(value: newValue) + } else { + _handler.value = newValue + } + } + } /// The metadata provider this logger was created with. public var metadataProvider: Logger.MetadataProvider? { - return self.handler.metadataProvider + return handler.metadataProvider } internal init(label: String, _ handler: LogHandler) { self.label = label - self.handler = handler + self._handler = Box(value: handler) + } +} + +private final class Box { + var value: T + init(value: T) { + self.value = value } } From cfdef5af1eb6b28668eacea5c4fe019c05b01207 Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari Date: Thu, 9 May 2024 20:17:29 -0400 Subject: [PATCH 02/19] fix: refactored the struct and added test --- Sources/Logging/Logging.swift | 25 ++++++++++++--------- Tests/LoggingTests/LoggingTest+XCTest.swift | 1 + Tests/LoggingTests/LoggingTest.swift | 5 +++++ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index 190abd53..d802f009 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -40,7 +40,7 @@ import WASILibc /// ``` public struct Logger { /// A private property to hold the boxed `LogHandler`. - private var _handler: Box + private var _handler: Storage /// An identifier of the creator of this `Logger`. public let label: String @@ -48,13 +48,13 @@ public struct Logger { /// A computed property to access the `LogHandler`. public var handler: LogHandler { get { - return _handler.value + return _handler.handler } set { - if !(isKnownUniquelyReferenced(&_handler)) { - _handler = Box(value: newValue) + if !isKnownUniquelyReferenced(&self._handler) { + self._handler = Storage(label: self.label, handler: newValue) } else { - _handler.value = newValue + self._handler.handler = newValue } } } @@ -66,14 +66,17 @@ public struct Logger { internal init(label: String, _ handler: LogHandler) { self.label = label - self._handler = Box(value: handler) + self._handler = Storage(label: label, handler: handler) } -} -private final class Box { - var value: T - init(value: T) { - self.value = value + public class Storage { + var label: String + var handler: LogHandler + + init(label: String, handler: LogHandler) { + self.label = label + self.handler = handler + } } } diff --git a/Tests/LoggingTests/LoggingTest+XCTest.swift b/Tests/LoggingTests/LoggingTest+XCTest.swift index c30d1282..2962a786 100644 --- a/Tests/LoggingTests/LoggingTest+XCTest.swift +++ b/Tests/LoggingTests/LoggingTest+XCTest.swift @@ -63,6 +63,7 @@ extension LoggingTest { ("testStdioOutputStreamFlush", testStdioOutputStreamFlush), ("testOverloadingError", testOverloadingError), ("testCompileInitializeStandardStreamLogHandlersWithMetadataProviders", testCompileInitializeStandardStreamLogHandlersWithMetadataProviders), + ("testLoggerSize", testLoggerSize), ] } } diff --git a/Tests/LoggingTests/LoggingTest.swift b/Tests/LoggingTests/LoggingTest.swift index 90ed838e..bd2966d1 100644 --- a/Tests/LoggingTests/LoggingTest.swift +++ b/Tests/LoggingTests/LoggingTest.swift @@ -1053,6 +1053,11 @@ class LoggingTest: XCTestCase { LoggingSystem.bootstrap(StreamLogHandler.standardOutput, metadataProvider: .exampleMetadataProvider) LoggingSystem.bootstrap(StreamLogHandler.standardError, metadataProvider: .exampleMetadataProvider) } + + func testLoggerSize() { + let expectedSize = MemoryLayout.size + MemoryLayout.size + XCTAssertEqual(MemoryLayout.size, expectedSize) + } } extension Logger { From 1dce8747d94927de6348deded9406faf332a845a Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari Date: Thu, 9 May 2024 20:24:46 -0400 Subject: [PATCH 03/19] fix: refactored the struct --- Sources/Logging/Logging.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index d802f009..4318dad3 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -69,7 +69,7 @@ public struct Logger { self._handler = Storage(label: label, handler: handler) } - public class Storage { + public class Storage: @unchecked Sendable { var label: String var handler: LogHandler From 53972d85139d3a43213079fa08b75878677e8e80 Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari Date: Sat, 11 May 2024 09:30:42 -0400 Subject: [PATCH 04/19] fix: refactored the struct --- Sources/Logging/Logging.swift | 55 +++++++++++++-------- Tests/LoggingTests/LoggingTest+XCTest.swift | 2 +- Tests/LoggingTests/LoggingTest.swift | 4 +- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index 4318dad3..4e953e5f 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -39,22 +39,46 @@ import WASILibc /// logger.info("Hello World!") /// ``` public struct Logger { - /// A private property to hold the boxed `LogHandler`. - private var _handler: Storage - - /// An identifier of the creator of this `Logger`. - public let label: String + /// Storage class to hold the label and log handler/ + @usableFromInline + internal final class Storage: @unchecked /* and not actually */ Sendable /* but safe if only used with CoW */ { + @usableFromInline + var label: String + @usableFromInline + var handler: LogHandler + + init(label: String, handler: LogHandler) { + self.label = label + self.handler = handler + } + } + @usableFromInline + internal var _storage: Storage + + internal var label: String { + get { + return _storage.label + } + set { + if !isKnownUniquelyReferenced(&_storage) { + _storage = Storage(label: newValue, handler: _storage.handler) + } else { + _storage.label = newValue + } + } + } + /// A computed property to access the `LogHandler`. public var handler: LogHandler { get { - return _handler.handler + return _storage.handler } set { - if !isKnownUniquelyReferenced(&self._handler) { - self._handler = Storage(label: self.label, handler: newValue) + if !isKnownUniquelyReferenced(&_storage) { + _storage = Storage(label: label, handler: newValue) } else { - self._handler.handler = newValue + _storage.handler = newValue } } } @@ -65,18 +89,7 @@ public struct Logger { } internal init(label: String, _ handler: LogHandler) { - self.label = label - self._handler = Storage(label: label, handler: handler) - } - - public class Storage: @unchecked Sendable { - var label: String - var handler: LogHandler - - init(label: String, handler: LogHandler) { - self.label = label - self.handler = handler - } + self._storage = Storage(label: label, handler: handler) } } diff --git a/Tests/LoggingTests/LoggingTest+XCTest.swift b/Tests/LoggingTests/LoggingTest+XCTest.swift index 2962a786..f9ef8b69 100644 --- a/Tests/LoggingTests/LoggingTest+XCTest.swift +++ b/Tests/LoggingTests/LoggingTest+XCTest.swift @@ -63,7 +63,7 @@ extension LoggingTest { ("testStdioOutputStreamFlush", testStdioOutputStreamFlush), ("testOverloadingError", testOverloadingError), ("testCompileInitializeStandardStreamLogHandlersWithMetadataProviders", testCompileInitializeStandardStreamLogHandlersWithMetadataProviders), - ("testLoggerSize", testLoggerSize), + ("testLoggerIsJustHoldingASinglePointer", testLoggerIsJustHoldingASinglePointer), ] } } diff --git a/Tests/LoggingTests/LoggingTest.swift b/Tests/LoggingTests/LoggingTest.swift index bd2966d1..47a593de 100644 --- a/Tests/LoggingTests/LoggingTest.swift +++ b/Tests/LoggingTests/LoggingTest.swift @@ -1054,8 +1054,8 @@ class LoggingTest: XCTestCase { LoggingSystem.bootstrap(StreamLogHandler.standardError, metadataProvider: .exampleMetadataProvider) } - func testLoggerSize() { - let expectedSize = MemoryLayout.size + MemoryLayout.size + func testLoggerIsJustHoldingASinglePointer() { + let expectedSize = MemoryLayout.size XCTAssertEqual(MemoryLayout.size, expectedSize) } } From f9b85b86d70eb0a9ed4bdcd9222cfb4f5b5d26f7 Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari Date: Mon, 13 May 2024 10:42:45 -0400 Subject: [PATCH 05/19] =?UTF-8?q?fix:Added=20compiler=20directive=20and?= =?UTF-8?q?=C2=A0@usableFromInline/@inlinable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Logging/Logging.swift | 38 +++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index 4e953e5f..e514c9dc 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -38,24 +38,39 @@ import WASILibc /// ```swift /// logger.info("Hello World!") /// ``` + public struct Logger { - /// Storage class to hold the label and log handler/ - @usableFromInline - internal final class Storage: @unchecked /* and not actually */ Sendable /* but safe if only used with CoW */ { + #if compiler(>=5.7) + /// Storage class to hold the label and log handler/ @usableFromInline - var label: String + internal final class Storage: @unchecked /* and not actually */ Sendable /* but safe if only used with CoW */ { + @usableFromInline + var label: String + @usableFromInline + var handler: LogHandler + + init(label: String, handler: LogHandler) { + self.label = label + self.handler = handler + } + } + #else @usableFromInline - var handler: LogHandler - - init(label: String, handler: LogHandler) { - self.label = label - self.handler = handler + internal final class Storage: { + @usableFromInline + var label: String + @usableFromInline + var handler: LogHandler + + init(label: String, handler: LogHandler) { + self.label = label + self.handler = handler + } } - } @usableFromInline internal var _storage: Storage - + @usableFromInline internal var label: String { get { return _storage.label @@ -70,6 +85,7 @@ public struct Logger { } /// A computed property to access the `LogHandler`. + @inlinable public var handler: LogHandler { get { return _storage.handler From c7274d5f584df789878d7cab7a365a68dde3864d Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari Date: Mon, 13 May 2024 10:46:00 -0400 Subject: [PATCH 06/19] fix: Add endif label --- Sources/Logging/Logging.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index e514c9dc..f3d2bede 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -67,6 +67,7 @@ public struct Logger { self.handler = handler } } + #endif @usableFromInline internal var _storage: Storage From 76953e4101bae85033d3b39a0700da214bfb5bc3 Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari Date: Mon, 13 May 2024 10:48:03 -0400 Subject: [PATCH 07/19] fix: Remove colon causing expected type --- Sources/Logging/Logging.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index f3d2bede..58003057 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -56,7 +56,7 @@ public struct Logger { } #else @usableFromInline - internal final class Storage: { + internal final class Storage { @usableFromInline var label: String @usableFromInline From 1b3bde1dbba9da0ff3191c2ae29eab0d9e7cca4d Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari Date: Wed, 15 May 2024 09:40:44 -0400 Subject: [PATCH 08/19] fix: Add inlinable and moving compiler derective --- Sources/Logging/Logging.swift | 39 ++++++++++++----------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index 58003057..c624024e 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -40,34 +40,19 @@ import WASILibc /// ``` public struct Logger { - #if compiler(>=5.7) - /// Storage class to hold the label and log handler/ + /// Storage class to hold the label and log handler/ + @usableFromInline + internal final class Storage { @usableFromInline - internal final class Storage: @unchecked /* and not actually */ Sendable /* but safe if only used with CoW */ { - @usableFromInline - var label: String - @usableFromInline - var handler: LogHandler - - init(label: String, handler: LogHandler) { - self.label = label - self.handler = handler - } - } - #else + var label: String @usableFromInline - internal final class Storage { - @usableFromInline - var label: String - @usableFromInline - var handler: LogHandler - - init(label: String, handler: LogHandler) { - self.label = label - self.handler = handler - } + var handler: LogHandler + @usableFromInline + init(label: String, handler: LogHandler) { + self.label = label + self.handler = handler } - #endif + } @usableFromInline internal var _storage: Storage @@ -104,7 +89,7 @@ public struct Logger { public var metadataProvider: Logger.MetadataProvider? { return handler.metadataProvider } - + @usableFromInline internal init(label: String, _ handler: LogHandler) { self._storage = Storage(label: label, handler: handler) } @@ -1594,6 +1579,8 @@ private final class WarnOnceBox { #if compiler(>=5.7.0) extension Logger.MetadataValue: Sendable {} // on 5.7 `stringConvertible`'s value marked as Sendable; but if a value not conforming to Sendable is passed there, a warning is emitted. We are okay with warnings, but on 5.6 for the same situation an error is emitted (!) +extension Logger.Storage: @unchecked Sendable {} + #elseif compiler(>=5.6) extension Logger.MetadataValue: @unchecked Sendable {} // sadly, On 5.6 a missing Sendable conformance causes an 'error' (specifically this is about `stringConvertible`'s value) #endif From 4c132794f7c73df415a8e51fe781e64fa705dc6b Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari Date: Wed, 15 May 2024 09:52:34 -0400 Subject: [PATCH 09/19] nit --- Sources/Logging/Logging.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index c624024e..46032ecf 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -40,7 +40,7 @@ import WASILibc /// ``` public struct Logger { - /// Storage class to hold the label and log handler/ + /// Storage class to hold the label and log handler @usableFromInline internal final class Storage { @usableFromInline From 315fd6c64597e1874a385b95ea196020053853d4 Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari Date: Wed, 15 May 2024 10:09:27 -0400 Subject: [PATCH 10/19] fix: add inlinable --- Sources/Logging/Logging.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index 46032ecf..280b5c3c 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -86,6 +86,7 @@ public struct Logger { } /// The metadata provider this logger was created with. + @inlinable public var metadataProvider: Logger.MetadataProvider? { return handler.metadataProvider } From 7bd886997afbe6224b05833514a53758ee5a6826 Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari <43102557+ayushi2103@users.noreply.github.com> Date: Fri, 17 May 2024 08:40:34 -0400 Subject: [PATCH 11/19] Update Sources/Logging/Logging.swift Co-authored-by: Johannes Weiss --- Sources/Logging/Logging.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index 0fe6cf64..a7ab4014 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -74,7 +74,7 @@ public struct Logger { @inlinable public var handler: LogHandler { get { - return _storage.handler + return self._storage.handler } set { if !isKnownUniquelyReferenced(&_storage) { From 9e5e25751b9ee92f7a972590d493e80264ff4a6e Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari <43102557+ayushi2103@users.noreply.github.com> Date: Fri, 17 May 2024 08:41:00 -0400 Subject: [PATCH 12/19] Update Sources/Logging/Logging.swift Co-authored-by: Johannes Weiss --- Sources/Logging/Logging.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index a7ab4014..245ee7fe 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -65,7 +65,7 @@ public struct Logger { if !isKnownUniquelyReferenced(&_storage) { _storage = Storage(label: newValue, handler: _storage.handler) } else { - _storage.label = newValue + self._storage.label = newValue } } } From 282d676f3b892fb4f05774e6f82e73abd1b6db9a Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari <43102557+ayushi2103@users.noreply.github.com> Date: Fri, 17 May 2024 18:51:32 -0400 Subject: [PATCH 13/19] Update Sources/Logging/Logging.swift Co-authored-by: Franz Busch --- Sources/Logging/Logging.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index 245ee7fe..efcf67fa 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -47,6 +47,7 @@ public struct Logger { var label: String @usableFromInline var handler: LogHandler + @usableFromInline init(label: String, handler: LogHandler) { self.label = label From ad18e6d96e7bddafe414d29d01da292fef3ac41d Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari <43102557+ayushi2103@users.noreply.github.com> Date: Fri, 17 May 2024 18:51:49 -0400 Subject: [PATCH 14/19] Update Sources/Logging/Logging.swift Co-authored-by: Franz Busch --- Sources/Logging/Logging.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index efcf67fa..af3370f4 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -38,7 +38,6 @@ import WASILibc /// ```swift /// logger.info("Hello World!") /// ``` - public struct Logger { /// Storage class to hold the label and log handler @usableFromInline From 078231d1138387a7b5445ec21afc6ce50ae6a22e Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari Date: Sun, 19 May 2024 20:00:18 -0400 Subject: [PATCH 15/19] fix: add self to class declaration and fixed formatting --- Sources/Logging/Logging.swift | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index af3370f4..f755ed44 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -41,17 +41,23 @@ import WASILibc public struct Logger { /// Storage class to hold the label and log handler @usableFromInline - internal final class Storage { + internal final class Storage: @unchecked /* and not actually */ Sendable /* but safe if only used with CoW */ { @usableFromInline var label: String + @usableFromInline var handler: LogHandler - @usableFromInline + @inlinable init(label: String, handler: LogHandler) { self.label = label self.handler = handler } + + @inlinable + func copy() -> Storage { + return Storage(label: self.label, handler: self.handler) + } } @usableFromInline @@ -59,14 +65,13 @@ public struct Logger { @usableFromInline internal var label: String { get { - return _storage.label + return self._storage.label } set { - if !isKnownUniquelyReferenced(&_storage) { - _storage = Storage(label: newValue, handler: _storage.handler) - } else { - self._storage.label = newValue + if !isKnownUniquelyReferenced(&self._storage) { + self._storage = self._storage.copy() } + self._storage.label = newValue // mutation is fine now because we just copied } } @@ -77,10 +82,10 @@ public struct Logger { return self._storage.handler } set { - if !isKnownUniquelyReferenced(&_storage) { - _storage = Storage(label: label, handler: newValue) + if !isKnownUniquelyReferenced(&self._storage) { + self._storage = Storage(label: label, handler: newValue) } else { - _storage.handler = newValue + self._storage.handler = newValue } } } @@ -1370,7 +1375,6 @@ private final class WarnOnceBox { // MARK: - Sendable support helpers extension Logger.MetadataValue: Sendable {} -extension Logger.Storage: @unchecked Sendable {} extension Logger: Sendable {} extension Logger.Level: Sendable {} extension Logger.Message: Sendable {} From c235ce09c8276f325a5fbdd246870d414471cc33 Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari Date: Tue, 21 May 2024 09:19:14 -0400 Subject: [PATCH 16/19] fix: make label public --- Sources/Logging/Logging.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index f755ed44..a9cba2af 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -62,8 +62,7 @@ public struct Logger { @usableFromInline internal var _storage: Storage - @usableFromInline - internal var label: String { + public var label: String { get { return self._storage.label } From 5bb8164b71585d391cd384bb2635fe5aa8cd388e Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari Date: Tue, 21 May 2024 09:43:32 -0400 Subject: [PATCH 17/19] fix: format --- Sources/Logging/Logging.swift | 9 +++++---- Tests/LoggingTests/LoggingTest.swift | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index a9cba2af..9e974338 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -53,13 +53,13 @@ public struct Logger { self.label = label self.handler = handler } - + @inlinable func copy() -> Storage { return Storage(label: self.label, handler: self.handler) } } - + @usableFromInline internal var _storage: Storage public var label: String { @@ -82,7 +82,7 @@ public struct Logger { } set { if !isKnownUniquelyReferenced(&self._storage) { - self._storage = Storage(label: label, handler: newValue) + self._storage = Storage(label: self.label, handler: newValue) } else { self._storage.handler = newValue } @@ -92,8 +92,9 @@ public struct Logger { /// The metadata provider this logger was created with. @inlinable public var metadataProvider: Logger.MetadataProvider? { - return handler.metadataProvider + return self.handler.metadataProvider } + @usableFromInline internal init(label: String, _ handler: LogHandler) { self._storage = Storage(label: label, handler: handler) diff --git a/Tests/LoggingTests/LoggingTest.swift b/Tests/LoggingTests/LoggingTest.swift index c748137a..2972fe99 100644 --- a/Tests/LoggingTests/LoggingTest.swift +++ b/Tests/LoggingTests/LoggingTest.swift @@ -1039,7 +1039,7 @@ class LoggingTest: XCTestCase { LoggingSystem.bootstrap(StreamLogHandler.standardOutput, metadataProvider: .exampleMetadataProvider) LoggingSystem.bootstrap(StreamLogHandler.standardError, metadataProvider: .exampleMetadataProvider) } - + func testLoggerIsJustHoldingASinglePointer() { let expectedSize = MemoryLayout.size XCTAssertEqual(MemoryLayout.size, expectedSize) From 98e5b9dd3ccc351b21a555a80536ee65fe2afeb4 Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari Date: Wed, 22 May 2024 09:30:06 -0400 Subject: [PATCH 18/19] fix: remove setter --- Sources/Logging/Logging.swift | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index 9e974338..e613b0e9 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -63,15 +63,7 @@ public struct Logger { @usableFromInline internal var _storage: Storage public var label: String { - get { - return self._storage.label - } - set { - if !isKnownUniquelyReferenced(&self._storage) { - self._storage = self._storage.copy() - } - self._storage.label = newValue // mutation is fine now because we just copied - } + return self._storage.label } /// A computed property to access the `LogHandler`. From 5b7e1bd28644ce4ae87723e50188528d19f1ff9e Mon Sep 17 00:00:00 2001 From: Ayushi Tiwari Date: Thu, 30 May 2024 05:51:14 -0400 Subject: [PATCH 19/19] test: added a test case --- Sources/Logging/Logging.swift | 5 ++--- Tests/LoggingTests/LoggingTest.swift | 9 +++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index e613b0e9..fdb1f138 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -74,10 +74,9 @@ public struct Logger { } set { if !isKnownUniquelyReferenced(&self._storage) { - self._storage = Storage(label: self.label, handler: newValue) - } else { - self._storage.handler = newValue + self._storage = self._storage.copy() } + self._storage.handler = newValue } } diff --git a/Tests/LoggingTests/LoggingTest.swift b/Tests/LoggingTests/LoggingTest.swift index 2972fe99..c086473f 100644 --- a/Tests/LoggingTests/LoggingTest.swift +++ b/Tests/LoggingTests/LoggingTest.swift @@ -1044,6 +1044,15 @@ class LoggingTest: XCTestCase { let expectedSize = MemoryLayout.size XCTAssertEqual(MemoryLayout.size, expectedSize) } + + func testLoggerCopyOnWrite() { + var logger1 = Logger(label: "foo") + logger1.logLevel = .error + var logger2 = logger1 + logger2.logLevel = .trace + XCTAssertEqual(.error, logger1.logLevel) + XCTAssertEqual(.trace, logger2.logLevel) + } } extension Logger {