Skip to content

Commit 74625e4

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 006b562 commit 74625e4

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
@@ -212,7 +212,7 @@ public final class RegistryManager {
212212
do {
213213
let contents = ByteString(data)
214214
let advertisedChecksum = digest.spm_dropPrefix("sha-256=")
215-
let actualChecksum = SHA256().hash(contents).hexadecimalRepresentation
215+
let actualChecksum = contents.sha256Checksum.hexadecimalRepresentation
216216

217217
guard (expectedChecksum?.hexadecimalRepresentation ?? actualChecksum) == actualChecksum,
218218
advertisedChecksum == actualChecksum
@@ -291,3 +291,15 @@ private extension AbsolutePath {
291291
return AbsolutePath(self, RelativePath("..")).appending(component: "\(basename).\(`extension`)")
292292
}
293293
}
294+
295+
private extension ByteString {
296+
var sha256Checksum: ByteString {
297+
#if canImport(CryptoKit)
298+
if #available(macOS 10.15, *) {
299+
return CryptoKitSHA256().hash(self)
300+
}
301+
#endif
302+
303+
return SHA256().hash(self)
304+
}
305+
}

0 commit comments

Comments
 (0)