Skip to content

Conversation

patrickfreed
Copy link
Contributor

SWIFT-1088

This PR adds APIs to ExtendedJSONEncoder and ExtendedJSONDecoder which utilize NIO's ByteBuffer instead of Foundation's Data. This can help reduce needless copies when using ByteBuffer that would otherwise be required when routing through Data.

@patrickfreed patrickfreed changed the title SWIFT-1088 Support encoding to and decoding from extJSON ByteBuffers SWIFT-1088 ByteBuffer extJSON interoperability Feb 4, 2021
// readBytes never returns nil here because we checked that the buffer wasn't empty and only read
// readable bytes out from it.
// swiftlint:disable:next force_unwrapping
return try self.decodeBytes(T.self, from: buffer.readBytes(length: buffer.readableBytes)!)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

according to the swift-extras-json readme, it's more performant to read the bytes out of the buffer than it is to use ByteBufferView via readableBytesView. I think this is because the view relies on readInteger for each byte, which can be expensive to call repeatedly.

@patrickfreed patrickfreed requested a review from kmahar February 4, 2021 01:30
Copy link
Contributor

@kmahar kmahar left a comment

Choose a reason for hiding this comment

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

LGTM! did get me thinking about whether we should be showing example usage of our encoders and decoders in READMEs either here or on the driver itself which I opened SWIFT-1111 about (fun ticket number)

@patrickfreed patrickfreed merged commit 536781f into mongodb:master Feb 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants