Skip to content

Commit 551acec

Browse files
committed
use lru cache for input data
1 parent ec59149 commit 551acec

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.23.7
55
require (
66
github.com/go-logr/zapr v1.3.0
77
github.com/hashicorp/go-retryablehttp v0.7.7
8+
github.com/hashicorp/golang-lru/v2 v2.0.7
89
go.uber.org/zap v1.27.0
910
golang.org/x/net v0.37.0
1011
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB1
1212
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
1313
github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU=
1414
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=
15+
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
16+
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
1517
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
1618
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
1719
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=

internal/gameServer/udp.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net"
99
"time"
1010

11+
lru "github.com/hashicorp/golang-lru/v2"
1112
"golang.org/x/net/ipv4"
1213
"golang.org/x/net/ipv6"
1314
)
@@ -17,8 +18,8 @@ type GameData struct {
1718
PlayerAddresses []*net.UDPAddr
1819
BufferSize []uint32
1920
BufferHealth []int32
20-
Inputs []map[uint32]uint32
21-
Plugin []map[uint32]byte
21+
Inputs []*lru.Cache[uint32, uint32]
22+
Plugin []*lru.Cache[uint32, byte]
2223
PendingInput []uint32
2324
CountLag []uint32
2425
PendingPlugin []byte
@@ -36,11 +37,11 @@ const (
3637
)
3738

3839
const (
39-
StatusDesync = 1
40-
DisconnectTimeoutS = 30
41-
NoRegID = 255
42-
InputDataMax uint32 = 5000
43-
CS4 = 32
40+
StatusDesync = 1
41+
DisconnectTimeoutS = 30
42+
NoRegID = 255
43+
InputDataMax int = 5000
44+
CS4 = 32
4445
)
4546

4647
// returns true if v is bigger than w (accounting for uint32 wrap around).
@@ -62,12 +63,10 @@ func (g *GameServer) getPlayerNumberByID(regID uint32) (byte, error) {
6263
}
6364

6465
func (g *GameServer) fillInput(playerNumber byte, count uint32) {
65-
_, inputExists := g.GameData.Inputs[playerNumber][count]
66-
delete(g.GameData.Inputs[playerNumber], count-InputDataMax) // no need to keep old input data
67-
delete(g.GameData.Plugin[playerNumber], count-InputDataMax) // no need to keep old input data
66+
_, inputExists := g.GameData.Inputs[playerNumber].Get(count)
6867
if !inputExists {
69-
g.GameData.Inputs[playerNumber][count] = g.GameData.PendingInput[playerNumber]
70-
g.GameData.Plugin[playerNumber][count] = g.GameData.PendingPlugin[playerNumber]
68+
g.GameData.Inputs[playerNumber].Add(count, g.GameData.PendingInput[playerNumber])
69+
g.GameData.Plugin[playerNumber].Add(count, g.GameData.PendingPlugin[playerNumber])
7170
}
7271
}
7372

@@ -93,17 +92,17 @@ func (g *GameServer) sendUDPInput(count uint32, addr *net.UDPAddr, playerNumber
9392
currentByte := 5
9493
start := count
9594
end := start + g.GameData.BufferSize[sendingPlayerNumber]
96-
_, ok := g.GameData.Inputs[playerNumber][count] // check if input exists for this count
95+
inputData, ok := g.GameData.Inputs[playerNumber].Get(count) // check if input exists for this count
9796
for (currentByte < len(buffer)-9) && ((!spectator && countLag == 0 && uintLarger(end, count)) || ok) {
9897
binary.BigEndian.PutUint32(buffer[currentByte:], count)
9998
currentByte += 4
10099
g.fillInput(playerNumber, count)
101-
binary.BigEndian.PutUint32(buffer[currentByte:], g.GameData.Inputs[playerNumber][count])
100+
binary.BigEndian.PutUint32(buffer[currentByte:], inputData)
102101
currentByte += 4
103-
buffer[currentByte] = g.GameData.Plugin[playerNumber][count]
102+
buffer[currentByte], _ = g.GameData.Plugin[playerNumber].Get(count)
104103
currentByte++
105104
count++
106-
_, ok = g.GameData.Inputs[playerNumber][count] // check if input exists for this count
105+
inputData, ok = g.GameData.Inputs[playerNumber].Get(count) // check if input exists for this count
107106
}
108107

109108
if count > start {
@@ -210,13 +209,13 @@ func (g *GameServer) createUDPServer() error {
210209
g.GameData.PlayerAddresses = make([]*net.UDPAddr, 4) //nolint:gomnd,mnd
211210
g.GameData.BufferSize = []uint32{3, 3, 3, 3}
212211
g.GameData.BufferHealth = []int32{-1, -1, -1, -1}
213-
g.GameData.Inputs = make([]map[uint32]uint32, 4) //nolint:gomnd,mnd
212+
g.GameData.Inputs = make([]*lru.Cache[uint32, uint32], 4) //nolint:gomnd,mnd
214213
for i := range 4 {
215-
g.GameData.Inputs[i] = make(map[uint32]uint32)
214+
g.GameData.Inputs[i], _ = lru.New[uint32, uint32](InputDataMax)
216215
}
217-
g.GameData.Plugin = make([]map[uint32]byte, 4) //nolint:gomnd,mnd
216+
g.GameData.Plugin = make([]*lru.Cache[uint32, byte], 4) //nolint:gomnd,mnd
218217
for i := range 4 {
219-
g.GameData.Plugin[i] = make(map[uint32]byte)
218+
g.GameData.Plugin[i], _ = lru.New[uint32, byte](InputDataMax)
220219
}
221220
g.GameData.PendingInput = make([]uint32, 4) //nolint:gomnd,mnd
222221
g.GameData.PendingPlugin = make([]byte, 4) //nolint:gomnd,mnd

0 commit comments

Comments
 (0)