Skip to content

Commit ea40837

Browse files
committed
[stdlib] internal UnsafeRawBufferPointer tweaks
1 parent ba1446e commit ea40837

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

stdlib/public/core/Span/OutputRawSpan.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,15 @@ extension OutputRawSpan {
340340
) throws(E) -> R
341341
) throws(E) -> R {
342342
guard let start = unsafe _pointer, capacity > 0 else {
343-
let buffer = unsafe UnsafeMutableRawBufferPointer(start: nil, count: 0)
343+
let buffer = UnsafeMutableRawBufferPointer(_empty: ())
344344
var initializedCount = 0
345345
defer {
346346
_precondition(initializedCount == 0, "OutputRawSpan capacity overflow")
347347
}
348348
return unsafe try body(buffer, &initializedCount)
349349
}
350350
let buffer = unsafe UnsafeMutableRawBufferPointer(
351-
start: start, count: capacity
351+
_uncheckedStart: start, count: capacity
352352
)
353353
var initializedCount = _count
354354
defer {

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ public struct Unsafe${Mutable}RawBufferPointer {
100100
@usableFromInline
101101
internal let _position, _end: Unsafe${Mutable}RawPointer?
102102

103+
// This works around _debugPrecondition() impacting the performance of
104+
// optimized code. (rdar://72246338)
105+
@_alwaysEmitIntoClient
106+
internal init(
107+
@_nonEphemeral _uncheckedStart start: Unsafe${Mutable}RawPointer?,
108+
count: Int
109+
) {
110+
unsafe _position = start
111+
unsafe _end = start.map { unsafe $0 + _assumeNonNegative(count) }
112+
}
113+
103114
/// Creates a buffer over the specified number of contiguous bytes starting
104115
/// at the given pointer.
105116
///
@@ -116,9 +127,14 @@ public struct Unsafe${Mutable}RawBufferPointer {
116127
_debugPrecondition(count >= 0, "${Self} with negative count")
117128
_debugPrecondition(unsafe count == 0 || start != nil,
118129
"${Self} has a nil start and nonzero count")
130+
unsafe self.init(_uncheckedStart: start, count: count)
131+
}
119132

120-
unsafe _position = start
121-
unsafe _end = start.map { unsafe $0 + _assumeNonNegative(count) }
133+
@safe
134+
@_alwaysEmitIntoClient
135+
public init(_empty: ()) {
136+
unsafe _position = nil
137+
unsafe _end = nil
122138
}
123139
}
124140

0 commit comments

Comments
 (0)