Skip to content

Commit 66a925c

Browse files
authored
Merge pull request #23793 from phausler/data_mapped_perf_bug
2 parents 1dc8bb3 + 84ba611 commit 66a925c

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

Darwin/Foundation-swiftoverlay-Tests/TestData.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3807,6 +3807,34 @@ class TestData : TestDataSuper {
38073807
let range = slice.range(of: "a".data(using: .ascii)!)
38083808
expectEqual(range, Range<Data.Index>(4..<5))
38093809
}
3810+
3811+
func test_nsdataSequence() {
3812+
let bytes: [UInt8] = Array(0x00...0xFF)
3813+
let data = bytes.withUnsafeBytes { NSData(bytes: $0.baseAddress, length: $0.count) }
3814+
3815+
for byte in bytes {
3816+
expectEqual(data[Int(byte)], byte)
3817+
}
3818+
}
3819+
3820+
func test_dispatchSequence() {
3821+
let bytes1: [UInt8] = Array(0x00..<0xF0)
3822+
let bytes2: [UInt8] = Array(0xF0..<0xFF)
3823+
var data = DispatchData.empty
3824+
bytes1.withUnsafeBytes {
3825+
data.append($0)
3826+
}
3827+
bytes2.withUnsafeBytes {
3828+
data.append($0)
3829+
}
3830+
3831+
for byte in bytes1 {
3832+
expectEqual(data[Int(byte)], byte)
3833+
}
3834+
for byte in bytes2 {
3835+
expectEqual(data[Int(byte)], byte)
3836+
}
3837+
}
38103838
}
38113839

38123840
#if !FOUNDATION_XCTEST
@@ -4126,6 +4154,9 @@ DataTests.test("test_validateMutation_slice_customBacking_withUnsafeMutableBytes
41264154
DataTests.test("test_validateMutation_slice_customMutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound") { TestData().test_validateMutation_slice_customMutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound() }
41274155
DataTests.test("test_byte_access_of_discontiguousData") { TestData().test_byte_access_of_discontiguousData() }
41284156
DataTests.test("test_rangeOfSlice") { TestData().test_rangeOfSlice() }
4157+
DataTests.test("test_nsdataSequence") { TestData().test_nsdataSequence() }
4158+
DataTests.test("test_dispatchSequence") { TestData().test_dispatchSequence() }
4159+
41294160

41304161
// XCTest does not have a crash detection, whereas lit does
41314162
DataTests.test("bounding failure subdata") {

Darwin/Foundation-swiftoverlay/Data.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,7 +1989,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
19891989
@inlinable // This is @inlinable as a convenience initializer.
19901990
public init(contentsOf url: __shared URL, options: Data.ReadingOptions = []) throws {
19911991
let d = try NSData(contentsOf: url, options: ReadingOptions(rawValue: options.rawValue))
1992-
self.init(bytes: d.bytes, count: d.length)
1992+
self.init(referencing: d)
19931993
}
19941994

19951995
/// Initialize a `Data` from a Base-64 encoded String using the given options.
@@ -2000,7 +2000,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
20002000
@inlinable // This is @inlinable as a convenience initializer.
20012001
public init?(base64Encoded base64String: __shared String, options: Data.Base64DecodingOptions = []) {
20022002
if let d = NSData(base64Encoded: base64String, options: Base64DecodingOptions(rawValue: options.rawValue)) {
2003-
self.init(bytes: d.bytes, count: d.length)
2003+
self.init(referencing: d)
20042004
} else {
20052005
return nil
20062006
}
@@ -2015,7 +2015,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
20152015
@inlinable // This is @inlinable as a convenience initializer.
20162016
public init?(base64Encoded base64Data: __shared Data, options: Data.Base64DecodingOptions = []) {
20172017
if let d = NSData(base64Encoded: base64Data, options: Base64DecodingOptions(rawValue: options.rawValue)) {
2018-
self.init(bytes: d.bytes, count: d.length)
2018+
self.init(referencing: d)
20192019
} else {
20202020
return nil
20212021
}

Darwin/Foundation-swiftoverlay/NSData+DataProtocol.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ extension NSData : DataProtocol {
4343
@nonobjc
4444
public subscript(position: Int) -> UInt8 {
4545
var byte = UInt8(0)
46+
var offset = position
4647
enumerateBytes { (ptr, range, stop) in
47-
if range.location <= position && position < range.upperBound {
48-
byte = ptr.load(fromByteOffset: range.location - position, as: UInt8.self)
48+
offset -= range.lowerBound
49+
if range.contains(position) {
50+
byte = ptr.load(fromByteOffset: offset, as: UInt8.self)
4951
stop.pointee = true
5052
}
5153
}

0 commit comments

Comments
 (0)