Skip to content

Improve JSON serialization performance on Linux #718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 24, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions Foundation/NSJSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,13 @@ open class JSONSerialization : NSObject {
/* Generate JSON data from a Foundation object. If the object will not produce valid JSON then an exception will be thrown. Setting the NSJSONWritingPrettyPrinted option will generate JSON with whitespace designed to make the output more readable. If that option is not set, the most compact possible JSON will be generated. If an error occurs, the error parameter will be set and the return value will be nil. The resulting data is a encoded in UTF-8.
*/
internal class func _data(withJSONObject value: Any, options opt: WritingOptions, stream: Bool) throws -> Data {
var result = Data()
var jsonStr = String()

var writer = JSONWriter(
pretty: opt.contains(.prettyPrinted),
writer: { (str: String?) in
if let str = str {
let count = str.lengthOfBytes(using: .utf8)
result.append(UnsafeRawPointer(str.cString(using: .utf8)!).bindMemory(to: UInt8.self, capacity: count), count: count)
jsonStr.append(str)
}
}
)
Expand All @@ -131,6 +130,14 @@ open class JSONSerialization : NSObject {
}
}

let count = jsonStr.lengthOfBytes(using: .utf8)
let bufferLength = count+1 // Allow space for null terminator
var utf8: [CChar] = Array<CChar>(repeating: 0, count: bufferLength)
if !jsonStr.getCString(&utf8, maxLength: bufferLength, encoding: .utf8) {
fatalError("Failed to generate a CString from a String")
Copy link
Contributor

@parkera parkera Nov 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize we had a force-unwrap here in the previous code, but why would getCString fail here? If it did, is there a way to recover instead of asserting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. The API documentation for Swift String.getCString does not specify what the return value means, but I gather from the implementation that it calls through to NSString.getCString, the documentation for which says that false would be returned if the buffer is too small, or an encoding error occurs.
In this case, we sized the buffer appropriately, and because UTF8 can represent all Unicode code points, we shouldn't encounter an encoding error. Perhaps it would be safe to ignore the return value in this case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're sure it should not happen, then asserting on that is the right answer instead of ignoring.

}
let rawBytes = UnsafeRawPointer(UnsafePointer(utf8))
let result = Data(bytes: rawBytes.bindMemory(to: UInt8.self, capacity: count), count: count)
return result
}
open class func data(withJSONObject value: Any, options opt: WritingOptions = []) throws -> Data {
Expand Down