Skip to content

Commit 1992cfe

Browse files
authored
Containerization: Reduce allocations for image subsystems (#152)
Continue the allocations journey for anything that is in the codepaths for pulling images. This time there's a couple spots in archive and ext4 we can get rid of some copies.
1 parent 35a821f commit 1992cfe

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

Sources/ContainerizationArchive/Reader.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ extension ArchiveReader: Sequence {
101101

102102
internal func readDataForEntry(_ entry: WriteEntry) -> Data {
103103
let bufferSize = Int(Swift.min(entry.size ?? 4096, 4096))
104-
var data = Data()
104+
var entry = Data()
105+
var part = Data(count: bufferSize)
105106
while true {
106-
var part = Data(count: bufferSize)
107107
let c = part.withUnsafeMutableBytes { buffer in
108108
guard let baseAddress = buffer.baseAddress else {
109109
return 0
@@ -112,9 +112,9 @@ extension ArchiveReader: Sequence {
112112
}
113113
guard c > 0 else { break }
114114
part.count = c
115-
data.append(part)
115+
entry.append(part)
116116
}
117-
return data
117+
return entry
118118
}
119119
}
120120

Sources/ContainerizationEXT4/EXT4Reader+Export.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,14 @@ extension EXT4.EXT4Reader {
130130
entry.fileType = .symbolicLink
131131
if size < 60 {
132132
let linkBytes = EXT4.tupleToArray(inode.block)
133-
entry.symlinkTarget = String(data: Data(linkBytes), encoding: .utf8) ?? ""
133+
entry.symlinkTarget = String(bytes: linkBytes, encoding: .utf8) ?? ""
134134
} else {
135135
if let block = item.blocks {
136136
try self.seek(block: block.start)
137137
guard let linkBytes = try self.handle.read(upToCount: Int(size)) else {
138138
throw EXT4.Error.couldNotReadBlock(block.start)
139139
}
140-
entry.symlinkTarget = String(data: Data(linkBytes), encoding: .utf8) ?? ""
140+
entry.symlinkTarget = String(bytes: linkBytes, encoding: .utf8) ?? ""
141141
}
142142
}
143143
try writer.writeEntry(entry: entry, data: nil)

Sources/ContainerizationOCI/Client/RegistryClient.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,27 @@ public final class RegistryClient: ContentClient {
260260
}
261261
}
262262

263+
internal func requestBuffer(
264+
components: URLComponents,
265+
headers: [(String, String)]? = nil
266+
) async throws -> ByteBuffer {
267+
try await request(components: components, method: .GET, headers: headers) { response in
268+
guard response.status == .ok else {
269+
let url = components.url?.absoluteString ?? "unknown"
270+
let reason = await ErrorResponse.fromResponseBody(response.body)?.jsonString
271+
throw Error.invalidStatus(url: url, response.status, reason: reason)
272+
}
273+
274+
return try await response.body.collect(upTo: self.bufferSize)
275+
}
276+
}
277+
263278
internal func requestJSON<T: Decodable>(
264279
components: URLComponents,
265280
headers: [(String, String)]? = nil
266281
) async throws -> T {
267-
let data = try await self.requestData(components: components, headers: headers)
268-
return try JSONDecoder().decode(T.self, from: data)
282+
let buffer = try await self.requestBuffer(components: components, headers: headers)
283+
return try JSONDecoder().decode(T.self, from: buffer)
269284
}
270285

271286
/// A minimal endpoint, mounted at /v2/ will provide version support information based on its response statuses.

0 commit comments

Comments
 (0)