Skip to content

Commit

Permalink
fix: Use an autorelease pool on macOS (#9)
Browse files Browse the repository at this point in the history
Unfotunately the memory models on macOS and Linux differ significantly
and an autorelease pool—unavailable on Linux—is required on macOS to
correctly manage memory usage.
  • Loading branch information
jbmorley authored Feb 12, 2025
1 parent bd8aaf4 commit 4733189
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
14 changes: 8 additions & 6 deletions Sources/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ func checksum(url: URL, bufferSize: Int = 4 * 1024 * 1024) throws -> Data {
}

var md5 = Crypto.Insecure.MD5()
while true {
let data = file.readData(ofLength: bufferSize)
guard data.count > 0 else {
break
}
md5.update(data: data)
while autoreleasepool(invoking: {
let data = file.readData(ofLength: bufferSize)
guard data.count > 0 else {
return false
}
md5.update(data: data)
return true
}) {
}

return Data(md5.finalize())
Expand Down
9 changes: 9 additions & 0 deletions Sources/Extensions/NSAutoreleasePool.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation

#if os(Linux)

@inlinable public func autoreleasepool<Result>(invoking body: () throws -> Result) rethrows -> Result {
return try body()
}

#endif

0 comments on commit 4733189

Please sign in to comment.