# DispatchQueue.concurrentPerform + UnsafeRawPointer in Swift 6

**URL:** <https://forums.swift.org/t/dispatchqueue-concurrentperform-unsaferawpointer-in-swift-6/74125>\
**Category:** Using Swift\
**Tags:** concurrency, gcd, swift6\
**Created:** [August 23, 2024, 8:05am UTC](https://forums.swift.org/t/dispatchqueue-concurrentperform-unsaferawpointer-in-swift-6/74125 "2024-08-23T08:05:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fuzzyhead](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/fuzzyhead/32/28653_2.png) [@fuzzyhead](https://forums.swift.org/u/fuzzyhead)\
**Post date:** [August 23, 2024, 8:05am UTC](https://forums.swift.org/t/dispatchqueue-concurrentperform-unsaferawpointer-in-swift-6/74125/1 "2024-08-23T08:05:13Z")

</div>

We use GCD to speed up the calculation of some raw data.  
Specifically, we use `DispatchQueue.concurrentPerform` to parallelize the initialization of a data buffer. The thread safety is guaranteed by not overlapping accessed buffer regions.

The code looks something like this:

```swift
let someData: UnsafeBufferPointer<simd_float2> = ...
let moreData: UnsafeBufferPointer<simd_float2> = ...

let capacity = 5_000_000

let computedData = [simd_float4](unsafeUninitializedCapacity: capacity) { buffer, initializedCount in
    initializedCount = capacity

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

```

With the new Xcode, the closure passed to `concurrentPerform` is now `Sendable`. This causes some warnings and errors in the code above:

 ![image](https://global.discourse-cdn.com/swift/original/3X/7/8/7821d59f24ac7a25d4ec0941894a504311876a0d.png)

Is there a way to tell the compiler "I know what I'm doing" here? Or is there a new, safe way to operate on raw data in parallel in Swift 6?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![fuzzyhead](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/fuzzyhead/32/28653_2.png) [@fuzzyhead](https://forums.swift.org/u/fuzzyhead)\
**Post date:** [August 23, 2024, 8:27am UTC](https://forums.swift.org/t/dispatchqueue-concurrentperform-unsaferawpointer-in-swift-6/74125/2 "2024-08-23T08:27:13Z")

</div>

It turns out, if we just define the closure before passing it to `concurrentPerform` it works:

```swift
let closure = { (row: Int) in
    let part1 = someData[row]
    let part2 = moreData[row]
    buffer[row] = simd_float4(lowHalf: part1, highHalf: part2)
}

DispatchQueue.concurrentPerform(iterations: capacity, execute: closure)

```

My guess is that `closure` is not `Sendable` here, so the compiler won't complain about the buffer access. And `concurrentPerform` is marked with `@preconcurrency`, so passing a non-sendable closure is also ok. But it feels wrong...

Is there a better pattern for this kind of work?

---

<div class="post-metadata">

**Author:** ![tubescreamer](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tubescreamer/32/3144_2.png) [@tubescreamer](https://forums.swift.org/u/tubescreamer)\
**Post date:** [August 23, 2024, 8:30am UTC](https://forums.swift.org/t/dispatchqueue-concurrentperform-unsaferawpointer-in-swift-6/74125/3 "2024-08-23T08:30:45Z")

</div>

```swift
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)
        
    }

}

```

---

<div class="post-metadata">

**Author:** ![vns](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vns/32/37396_2.png) [@vns](https://forums.swift.org/u/vns)\
**Post date:** [August 23, 2024, 8:58am UTC](https://forums.swift.org/t/dispatchqueue-concurrentperform-unsaferawpointer-in-swift-6/74125/4 "2024-08-23T08:58:25Z")

</div>

> [@fuzzyhead](#):
>
> It turns out, if we just define the closure before passing it to `concurrentPerform` it works:

It doesn't with Xcode 15 beta 6 version of toolchain, you still would be getting an error. The solution with GCD is to opt-out to unsafe as @tubescreamer suggested. Swift would never allow your code since it cannot reason if you accessing memory in a safe manner, and unsafe here to be explicit "compiler won't check that, but I'm sure what I do here is correct".

But you can make it safe with `TaskGroup` to look something like:

```swift
let computedData: [simd_float4] = await withTaskGroup(
    of: (row: Int, element: simd_float4).self
) { group in
    for i in 0..<capacity {
        group.addTask {
            let part1 = someData[row]
            let part2 = moreData[row]
            return (i, simd_float4(lowHalf: part1, highHalf: part2))
        }
    }
    // this can be probably replaced with some buffer as well to not fill unnecessarily
    var result: [simd_float4] = Array(repeating: simd_float4(), count: capacity)
    for await (row, element) in group {
        result[row] = element
    }
    return result
}

```

Here you won't have a potentially dangerous access, and work will still be parallelized.

---

<div class="post-metadata">

**Author:** ![fuzzyhead](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/fuzzyhead/32/28653_2.png) [@fuzzyhead](https://forums.swift.org/u/fuzzyhead)\
**Post date:** [August 23, 2024, 9:47am UTC](https://forums.swift.org/t/dispatchqueue-concurrentperform-unsaferawpointer-in-swift-6/74125/5 "2024-08-23T09:47:21Z")

</div>

Thanks for the suggestion!

Unfortunately, we can't use task groups in this case because we have to do the work in a synchronous context (ROI calculation of a `CIImageProcessorKernel`).

Swift concurrency doesn't seem well-suited for parallelizing work in an otherwise synchronous task, like above. We just want to spawn a few more threads to work on the problem without creating a suspension point in the main task.

---

<div class="post-metadata">

**Author:** ![vns](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vns/32/37396_2.png) [@vns](https://forums.swift.org/u/vns)\
**Post date:** [August 23, 2024, 9:52am UTC](https://forums.swift.org/t/dispatchqueue-concurrentperform-unsaferawpointer-in-swift-6/74125/6 "2024-08-23T09:52:55Z")

</div>

> [@fuzzyhead](#):
>
> Swift concurrency doesn't seem well-suited for parallelizing work in an otherwise synchronous task

~~But you start asynchronous work with `concurrentPerform` anyway, what difference it makes if you wrap group in a `Task` instead? It might be even clearer what is happening in that case, since with `concurrentPerform` it looks like array being initialized right away, while in fact it does not.~~ My bad, it waits.

---

<div class="post-metadata">

**Author:** ![dima\_kozhinov](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dima\_kozhinov](https://forums.swift.org/u/dima_kozhinov)\
**Post date:** [August 23, 2024, 4:35pm UTC](https://forums.swift.org/t/dispatchqueue-concurrentperform-unsaferawpointer-in-swift-6/74125/7 "2024-08-23T16:35:56Z")

</div>

It looks like [this proposal](https://forums.swift.org/t/se-0443-precise-control-flags-over-compiler-warnings/74116) is trying to mitigate this kind of Swift 6 problems.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [August 23, 2024, 5:59pm UTC](https://forums.swift.org/t/dispatchqueue-concurrentperform-unsaferawpointer-in-swift-6/74125/8 "2024-08-23T17:59:25Z")

</div>

> [@fuzzyhead](#):
>
> Swift concurrency doesn't seem well-suited for parallelizing work in an otherwise synchronous task, like above. We just want to spawn a few more threads to work on the problem without creating a suspension point in the main task.

There are many aspects to Swift concurrency. Modelling data isolation in the language to prevent data races is part of it, and as you discovered it will notify about potential races even if you execute your tasks in some other way (assuming the interfaces are correctly annotated).

TaskGroup as it is today is not a replacement for `DispatchQueue.concurrentPerform`, which remains the recommended way to do what you're trying to do. I don't know if we plan to introduce a similar construct in the standard library one day.
