Stop copying Strings into Arrays for SHA1 (#1413)

Motivation:

Resolve #1409

Modifications:

Using withContiguousStorageIfAvailable if string.utf8 supports an internal representation in a form. Otherwise, fall back to use withUnsafeBufferPointer.

Result:

Save an allocation for each hash function update
This commit is contained in:
trungducc 2020-02-25 16:52:36 +08:00 committed by GitHub
parent dfd56c8b56
commit 8ad2549a49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 0 deletions

View File

@ -46,6 +46,12 @@ internal struct SHA1 {
/// - string: The string that will be UTF-8 encoded and fed into the
/// hash context.
mutating func update(string: String) {
let isAvailable: ()? = string.utf8.withContiguousStorageIfAvailable {
self.update($0)
}
if isAvailable != nil {
return
}
let buffer = Array(string.utf8)
buffer.withUnsafeBufferPointer {
self.update($0)