When decoding a complex JSON payload with deeply nested structures, the first decode using YYJSONDecoder takes approximately 7 seconds, while Foundation's JSONDecoder completes in ~0.25 seconds. Subsequent decodes with YYJSON are fast (~1ms).
Environment:
- swift-yyjson: 0.3.0 (04748af)
- Swift: 6.1.2
- Platform: macOS 15.0, arm64
Reproduction:
The payload is a ~660KB JSON blob containing a struct with a value: JSON? field (from SwifterJSON) that contains deeply nested arbitrary JSON.
// Timing comparison
let payload: Data = ... // ~660KB JSON
// YYJSON - first decode
let t0 = Date()
let result1 = try YYJSONDecoder().decode(ElectricEventPayload.self, from: payload)
print("YYJSON: (Date().timeIntervalSince(t0))s") // ~7 seconds
// Foundation - first decode
let t1 = Date()
let result2 = try JSONDecoder().decode(ElectricEventPayload.self, from: payload)
print("Foundation: (Date().timeIntervalSince(t1))s") // ~0.25 seconds
Observations:
- The slowness only affects the first decode
- Subsequent YYJSON decodes of similar payloads are fast
- The issue may be related to recursive Decodable handling for nested JSON types
- Creating a shared decoder instance doesn't help (ruled out initialization overhead)
When decoding a complex JSON payload with deeply nested structures, the first decode using YYJSONDecoder takes approximately 7 seconds, while Foundation's JSONDecoder completes in ~0.25 seconds. Subsequent decodes with YYJSON are fast (~1ms).
Environment:
Reproduction:
The payload is a ~660KB JSON blob containing a struct with a value: JSON? field (from SwifterJSON) that contains deeply nested arbitrary JSON.
// Timing comparison
let payload: Data = ... // ~660KB JSON
// YYJSON - first decode
let t0 = Date()
let result1 = try YYJSONDecoder().decode(ElectricEventPayload.self, from: payload)
print("YYJSON: (Date().timeIntervalSince(t0))s") // ~7 seconds
// Foundation - first decode
let t1 = Date()
let result2 = try JSONDecoder().decode(ElectricEventPayload.self, from: payload)
print("Foundation: (Date().timeIntervalSince(t1))s") // ~0.25 seconds
Observations: