Skip to content

Commit e9fd540

Browse files
Merge pull request #37 from NeedleInAJayStack/chore/restore-async-collections
Restore AsyncCollections dependency
2 parents 6e61423 + 77a5166 commit e9fd540

4 files changed

Lines changed: 24 additions & 16 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: 2 additions & 13 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

0 commit comments

Comments
 (0)