Skip to content

Commit 1e99798

Browse files
committed
Fetch without chunking in parallel
1 parent 8a92500 commit 1e99798

1 file changed

Lines changed: 33 additions & 21 deletions

File tree

Sources/Containerization/Image/ImageStore/ImageStore+Import.swift

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -120,31 +120,43 @@ extension ImageStore {
120120

121121
private func fetch(_ inDesc: [Descriptor]) async throws {
122122
try await withThrowingTaskGroup(of: Void.self) { group in
123-
for chunk in inDesc.chunks(ofCount: 8) {
124-
for desc in chunk {
125-
if let found = try await self.contentStore.get(digest: desc.digest) {
126-
try FileManager.default.copyItem(at: found.path, to: ingestDir.appendingPathComponent(desc.digest.trimmingDigestPrefix))
127-
await progress?([
128-
// Count the size of the blob
129-
ProgressEvent(event: "add-size", value: desc.size),
130-
// Count the number of blobs
131-
ProgressEvent(event: "add-items", value: 1),
132-
])
133-
continue
123+
func _fetch(_ desc: Descriptor) async throws {
124+
if let found = try await self.contentStore.get(digest: desc.digest) {
125+
try FileManager.default.copyItem(at: found.path, to: ingestDir.appendingPathComponent(desc.digest.trimmingDigestPrefix))
126+
await progress?([
127+
// Count the size of the blob
128+
ProgressEvent(event: "add-size", value: desc.size),
129+
// Count the number of blobs
130+
ProgressEvent(event: "add-items", value: 1),
131+
])
132+
return
133+
}
134+
135+
if desc.size > 1.mib() {
136+
try await self.fetchBlob(desc)
137+
} else {
138+
try await self.fetchData(desc)
139+
}
140+
// Count the number of blobs
141+
await progress?([
142+
ProgressEvent(event: "add-items", value: 1)
143+
])
144+
}
145+
146+
var iterator = inDesc.makeIterator()
147+
for _ in 0..<8 {
148+
if let desc = iterator.next() {
149+
group.addTask {
150+
try await _fetch(desc)
134151
}
152+
}
153+
}
154+
for try await _ in group {
155+
if let desc = iterator.next() {
135156
group.addTask {
136-
if desc.size > 1.mib() {
137-
try await self.fetchBlob(desc)
138-
} else {
139-
try await self.fetchData(desc)
140-
}
141-
// Count the number of blobs
142-
await progress?([
143-
ProgressEvent(event: "add-items", value: 1)
144-
])
157+
try await _fetch(desc)
145158
}
146159
}
147-
try await group.waitForAll()
148160
}
149161
}
150162
}

0 commit comments

Comments
 (0)