Skip to content

Commit b724a65

Browse files
committed
[stdlib] refactor lazy-eager-bridging for reusability
1 parent a8d4081 commit b724a65

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

stdlib/public/core/ArrayBuffer.swift

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -608,11 +608,10 @@ extension _ArrayBuffer {
608608
1 //OBJC_ASSOCIATION_RETAIN_NONATOMIC
609609
)
610610
}
611-
612-
@_alwaysEmitIntoClient @inline(never)
613-
internal func withUnsafeBufferPointer_nonNative<R, E>(
614-
_ body: (UnsafeBufferPointer<Element>) throws(E) -> R
615-
) throws(E) -> R {
611+
612+
@_alwaysEmitIntoClient
613+
internal func getOrAllocateAssociatedObjectBuffer(
614+
) -> _ContiguousArrayBuffer<Element> {
616615
let unwrapped: _ContiguousArrayBuffer<Element>
617616
// libobjc already provides the necessary memory barriers for
618617
// double checked locking to be safe, per comments on
@@ -633,12 +632,16 @@ extension _ArrayBuffer {
633632
defer { _fixLifetime(unwrapped) }
634633
objc_sync_exit(lock)
635634
}
636-
return try unsafe body(
637-
UnsafeBufferPointer(
638-
start: unwrapped.firstElementAddress,
639-
count: unwrapped.count
640-
)
641-
)
635+
return unwrapped
636+
}
637+
638+
@_alwaysEmitIntoClient @inline(never)
639+
internal func withUnsafeBufferPointer_nonNative<R, E>(
640+
_ body: (UnsafeBufferPointer<Element>) throws(E) -> R
641+
) throws(E) -> R {
642+
let buffer = getOrAllocateAssociatedObjectBuffer()
643+
let (pointer, count) = (buffer.firstElementAddress, buffer.count)
644+
return try unsafe body(UnsafeBufferPointer(start: pointer, count: count))
642645
}
643646

644647
/// Call `body(p)`, where `p` is an `UnsafeBufferPointer` over the

0 commit comments

Comments
 (0)