Skip to content

Import Data and DataProtocol from the overlay (and correct some incorrect CFData and NSData usages) #1834

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 2 commits into from
Jan 24, 2019

Conversation

phausler
Copy link
Contributor

This brings in the new 2-word implementation of Data which adds numerous performance improvements that reduce the overhead of using Data. Particularly certain optimized scenarios will instead of using heap allocations for Data it now uses a stack switching implementation (similar to the smol String implementation). Additionally this brings in a new protocol called DataProtocol which unifies Data like types to be usable in a generic fashion. This codifies a concept of contiguous byte storage so that types such as DispatchData which represent a collection of contiguous regions can share a similar interface to those with full contiguous storage like Data or Array etc.

@phausler
Copy link
Contributor Author

@swift-ci please test

@phausler phausler requested a review from millenomi January 18, 2019 21:28
@@ -130,15 +130,19 @@ add_swift_library(Foundation
Foundation/CGFloat.swift
Foundation/CharacterSet.swift
Foundation/Codable.swift
Foundation/Collections+DataProtocol.swift
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I sorted this section: hopefully that does not pose any issue.

Copy link
Member

Choose a reason for hiding this comment

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

If it does, it will be immediately apparent. The places where things were out of order were due to the compiler requiring the interfaces to be processed earlier.

@@ -377,7 +374,6 @@ static Boolean __CFDataShouldUseAllocator(CFAllocatorRef allocator) {
// that there should be no deallocator, and the bytes should be copied.
static CFMutableDataRef __CFDataInit(CFAllocatorRef allocator, _CFDataMutableVariety variety, CFIndex capacity, const uint8_t *bytes, CFIndex length, CFAllocatorRef bytesDeallocator) CF_RETURNS_RETAINED {
CFMutableDataRef memory;
__CFGenericValidateMutability(variety);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is inappropriate and does not match the Darwin behavior.

Copy link
Contributor

Choose a reason for hiding this comment

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

It must've been a merge artifact.

@@ -31,7 +31,11 @@ let kNullString = "<null>"
private func getTestData() -> [Any]? {
let testFilePath = testBundle().url(forResource: "NSURLTestData", withExtension: "plist")
let data = try! Data(contentsOf: testFilePath!)
guard let testRoot = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String : Any] else {
guard let plist = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) else {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is an error in the latest toolchain

Copy link
Member

Choose a reason for hiding this comment

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

Oh I'm sorry that #1829 conflicts with this 😓

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Eh no worries: I was just trying to ensure a good state by testing on macOS. As long as the test is validly fixed it does not matter if it is from your commit or mine imo

@@ -1421,10 +1421,6 @@ extension TestNSData {
}

func test_dataHash() {
let dataStruct = "Hello World".data(using: .utf8)!
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this test is no longer valid

@@ -23,9 +23,7 @@ import Dispatch
/// Turn `Data` into `DispatchData`
internal func createDispatchData(_ data: Data) -> DispatchData {
//TODO: Avoid copying data
let buffer = UnsafeRawBufferPointer(start: data._backing.bytes,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was super-unsafe!

@@ -179,7 +179,9 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
/// Initializes a data object with the contents of another data object.
public init(data: Data) {
super.init()
_init(bytes: UnsafeMutableRawPointer(mutating: data._nsObject.bytes), length: data.count, copy: true)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not quite sure how this didnt crash before. The new version is considerably safer

@phausler phausler assigned parkera and unassigned parkera Jan 18, 2019
@phausler
Copy link
Contributor Author

@swift-ci please test

1 similar comment
@phausler
Copy link
Contributor Author

@swift-ci please test

@phausler phausler force-pushed the data_and_dataprotocol branch from 0ab7132 to 82f3236 Compare January 23, 2019 18:44
@phausler
Copy link
Contributor Author

@swift-ci please test

1 similar comment
@phausler
Copy link
Contributor Author

@swift-ci please test

@weissi
Copy link
Contributor

weissi commented Jan 25, 2019

@phausler will this also go to swift-5.0-branch?

@millenomi
Copy link
Contributor

@weissi Yes. I'm working on the cherry-pick.

@weissi
Copy link
Contributor

weissi commented Jan 26, 2019

Thanks very much @millenomi

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.

6 participants