mirror of
https://github.com/apple/swift-log
synced 2026-11-22 03:40:39 +00:00
Fix nested metadata values handling (#516)
Only quote leaf metadata values. ### Motivation: There are several issues with how nested `MetadataValue`s are handled: 1. For highly nested (10k+) metadata, `.description` and `==` operator hit stack overflow. This is probably not very realistic depth, but still. 2. For not so highly nested metadata (starting at about 20) `.description` and `==` operator get very computationally intensive because of re-escaping of the already escaped children. This makes the above stack overflow crash not reachable in practice. 3. This also results in simple structured metadata to be rendered something like `["[\"[\\\"[\\\\\\\"[\\\\\\\\\\\\\\\"leaf\\\\\\\\\\\\\\\"]\\\\\\\"]\\\"]\"]"]`. 4. Finally, even after solving the re-quoting and stack over flow on rendering, there is still an issue of [recursion in `.deinit`](https://github.com/swiftlang/swift/issues/58080), resulting in a _different_ stack overflow crash. This PR provides a fix for 2 and 3, leaving 1 and 4 out because there is no practical way of fixing it in this package, while the case is borderline unrealistic. ### Modifications: - Only quote left `String` and `StringRepresentable` values in `Logger.MetadataValue.description`. - Add benchmarks for metadata values description rendering. ### Result: 100 nested metadata values render cleanly. Resolves #515 ### Alternatives This is discussable if this fix is really necessary. Iterative approach, because of allocations, is slower than recursion. - Is it worth fixing the recursion, or a metadata depth check is enough? - Is it worth fixing the nested metadata formatting?
This commit is contained in:
1 parent
cf3e828428
commit
9c9acde6dc
7 files changed
+115
-4
No files matched your search
@@ -86,4 +86,21 @@ public let benchmarks: @Sendable () -> Void = {
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
// MARK: - metadata serialization performance
|
||||
|
||||
func nestedArrayMetadataValue(depth: Int) -> Logger.MetadataValue {
|
||||
var value: Logger.MetadataValue = .string("leaf")
|
||||
for _ in 0..<depth {
|
||||
value = .array([value])
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
for depth in [1, 5, 20, 1_000] {
|
||||
let nested = nestedArrayMetadataValue(depth: depth)
|
||||
makeBenchmark(loggerLevel: .info, logLevel: .debug, "_metadata_nesting_depth_\(depth)") { _ in
|
||||
blackHole(nested.description)
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"instructions" : 5483,
|
||||
"objectAllocCount" : 1
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"instructions" : 4939775,
|
||||
"objectAllocCount" : 2016
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"instructions" : 82239,
|
||||
"objectAllocCount" : 37
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"instructions" : 18351,
|
||||
"objectAllocCount" : 6
|
||||
}
|
||||
@@ -1414,14 +1414,29 @@ extension Logger.MetadataValue: CustomStringConvertible {
|
||||
/// A string representation of the metadata value.
|
||||
public var description: String {
|
||||
switch self {
|
||||
case .dictionary(let dict):
|
||||
return dict.mapValues { $0.description }.description
|
||||
case .array(let list):
|
||||
return list.map { $0.description }.description
|
||||
case .string(let str):
|
||||
return str
|
||||
case .stringConvertible(let repr):
|
||||
return repr.description
|
||||
case .array(let list):
|
||||
let items = list.map { $0.quotedAsCollectionElement }
|
||||
return "[\(items.joined(separator: ", "))]"
|
||||
case .dictionary(let dict):
|
||||
guard !dict.isEmpty else { return "[:]" }
|
||||
let entries = dict.map { key, value in "\(key.debugDescription): \(value.quotedAsCollectionElement)" }
|
||||
return "[\(entries.joined(separator: ", "))]"
|
||||
}
|
||||
}
|
||||
|
||||
/// Only qute the leaf
|
||||
private var quotedAsCollectionElement: String {
|
||||
switch self {
|
||||
case .string(let str):
|
||||
return str.debugDescription
|
||||
case .stringConvertible(let repr):
|
||||
return repr.description.debugDescription
|
||||
case .array, .dictionary:
|
||||
return self.description
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Logging API open source project
|
||||
//
|
||||
// Copyright (c) 2026 Apple Inc. and the Swift Logging API project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of Swift Logging API project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Logging
|
||||
import Testing
|
||||
|
||||
@Suite
|
||||
struct MetadataValueTest {
|
||||
@Test
|
||||
func descriptionRendering() {
|
||||
#expect(Logger.MetadataValue.array([]).description == "[]")
|
||||
#expect(Logger.MetadataValue.array(["a", "b"]).description == ["a", "b"].description)
|
||||
|
||||
#expect(Logger.MetadataValue.dictionary([:]).description == "[:]")
|
||||
#expect(Logger.MetadataValue.dictionary(["k": "v"]).description == ["k": "v"].description)
|
||||
#expect(
|
||||
Logger.MetadataValue.dictionary(["outer": ["inner": "leaf"]]).description
|
||||
== "[\"outer\": [\"inner\": \"leaf\"]]"
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
func equatableCompareson() {
|
||||
let lhs: Logger.MetadataValue = ["a": ["1", "2"]]
|
||||
let rhs: Logger.MetadataValue = ["a": ["1", "2"]]
|
||||
let different: Logger.MetadataValue = ["a": ["1", "3"]]
|
||||
|
||||
#expect(lhs == rhs)
|
||||
#expect(lhs != different)
|
||||
}
|
||||
|
||||
@Test
|
||||
func descriptionOfNestedArraysGrowsLinearlyNotExponentially() {
|
||||
var value: Logger.MetadataValue = .string("leaf")
|
||||
for _ in 0..<100 {
|
||||
value = .array([value])
|
||||
}
|
||||
// "leaf".debugDescription + each level adds a "[" and "]".
|
||||
#expect(value.description.count == 6 + 2 * 100)
|
||||
}
|
||||
|
||||
@Test
|
||||
func equatableComparesNestedArrays() {
|
||||
var lhs: Logger.MetadataValue = .string("leaf")
|
||||
var rhs: Logger.MetadataValue = .string("leaf")
|
||||
for _ in 0..<100 {
|
||||
lhs = .array([lhs])
|
||||
rhs = .array([rhs])
|
||||
}
|
||||
#expect(lhs == rhs)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user