Skip to content

Commit

Permalink
Revert "perf: reduce memory usage + increase speed ~10%"
Browse files Browse the repository at this point in the history
This reverts commit 2d8b7c7.
  • Loading branch information
markus-wa committed Dec 2, 2024
1 parent 2d8b7c7 commit 5a61323
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 28 deletions.
22 changes: 5 additions & 17 deletions pkg/demoinfocs/sendtables2/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,25 +437,13 @@ func (e *Entity) readFields(r *reader, paths *[]*fieldPath) {
val := decoder(r)

if base && (f.model == fieldModelVariableArray || f.model == fieldModelVariableTable) {
fs := fieldState{}
oldFS := e.state.get(fp)
fs := newFieldState()

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

if !ok {
fs.state = make([]any, val.(uint64))
}

if ok {
if uint64(len(oldFS.state)) >= val.(uint64) {
fs.state = oldFS.state[:val.(uint64)]
} else {
if uint64(cap(oldFS.state)) >= val.(uint64) {
fs.state = oldFS.state[:val.(uint64)]
} else {
fs.state = make([]any, val.(uint64))
copy(fs.state, oldFS.state)
}
}
if oldFS != nil {
copy(fs.state, oldFS.(*fieldState).state[:min(len(fs.state), len(oldFS.(*fieldState).state))])
}

e.state.set(fp, fs)
Expand Down
16 changes: 7 additions & 9 deletions pkg/demoinfocs/sendtables2/field_state.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package sendtables2

type fieldState struct {
state []any
state []interface{}
}

func newFieldState() *fieldState {
return &fieldState{
state: make([]any, 8),
state: make([]interface{}, 8),
}
}

func (s *fieldState) get(fp *fieldPath) any {
func (s *fieldState) get(fp *fieldPath) interface{} {
x := s
z := 0

for i := 0; i <= fp.last; i++ {
z = fp.path[i]
if len(x.state) < z+1 {
Expand All @@ -27,21 +26,20 @@ func (s *fieldState) get(fp *fieldPath) any {
}
x = x.state[z].(*fieldState)
}

return nil
}

func (s *fieldState) set(fp *fieldPath, v any) {
func (s *fieldState) set(fp *fieldPath, v interface{}) {
x := s
z := 0

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

if y := len(x.state); y <= z {
newCap := max(z*2, y*2)
if z+1 > cap(x.state) {
newSlice := make([]any, z+1, newCap)
newCap := max(z+2, y*2)
if z+2 > cap(x.state) {
newSlice := make([]interface{}, z+1, newCap)
copy(newSlice, x.state)
x.state = newSlice
} else {
Expand Down
2 changes: 0 additions & 2 deletions pkg/demoinfocs/sendtables2/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,10 @@ func (s *serializer) getFieldPathForName(fp *fieldPath, name string) bool {

func (s *serializer) getFieldPaths(fp *fieldPath, state *fieldState) []*fieldPath {
results := make([]*fieldPath, 0, 4)

for i, f := range s.fields {
fp.path[fp.last] = i
results = append(results, f.getFieldPaths(fp, state)...)
}

return results
}

Expand Down

0 comments on commit 5a61323

Please sign in to comment.