Skip to content

Commit

Permalink
Merge pull request #448 from Ektaros/fp-name-cache
Browse files Browse the repository at this point in the history
FieldPath name cache
  • Loading branch information
markus-wa authored Oct 18, 2023
2 parents df4c335 + db36454 commit 1c75df8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
28 changes: 27 additions & 1 deletion pkg/demoinfocs/sendtables2/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ import (
st "github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs/sendtables"
)

type fpNameTreeCache struct {
next map[int]*fpNameTreeCache
name string
}

type class struct {
classId int32
name string
serializer *serializer
createdHandlers []st.EntityCreatedHandler
fpNameCache *fpNameTreeCache
}

func (c *class) ID() int {
Expand Down Expand Up @@ -72,7 +78,27 @@ func (c *class) collectFieldsEntries(fields []*field, prefix string) []string {
}

func (c *class) getNameForFieldPath(fp *fieldPath) string {
return strings.Join(c.serializer.getNameForFieldPath(fp, 0), ".")
currentCacheNode := c.fpNameCache

for i := 0; i <= fp.last; i++ {
if currentCacheNode.next == nil {
currentCacheNode.next = make(map[int]*fpNameTreeCache)
}

pos := fp.path[i]
next, exists := currentCacheNode.next[pos]
if !exists {
next = &fpNameTreeCache{}
currentCacheNode.next[pos] = next
}
currentCacheNode = next
}

if currentCacheNode.name == "" {
currentCacheNode.name = strings.Join(c.serializer.getNameForFieldPath(fp, 0), ".")
}

return currentCacheNode.name
}

func (c *class) getTypeForFieldPath(fp *fieldPath) *fieldType {
Expand Down
3 changes: 3 additions & 0 deletions pkg/demoinfocs/sendtables2/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ func (p *Parser) OnDemoClassInfo(m *msgs2.CDemoClassInfo) error {
classId: classId,
name: networkName,
serializer: p.serializers[networkName],
fpNameCache: &fpNameTreeCache{
next: make(map[int]*fpNameTreeCache),
},
}
p.classesById[class.classId] = class
p.classesByName[class.name] = class
Expand Down

0 comments on commit 1c75df8

Please sign in to comment.