Skip to content

Commit 56f509f

Browse files
committed
[Foundation] Merge sequence initializer fast paths into one initializer
1 parent 0957ca6 commit 56f509f

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

stdlib/public/SDK/Foundation/Data.swift

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,35 +1085,26 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
10851085
// slightly faster paths for common sequences
10861086

10871087
public init<S: Sequence>(_ elements: S) where S.Iterator.Element == UInt8 {
1088-
let underestimatedCount = elements.underestimatedCount
1089-
self.init(count: underestimatedCount)
1090-
1091-
let (endIterator, _) = UnsafeMutableBufferPointer(start: _backing._bytes?.assumingMemoryBound(to: UInt8.self), count: underestimatedCount).initialize(from: elements)
1092-
var iter = endIterator
1093-
while let byte = iter.next() { self.append(byte) }
1094-
}
1095-
1096-
public init(_ bytes: Array<UInt8>) {
1097-
self.init(bytes: bytes)
1098-
}
1099-
1100-
public init(_ bytes: ArraySlice<UInt8>) {
1101-
self.init(bytes: bytes)
1102-
}
1103-
1104-
public init(_ buffer: UnsafeBufferPointer<UInt8>) {
1105-
self.init(buffer: buffer)
1106-
}
1107-
1108-
public init(_ buffer: UnsafeMutableBufferPointer<UInt8>) {
1109-
self.init(buffer: buffer)
1110-
}
1111-
1112-
public init(_ data: Data) {
1113-
_sliceRange = 0..<data.count
1114-
_backing = data._backing.mutableCopy(data._sliceRange)
1088+
if elements is Array<UInt8> {
1089+
self.init(bytes: _identityCast(elements, to: Array<UInt8>.self))
1090+
} else if elements is ArraySlice<UInt8> {
1091+
self.init(bytes: _identityCast(elements, to: ArraySlice<UInt8>.self))
1092+
} else if elements is UnsafeBufferPointer<UInt8> {
1093+
self.init(buffer: _identityCast(elements, to: UnsafeBufferPointer<UInt8>.self))
1094+
} else if let buffer = elements as? UnsafeMutableBufferPointer<UInt8> {
1095+
self.init(buffer: buffer)
1096+
} else if let data = elements as? Data {
1097+
self.init(backing: data._backing.mutableCopy(data._sliceRange), range: 0..<data.count)
1098+
} else {
1099+
let underestimatedCount = elements.underestimatedCount
1100+
self.init(count: underestimatedCount)
1101+
1102+
let (endIterator, _) = UnsafeMutableBufferPointer(start: _backing._bytes?.assumingMemoryBound(to: UInt8.self), count: underestimatedCount).initialize(from: elements)
1103+
var iter = endIterator
1104+
while let byte = iter.next() { self.append(byte) }
1105+
}
11151106
}
1116-
1107+
11171108
@_versioned
11181109
internal init(backing: _DataStorage, range: Range<Index>) {
11191110
_backing = backing

test/stdlib/TestData.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,13 @@ class TestData : TestDataSuper {
11721172
let dataFromSliceOfData = Data(sliceOfData)
11731173
expectEqual(sliceOfData, dataFromSliceOfData)
11741174
}
1175+
1176+
func test_reversedDataInit() {
1177+
let data = Data(bytes: [1, 2, 3, 4, 5, 6, 7, 8, 9])
1178+
let reversedData = Data(data.reversed())
1179+
let expected = Data(bytes: [9, 8, 7, 6, 5, 4, 3, 2, 1])
1180+
expectEqual(expected, reversedData)
1181+
}
11751182
}
11761183

11771184
#if !FOUNDATION_XCTEST
@@ -1235,7 +1242,7 @@ DataTests.test("test_copyBytes2") { TestData().test_copyBytes2() }
12351242
DataTests.test("test_sliceOfSliceViaRangeExpression") { TestData().test_sliceOfSliceViaRangeExpression() }
12361243
DataTests.test("test_appendingSlices") { TestData().test_appendingSlices() }
12371244
DataTests.test("test_sequenceInitializers") { TestData().test_sequenceInitializers() }
1238-
1245+
DataTests.test("test_reversedDataInit") { TestData().test_reversedDataInit() }
12391246

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

0 commit comments

Comments
 (0)