Skip to content

[SR-6252] JSONDecoder is unnecessarily slow #3795

Open
@bjhomer

Description

@bjhomer
Previous ID SR-6252
Radar rdar://problem/35301452
Original Reporter @bjhomer
Type Improvement

Attachment: Download

Additional Detail from JIRA
Votes 1
Component/s Foundation, Standard Library
Labels Improvement, Codable
Assignee @itaiferber
Priority Medium

md5: eb4b493ba98c649f2cca2b70447bb532

Issue Description:

When converting a large JSON file, Swift's JSONDecoder type takes far longer than similar functionality from 3rd-party libraries. In investigating this, I've found that nearly all the time is spent in swift_dynamicCast(), converting from NSDictionary and [String: Any]. Since JSONSerialization produces NSDictionary objects, this conversion happens often.

In one case, involving a 7MB JSON file, I was able to cut the decoding time from ~1s to ~0.25s simply by making JSONDecoder work with NSDictionary directly, rather than converting to the Swift-native type. Since the underlying storage is already an NSDictionary for the moment, this seems like a natural optimization.

I'm attaching a test application that includes the changes to JSONDecoder. The JSON file is pulled from https://github.com/bwhiteley/JSONShootout.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions