DispatchQueue.concurrentPerform + UnsafeRawPointer in Swift 6

nonisolated(unsafe)
let someData: UnsafeBufferPointer<simd_float2> = ...

nonisolated(unsafe)
let moreData: UnsafeBufferPointer<simd_float2> = ...

let capacity = 5_000_000

struct UnsafeTransfer<Value>: @unchecked Sendable {
    var value: Value
}

let computedData = [simd_float4](
    unsafeUninitializedCapacity: capacity
) { buffer, initializedCount in
    
    initializedCount = capacity
    
    DispatchQueue.concurrentPerform(iterations: capacity) { [buffer = UnsafeTransfer(value: buffer)] row in
        
        // some expensive calculation
        let part1 = someData[row]
        let part2 = moreData[row]
        buffer.value[row] = simd_float4(lowHalf: part1, highHalf: part2)
        
    }

}
2 Likes