Skip to content

Commit 6a16a57

Browse files
committed
feat: small improvement on data hash
1 parent 6d63649 commit 6a16a57

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

state.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package goapai
22

33
import (
44
"encoding/binary"
5-
"fmt"
65
"hash/fnv"
76
"slices"
87
"strconv"
@@ -98,19 +97,25 @@ func (statesData statesData) sort() {
9897
func (statesData statesData) hashStates() uint64 {
9998
hash := fnv.New64()
10099

100+
buf := make([]byte, binary.MaxVarintLen64)
101101
for _, data := range statesData {
102-
hash.Write([]byte(strconv.Itoa(int(data.GetKey()))))
102+
n := binary.PutVarint(buf, int64(data.GetKey()))
103+
hash.Write(buf[:n])
103104
hash.Write([]byte(":"))
104105

105106
switch v := data.GetValue().(type) {
106107
case int8:
107-
hash.Write([]byte(fmt.Sprintf("%v", v)))
108+
n = binary.PutVarint(buf, int64(v))
109+
hash.Write(buf[:n])
108110
case int:
109-
hash.Write([]byte(strconv.Itoa(v)))
111+
n = binary.PutVarint(buf, int64(v))
112+
hash.Write(buf[:n])
110113
case uint8:
111-
hash.Write([]byte(fmt.Sprintf("%v", v)))
114+
n = binary.PutUvarint(buf, uint64(v))
115+
hash.Write(buf[:n])
112116
case uint64:
113-
hash.Write([]byte(fmt.Sprintf("%v", v)))
117+
n = binary.PutUvarint(buf, v)
118+
hash.Write(buf[:n])
114119
case float64:
115120
hash.Write([]byte(strconv.FormatFloat(v, 'f', -1, 64)))
116121
case string:

0 commit comments

Comments
 (0)