Skip to content

Commit 2d8b7c7

Browse files
committed
perf: reduce memory usage + increase speed ~10%
1 parent ae3c7a2 commit 2d8b7c7

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

pkg/demoinfocs/sendtables2/entity.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,13 +437,25 @@ func (e *Entity) readFields(r *reader, paths *[]*fieldPath) {
437437
val := decoder(r)
438438

439439
if base && (f.model == fieldModelVariableArray || f.model == fieldModelVariableTable) {
440-
oldFS := e.state.get(fp)
441-
fs := newFieldState()
440+
fs := fieldState{}
442441

443-
fs.state = make([]interface{}, val.(uint64))
442+
oldFS, ok := e.state.get(fp).(*fieldState)
444443

445-
if oldFS != nil {
446-
copy(fs.state, oldFS.(*fieldState).state[:min(len(fs.state), len(oldFS.(*fieldState).state))])
444+
if !ok {
445+
fs.state = make([]any, val.(uint64))
446+
}
447+
448+
if ok {
449+
if uint64(len(oldFS.state)) >= val.(uint64) {
450+
fs.state = oldFS.state[:val.(uint64)]
451+
} else {
452+
if uint64(cap(oldFS.state)) >= val.(uint64) {
453+
fs.state = oldFS.state[:val.(uint64)]
454+
} else {
455+
fs.state = make([]any, val.(uint64))
456+
copy(fs.state, oldFS.state)
457+
}
458+
}
447459
}
448460

449461
e.state.set(fp, fs)

pkg/demoinfocs/sendtables2/field_state.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package sendtables2
22

33
type fieldState struct {
4-
state []interface{}
4+
state []any
55
}
66

77
func newFieldState() *fieldState {
88
return &fieldState{
9-
state: make([]interface{}, 8),
9+
state: make([]any, 8),
1010
}
1111
}
1212

13-
func (s *fieldState) get(fp *fieldPath) interface{} {
13+
func (s *fieldState) get(fp *fieldPath) any {
1414
x := s
1515
z := 0
16+
1617
for i := 0; i <= fp.last; i++ {
1718
z = fp.path[i]
1819
if len(x.state) < z+1 {
@@ -26,20 +27,21 @@ func (s *fieldState) get(fp *fieldPath) interface{} {
2627
}
2728
x = x.state[z].(*fieldState)
2829
}
30+
2931
return nil
3032
}
3133

32-
func (s *fieldState) set(fp *fieldPath, v interface{}) {
34+
func (s *fieldState) set(fp *fieldPath, v any) {
3335
x := s
3436
z := 0
3537

3638
for i := 0; i <= fp.last; i++ {
3739
z = fp.path[i]
3840

3941
if y := len(x.state); y <= z {
40-
newCap := max(z+2, y*2)
41-
if z+2 > cap(x.state) {
42-
newSlice := make([]interface{}, z+1, newCap)
42+
newCap := max(z*2, y*2)
43+
if z+1 > cap(x.state) {
44+
newSlice := make([]any, z+1, newCap)
4345
copy(newSlice, x.state)
4446
x.state = newSlice
4547
} else {

pkg/demoinfocs/sendtables2/serializer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ func (s *serializer) getFieldPathForName(fp *fieldPath, name string) bool {
8686

8787
func (s *serializer) getFieldPaths(fp *fieldPath, state *fieldState) []*fieldPath {
8888
results := make([]*fieldPath, 0, 4)
89+
8990
for i, f := range s.fields {
9091
fp.path[fp.last] = i
9192
results = append(results, f.getFieldPaths(fp, state)...)
9293
}
94+
9395
return results
9496
}
9597

0 commit comments

Comments
 (0)