Skip to content

Commit

Permalink
Pre-allocate bytes.Buffer in hashContent()
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorunic committed Apr 9, 2024
1 parent d8d7e7d commit daf2e38
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion db/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,24 @@ func dbExists(filePath string) bool {

// hashContent creates SHA-256 hash from (bucket, subBucket, []target) concatenated strings and returns []byte result.
func hashContent(bucket, subBucket string, target []string) []byte {
// get total length of all strings
totalLen := len(bucket) + len(subBucket)
for i := range target {
totalLen += len(target[i])
}

var sb bytes.Buffer

// pre-allocate buffer
sb.Grow(totalLen)

sb.WriteString(bucket)
sb.WriteString(subBucket)

for i := range target {
sb.WriteString(target[i])
}

// calculate SHA-256 using SIMD AVX512 or SHA Extensions where possible
targetHash256 := sha256.Sum256(sb.Bytes())

return targetHash256[:]
Expand Down

0 comments on commit daf2e38

Please sign in to comment.