From 9c9acde6dc9bffb0727cfecb7f61228bc416b33f Mon Sep 17 00:00:00 2001 From: Vladimir Kukushkin Date: Wed, 23 Sep 2026 10:13:24 +0100 Subject: [PATCH] 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? --- .../NoTraitsBenchmarks/NoTraits.swift | 17 +++++ ...og_level_metadata_nesting_depth_1.p90.json | 4 ++ ...level_metadata_nesting_depth_1000.p90.json | 4 ++ ...g_level_metadata_nesting_depth_20.p90.json | 4 ++ ...og_level_metadata_nesting_depth_5.p90.json | 4 ++ Sources/Logging/Logger.swift | 23 +++++-- Tests/LoggingTests/MetadataValueTest.swift | 63 +++++++++++++++++++ 7 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 Benchmarks/NoTraits/Thresholds/Xcode swift 6.3/NoTraits.debug_log_with_info_log_level_metadata_nesting_depth_1.p90.json create mode 100644 Benchmarks/NoTraits/Thresholds/Xcode swift 6.3/NoTraits.debug_log_with_info_log_level_metadata_nesting_depth_1000.p90.json create mode 100644 Benchmarks/NoTraits/Thresholds/Xcode swift 6.3/NoTraits.debug_log_with_info_log_level_metadata_nesting_depth_20.p90.json create mode 100644 Benchmarks/NoTraits/Thresholds/Xcode swift 6.3/NoTraits.debug_log_with_info_log_level_metadata_nesting_depth_5.p90.json create mode 100644 Tests/LoggingTests/MetadataValueTest.swift diff --git a/Benchmarks/NoTraits/Benchmarks/NoTraitsBenchmarks/NoTraits.swift b/Benchmarks/NoTraits/Benchmarks/NoTraitsBenchmarks/NoTraits.swift index cc9feb8..ca52816 100644 --- a/Benchmarks/NoTraits/Benchmarks/NoTraitsBenchmarks/NoTraits.swift +++ b/Benchmarks/NoTraits/Benchmarks/NoTraitsBenchmarks/NoTraits.swift @@ -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..