Use WriteString in hashProviderInternal

maphash.Hash implements WriteString, which avoids an allocation in
order to convert the string to a byte slice.  Using the concrete
type instead of the io.Writer interface also allows int64Array to
be allocated on the stack.

Test: SOONG_PROFILE_MEM=/tmp/mem.pprof m nothing
Change-Id: I5894f7399c2a232f5f67d7d0724a6115ba2c278f
This commit is contained in:
Colin Cross 2024-02-01 12:34:01 -08:00
parent dbf18bec98
commit 2ef2c35664

View file

@ -19,7 +19,6 @@ import (
"encoding/binary"
"fmt"
"hash/maphash"
"io"
"math"
"reflect"
"sort"
@ -44,7 +43,7 @@ func HashProvider(provider interface{}) (uint64, error) {
return hasher.Sum64(), err
}
func hashProviderInternal(hasher io.Writer, v reflect.Value, ptrs map[uintptr]bool) error {
func hashProviderInternal(hasher *maphash.Hash, v reflect.Value, ptrs map[uintptr]bool) error {
var int64Array [8]byte
int64Buf := int64Array[:]
binary.LittleEndian.PutUint64(int64Buf, uint64(v.Kind()))
@ -129,7 +128,7 @@ func hashProviderInternal(hasher io.Writer, v reflect.Value, ptrs map[uintptr]bo
}
}
case reflect.String:
hasher.Write([]byte(v.String()))
hasher.WriteString(v.String())
case reflect.Bool:
if v.Bool() {
int64Buf[0] = 1