Skip to content

Commit ee66afd

Browse files
committed
combine lru caches
1 parent b671c8f commit ee66afd

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

internal/gameServer/udp.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ import (
1313
"golang.org/x/net/ipv6"
1414
)
1515

16+
type InputData struct {
17+
keys uint32
18+
plugin byte
19+
}
20+
1621
type GameData struct {
1722
SyncValues map[uint32][]byte
1823
PlayerAddresses []*net.UDPAddr
1924
BufferSize []uint32
2025
BufferHealth []int32
21-
Inputs []*lru.Cache[uint32, uint32]
22-
Plugin []*lru.Cache[uint32, byte]
26+
Inputs []*lru.Cache[uint32, InputData]
2327
PendingInput []uint32
2428
CountLag []uint32
2529
PendingPlugin []byte
@@ -62,12 +66,11 @@ func (g *GameServer) getPlayerNumberByID(regID uint32) (byte, error) {
6266
return NoRegID, fmt.Errorf("could not find ID")
6367
}
6468

65-
func (g *GameServer) fillInput(playerNumber byte, count uint32) uint32 {
69+
func (g *GameServer) fillInput(playerNumber byte, count uint32) InputData {
6670
input, inputExists := g.GameData.Inputs[playerNumber].Get(count)
6771
if !inputExists {
68-
input = g.GameData.PendingInput[playerNumber]
72+
input = InputData{keys: g.GameData.PendingInput[playerNumber], plugin: g.GameData.PendingPlugin[playerNumber]}
6973
g.GameData.Inputs[playerNumber].Add(count, input)
70-
g.GameData.Plugin[playerNumber].Add(count, g.GameData.PendingPlugin[playerNumber])
7174
}
7275
return input
7376
}
@@ -98,9 +101,10 @@ func (g *GameServer) sendUDPInput(count uint32, addr *net.UDPAddr, playerNumber
98101
for (currentByte < len(buffer)-9) && ((!spectator && countLag == 0 && uintLarger(end, count)) || ok) {
99102
binary.BigEndian.PutUint32(buffer[currentByte:], count)
100103
currentByte += 4
101-
binary.BigEndian.PutUint32(buffer[currentByte:], g.fillInput(playerNumber, count))
104+
input := g.fillInput(playerNumber, count)
105+
binary.BigEndian.PutUint32(buffer[currentByte:], input.keys)
102106
currentByte += 4
103-
buffer[currentByte], _ = g.GameData.Plugin[playerNumber].Get(count)
107+
buffer[currentByte] = input.plugin
104108
currentByte++
105109
count++
106110
_, ok = g.GameData.Inputs[playerNumber].Get(count) // check if input exists for this count
@@ -210,13 +214,9 @@ func (g *GameServer) createUDPServer() error {
210214
g.GameData.PlayerAddresses = make([]*net.UDPAddr, 4) //nolint:gomnd,mnd
211215
g.GameData.BufferSize = []uint32{3, 3, 3, 3}
212216
g.GameData.BufferHealth = []int32{-1, -1, -1, -1}
213-
g.GameData.Inputs = make([]*lru.Cache[uint32, uint32], 4) //nolint:gomnd,mnd
214-
for i := range 4 {
215-
g.GameData.Inputs[i], _ = lru.New[uint32, uint32](InputDataMax)
216-
}
217-
g.GameData.Plugin = make([]*lru.Cache[uint32, byte], 4) //nolint:gomnd,mnd
217+
g.GameData.Inputs = make([]*lru.Cache[uint32, InputData], 4) //nolint:gomnd,mnd
218218
for i := range 4 {
219-
g.GameData.Plugin[i], _ = lru.New[uint32, byte](InputDataMax)
219+
g.GameData.Inputs[i], _ = lru.New[uint32, InputData](InputDataMax)
220220
}
221221
g.GameData.PendingInput = make([]uint32, 4) //nolint:gomnd,mnd
222222
g.GameData.PendingPlugin = make([]byte, 4) //nolint:gomnd,mnd

0 commit comments

Comments
 (0)