Skip to content

Commit b3f6cae

Browse files
committed
chore: improve performances of the state hash
1 parent 95d995a commit b3f6cae

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

action.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,11 @@ func (effect Effect[T]) apply(data statesData) error {
8686
}
8787
return fmt.Errorf("data does not exist")
8888
}
89-
s := data[k]
90-
91-
if _, ok := s.(State[T]); !ok {
89+
if _, ok := data[k].(State[T]); !ok {
9290
return fmt.Errorf("type does not match")
9391
}
9492

95-
state := s.(State[T])
93+
state := data[k].(State[T])
9694
switch effect.Operator {
9795
case EFFECT_ARITHMETIC_SET:
9896
state.Value = effect.Value

astar.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ func astar(from states, goal goalInterface, actions Actions, maxDepth int) Plan
2020
availableActions := getImpactingActions(from, actions)
2121
openNodes := make([]node, 0, len(availableActions))
2222

23+
data := slices.Clone(from.data)
24+
data.sort()
2325
openNodes = append(openNodes, node{
2426
Action: &Action{},
2527
states: states{
2628
Agent: from.Agent,
27-
data: slices.Clone(from.data),
29+
data: data,
2830
hash: from.data.hashStates(),
2931
},
3032
parentNode: nil,

state.go

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

33
import (
4+
"encoding/binary"
45
"fmt"
56
"hash/fnv"
67
"slices"
@@ -19,9 +20,9 @@ const (
1920
)
2021

2122
type Numeric interface {
22-
~int | ~int8 | ~int16 | ~int32 | ~int64 |
23-
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
24-
~float32 | ~float64
23+
~int8 | ~int |
24+
~uint8 | ~uint64 |
25+
~float64
2526
}
2627

2728
type StateInterface interface {
@@ -100,7 +101,25 @@ func (statesData statesData) hashStates() uint64 {
100101
for _, data := range statesData {
101102
hash.Write([]byte(strconv.Itoa(int(data.GetKey()))))
102103
hash.Write([]byte(":"))
103-
hash.Write([]byte(fmt.Sprint(data.GetValue())))
104+
105+
switch v := data.GetValue().(type) {
106+
case int8:
107+
hash.Write([]byte(fmt.Sprintf("%v", v)))
108+
case int:
109+
hash.Write([]byte(strconv.Itoa(v)))
110+
case uint8:
111+
hash.Write([]byte(fmt.Sprintf("%v", v)))
112+
case uint64:
113+
hash.Write([]byte(fmt.Sprintf("%v", v)))
114+
case float64:
115+
hash.Write([]byte(strconv.FormatFloat(v, 'f', -1, 64)))
116+
case string:
117+
hash.Write([]byte(v))
118+
case []byte:
119+
hash.Write(v)
120+
default:
121+
binary.Write(hash, binary.LittleEndian, data.GetValue())
122+
}
104123
hash.Write([]byte(";"))
105124
}
106125

0 commit comments

Comments
 (0)