Skip to content

Commit b6125a3

Browse files
committed
Move variable into closure
1 parent 8266868 commit b6125a3

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

stdlib/public/core/KeyPath.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,6 @@ public class KeyPath<Root, Value>: PartialKeyPath<Root> {
326326
}
327327
}
328328

329-
var currentType = rootType
330-
331329
return withBuffer {
332330
var buffer = $0
333331

@@ -366,6 +364,8 @@ public class KeyPath<Root, Value>: PartialKeyPath<Root> {
366364
$0.initializeElement(at: 0, to: root)
367365
}
368366

367+
var currentType = rootType
368+
369369
while true {
370370
let (rawComponent, optNextType) = buffer.next()
371371
let newType = optNextType ?? valueType

stdlib/public/core/TemporaryAllocation.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ internal func _isStackAllocationSafe(byteCount: Int, alignment: Int) -> Bool {
8484
// without worrying about running out of space, and the compiler would emit
8585
// such allocations on the stack anyway when they represent structures or
8686
// stack-promoted objects.
87-
if byteCount <= 1024 {
87+
if _fastPath(byteCount <= 1024) {
8888
return true
8989
}
9090

@@ -174,7 +174,8 @@ internal func _withUnprotectedUnsafeTemporaryAllocation<T, R>(
174174
_ body: (Builtin.RawPointer) throws -> R
175175
) rethrows -> R {
176176
// How many bytes do we need to allocate?
177-
let byteCount = _byteCountForTemporaryAllocation(of: type, capacity: capacity)
177+
//let byteCount = _byteCountForTemporaryAllocation(of: type, capacity: capacity)
178+
let byteCount = MemoryLayout<T>.stride &* capacity
178179

179180
guard _isStackAllocationSafe(byteCount: byteCount, alignment: alignment) else {
180181
return try _fallBackToHeapAllocation(byteCount: byteCount, alignment: alignment, body)
@@ -298,7 +299,7 @@ public func _withUnprotectedUnsafeTemporaryAllocation<R>(
298299
alignment: alignment
299300
) { pointer in
300301
let buffer = UnsafeMutableRawBufferPointer(
301-
start: .init(pointer),
302+
_uncheckedStart: .init(pointer),
302303
count: byteCount
303304
)
304305
return try body(buffer)
@@ -374,7 +375,7 @@ public func _withUnprotectedUnsafeTemporaryAllocation<T, R>(
374375
) { pointer in
375376
Builtin.bindMemory(pointer, capacity._builtinWordValue, type)
376377
let buffer = UnsafeMutableBufferPointer<T>(
377-
start: .init(pointer),
378+
_uncheckedStart: .init(pointer),
378379
count: capacity
379380
)
380381
return try body(buffer)

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ public struct Unsafe${Mutable}RawBufferPointer {
115115
_debugPrecondition(count >= 0, "${Self} with negative count")
116116
_debugPrecondition(count == 0 || start != nil,
117117
"${Self} has a nil start and nonzero count")
118+
119+
self.init(_uncheckedStart: start, count: count)
120+
}
121+
122+
@_alwaysEmitIntoClient
123+
internal init(
124+
_uncheckedStart start: Unsafe${Mutable}RawPointer?,
125+
count: Int
126+
) {
127+
_internalInvariant(count >= 0, "${Self} with negative count")
128+
_internalInvariant(
129+
count == 0 || start != nil,
130+
"${Self} has a nil start and nonzero count"
131+
)
132+
118133
_position = start
119134
_end = start.map { $0 + _assumeNonNegative(count) }
120135
}
@@ -1264,7 +1279,12 @@ public func _withUnprotectedUnsafeBytes<T, Result>(
12641279
) rethrows -> Result
12651280
{
12661281
return try _withUnprotectedUnsafePointer(to: &value) {
1267-
try body(UnsafeRawBufferPointer(start: $0, count: MemoryLayout<T>.size))
1282+
try body(
1283+
UnsafeRawBufferPointer(
1284+
_uncheckedStart: $0,
1285+
count: MemoryLayout<T>.size
1286+
)
1287+
)
12681288
}
12691289
}
12701290

@@ -1312,7 +1332,10 @@ public func _withUnprotectedUnsafeBytes<T, Result>(
13121332
#else
13131333
let addr = UnsafeRawPointer(Builtin.addressOfBorrow(value))
13141334
#endif
1315-
let buffer = UnsafeRawBufferPointer(start: addr, count: MemoryLayout<T>.size)
1335+
let buffer = UnsafeRawBufferPointer(
1336+
_uncheckedStart: addr,
1337+
count: MemoryLayout<T>.size
1338+
)
13161339
return try body(buffer)
13171340
}
13181341

0 commit comments

Comments
 (0)