mirror of
https://github.com/apple/swift-log
synced 2026-11-25 03:57:48 +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?