Skip to content

Eliminates redundant objects creations in NSData base64 decoding #2773

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 1 commit into from
May 15, 2020

Conversation

valeriyvan
Copy link
Contributor

No description provided.

@@ -670,7 +671,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
- parameter options: Options for handling invalid input
- returns: The decoded bytes.
*/
private static func base64DecodeBytes(_ bytes: [UInt8], options: Base64DecodingOptions = []) -> [UInt8]? {
private static func base64DecodeBytes(_ bytes: UnsafeRawBufferPointer, options: Base64DecodingOptions = []) -> [UInt8]? {
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than accepting an UnsafeRawBufferPointer it would be safer to change the signature to :

private static func base64DecodeBytes<T: Collection>(_ bytes: T, options: Base64DecodingOptions = []) -> [UInt8]? where T.Element == UInt8 {

Then it could be called using:

NSData.base64DecodeBytes(base64String.utf8, ...)

and

NSData.base64DecodeBytes(base64Data, ...)

This has a 2nd benefit of avoiding the use of .utf8CString which may have to reallocate if it needs more space since it appends a NUL to the end of the buffer. This also eliminates the use of withUnsafeBytes in the 2nd initialiser.

What do you think?

@spevans
Copy link
Contributor

spevans commented Apr 24, 2020

@swift-ci test linux

4 similar comments
@spevans
Copy link
Contributor

spevans commented Apr 24, 2020

@swift-ci test linux

@spevans
Copy link
Contributor

spevans commented Apr 24, 2020

@swift-ci test linux

@spevans
Copy link
Contributor

spevans commented Apr 24, 2020

@swift-ci test linux

@spevans
Copy link
Contributor

spevans commented May 6, 2020

@swift-ci test linux

@valeriyvan valeriyvan requested a review from spevans May 14, 2020 19:30
@spevans
Copy link
Contributor

spevans commented May 14, 2020

This is fine although could you just squash it down into a single commit please. I do have some decoding optimisations to add but I can rebase them on top of these ones.

@valeriyvan valeriyvan force-pushed the RedundantZeroingNSData branch from 0d55e8d to b070a51 Compare May 14, 2020 20:06
@valeriyvan
Copy link
Contributor Author

Squashed

@spevans
Copy link
Contributor

spevans commented May 14, 2020

@swift-ci please test

1 similar comment
@spevans
Copy link
Contributor

spevans commented May 14, 2020

@swift-ci please test

@spevans
Copy link
Contributor

spevans commented May 14, 2020

@swift-ci test and merge

@spevans
Copy link
Contributor

spevans commented May 14, 2020

@swift-ci please test and merge

@swift-ci swift-ci merged commit 4a8a22c into swiftlang:master May 15, 2020
@valeriyvan valeriyvan deleted the RedundantZeroingNSData branch May 16, 2020 21:04
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.

3 participants