Skip to content

Commit 1d9ecb3

Browse files
Revert "chore: Remove AsyncCollections dependency"
This reverts commit 12b481e.
1 parent 6e61423 commit 1d9ecb3

4 files changed

Lines changed: 25 additions & 17 deletions

File tree

Benchmarks/Package.resolved

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.resolved

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ let package = Package(
1212
],
1313
dependencies: [
1414
.package(url: "https://github.com/apple/swift-algorithms.git", from: "1.0.0"),
15+
.package(url: "https://github.com/adam-fowler/async-collections", from: "0.0.1"),
1516
.package(url: "https://github.com/apple/swift-nio.git", from: "2.84.0"),
1617
],
1718
targets: [
@@ -26,6 +27,7 @@ let package = Package(
2627
name: "AsyncDataLoader",
2728
dependencies: [
2829
.product(name: "Algorithms", package: "swift-algorithms"),
30+
.product(name: "AsyncCollections", package: "async-collections"),
2931
]
3032
),
3133
.testTarget(

Sources/AsyncDataLoader/DataLoader.swift

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Algorithms
2+
import AsyncCollections
23

34
public enum DataLoaderValue<T: Sendable>: Sendable {
45
case success(T)
@@ -116,19 +117,7 @@ public actor DataLoader<Key: Hashable & Sendable, Value: Sendable> {
116117
return []
117118
}
118119

119-
// This buffer pointer allows us to initialize a pre-sized non-nullable list
120-
// that we can populate by index as `load` results stream in asyncronously
121-
let buffer = UnsafeMutableBufferPointer<Value>.allocate(capacity: keys.count)
122-
try await withThrowingTaskGroup { group in
123-
for (index, element) in keys.enumerated() {
124-
group.addTask {
125-
let result = try await self.load(key: element)
126-
buffer[index] = result
127-
}
128-
}
129-
try await group.waitForAll()
130-
}
131-
return Array(buffer)
120+
return try await keys.concurrentMap { try await self.load(key: $0) }
132121
}
133122

134123
/// Clears the value at `key` from the cache, if it exists. Returns itself for
@@ -184,7 +173,7 @@ public actor DataLoader<Key: Hashable & Sendable, Value: Sendable> {
184173
// If a maxBatchSize was provided and the queue is longer, then segment the
185174
// queue into multiple batches, otherwise treat the queue as a single batch.
186175
if let maxBatchSize = options.maxBatchSize, maxBatchSize > 0, maxBatchSize < batch.count {
187-
for slicedBatch in batch.chunks(ofCount: maxBatchSize) {
176+
try await batch.chunks(ofCount: maxBatchSize).asyncForEach { slicedBatch in
188177
try await self.executeBatch(batch: Array(slicedBatch))
189178
}
190179
} else {

0 commit comments

Comments
 (0)