Skip to content

Commit 924aaec

Browse files
committed
Use CryptoKitSHA256 if available
While benchmarking registry performance, I was surprised that RegistryManager.downloadSourceArchive was taking so long. After some further investigation, I found that >90% of the time was spent calculating the SHA256 checksum. This change improves my local end-to-end clean registry build from ~70 seconds to ~15 seconds.
1 parent 5e91c76 commit 924aaec

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Sources/PackageRegistry/RegistryManager.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public final class RegistryManager {
219219
do {
220220
let contents = ByteString(data)
221221
let advertisedChecksum = digest.spm_dropPrefix("sha-256=")
222-
let actualChecksum = SHA256().hash(contents).hexadecimalRepresentation
222+
let actualChecksum = contents.sha256Checksum.hexadecimalRepresentation
223223

224224
guard (expectedChecksum?.hexadecimalRepresentation ?? actualChecksum) == actualChecksum,
225225
advertisedChecksum == actualChecksum
@@ -298,3 +298,15 @@ private extension AbsolutePath {
298298
return AbsolutePath(self, RelativePath("..")).appending(component: "\(basename).\(`extension`)")
299299
}
300300
}
301+
302+
private extension ByteString {
303+
var sha256Checksum: ByteString {
304+
#if canImport(CryptoKit)
305+
if #available(macOS 10.15, *) {
306+
return CryptoKitSHA256().hash(self)
307+
}
308+
#endif
309+
310+
return SHA256().hash(self)
311+
}
312+
}

0 commit comments

Comments
 (0)