Skip to content

Commit 8078197

Browse files
chore: Remove AsyncCollections dependency
1 parent 7cbbb33 commit 8078197

3 files changed

Lines changed: 15 additions & 15 deletions

File tree

Package.resolved

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

Package.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ 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"),
1615
.package(url: "https://github.com/apple/swift-nio.git", from: "2.84.0"),
1716
],
1817
targets: [
@@ -27,7 +26,6 @@ let package = Package(
2726
name: "AsyncDataLoader",
2827
dependencies: [
2928
.product(name: "Algorithms", package: "swift-algorithms"),
30-
.product(name: "AsyncCollections", package: "async-collections"),
3129
]
3230
),
3331
.testTarget(

Sources/AsyncDataLoader/DataLoader.swift

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

43
public enum DataLoaderValue<T: Sendable>: Sendable {
54
case success(T)
@@ -115,7 +114,19 @@ public actor DataLoader<Key: Hashable & Sendable, Value: Sendable> {
115114
return []
116115
}
117116

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

121132
/// Clears the value at `key` from the cache, if it exists. Returns itself for
@@ -175,7 +186,7 @@ public actor DataLoader<Key: Hashable & Sendable, Value: Sendable> {
175186
// If a maxBatchSize was provided and the queue is longer, then segment the
176187
// queue into multiple batches, otherwise treat the queue as a single batch.
177188
if let maxBatchSize = options.maxBatchSize, maxBatchSize > 0, maxBatchSize < batch.count {
178-
try await batch.chunks(ofCount: maxBatchSize).asyncForEach { slicedBatch in
189+
for slicedBatch in batch.chunks(ofCount: maxBatchSize) {
179190
try await self.executeBatch(batch: Array(slicedBatch))
180191
}
181192
} else {

0 commit comments

Comments
 (0)