mirror of
https://github.com/apple/swift-log
synced 2026-11-23 03:44:22 +00:00
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?
64 lines
2.0 KiB
Swift
64 lines
2.0 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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)
|
|
}
|
|
}
|