From d8b69de52d8878f5090ea409eaade8b8cf85a80d Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Wed, 1 Nov 2023 15:00:48 +0400 Subject: [PATCH] Fetcher.fetchViaFile(): use an mmap(2)-ed file, similarly to DiskV1 (#641) * Fetcher.fetchViaFile(): use an mmap(2)-ed file, similarly to DiskV1 * No need to convert Data to Data --- Sources/tart/Fetcher.swift | 6 +++--- Sources/tart/OCI/Registry.swift | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/tart/Fetcher.swift b/Sources/tart/Fetcher.swift index f2d3391..461ad59 100644 --- a/Sources/tart/Fetcher.swift +++ b/Sources/tart/Fetcher.swift @@ -35,12 +35,12 @@ class Fetcher { // // This keeps a working reference to that file, yet we don't // have to deal with the cleanup any more. - let fh = try FileHandle(forReadingFrom: fileURL) + let mappedFile = try Data(contentsOf: fileURL, options: [.alwaysMapped]) try FileManager.default.removeItem(at: fileURL) Task { - while let data = try fh.read(upToCount: 64 * 1024 * 1024) { - await dataCh.send(data) + for chunk in (0 ..< mappedFile.count).chunks(ofCount: 64 * 1024 * 1024) { + await dataCh.send(mappedFile.subdata(in: chunk)) } dataCh.finish() diff --git a/Sources/tart/OCI/Registry.swift b/Sources/tart/OCI/Registry.swift index 2c9b8e4..56539fb 100644 --- a/Sources/tart/OCI/Registry.swift +++ b/Sources/tart/OCI/Registry.swift @@ -253,7 +253,7 @@ class Registry { for try await part in channel { try Task.checkCancellation() - try await handler(Data(part)) + try await handler(part) } }