Skip to content

Commit

Permalink
Merge pull request #454 from markus-wa/perf-improvements
Browse files Browse the repository at this point in the history
Performance improvements
  • Loading branch information
markus-wa authored Oct 31, 2023
2 parents aa1b925 + 051921a commit 8bf8a71
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
41 changes: 27 additions & 14 deletions pkg/demoinfocs/sendtables2/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Entity struct {
onCreateFinished []func()
onDestroy []func()
updateHandlers map[string][]st.PropertyUpdateHandler
propCache map[string]st.Property
}

func (e *Entity) ServerClass() st.ServerClass {
Expand Down Expand Up @@ -126,15 +127,19 @@ func (p property) Bind(variable any, t st.PropertyValueType) {
}

func (e *Entity) Property(name string) st.Property {
ok := e.class.serializer.checkFieldName(name)
if !ok {
return nil
}
if e.propCache[name] == nil {
ok := e.class.serializer.checkFieldName(name)
if !ok {
return nil
}

return property{
entity: e,
name: name,
e.propCache[name] = property{
entity: e,
name: name,
}
}

return e.propCache[name]
}

func (e *Entity) BindProperty(prop string, variable any, t st.PropertyValueType) {
Expand Down Expand Up @@ -258,6 +263,7 @@ func newEntity(index, serial int32, class *class) *Entity {
onCreateFinished: nil,
onDestroy: nil,
updateHandlers: make(map[string][]st.PropertyUpdateHandler),
propCache: map[string]st.Property{},
}
}

Expand Down Expand Up @@ -406,10 +412,10 @@ func (p *Parser) FilterEntity(fb func(*Entity) bool) []*Entity {
return entities
}

func (e *Entity) readFields(r *reader) {
fps := readFieldPaths(r)
func (e *Entity) readFields(r *reader, paths *[]*fieldPath) {
readFieldPaths(r, paths)

for _, fp := range fps {
for _, fp := range *paths {
decoder := e.class.serializer.getDecoderForFieldPath(fp, 0)

val := decoder(r)
Expand Down Expand Up @@ -456,7 +462,10 @@ func (p *Parser) OnPacketEntities(m *msgs2.CSVCMsg_PacketEntities) error {
op st.EntityOp
}

var tuples []tuple
var (
tuples []tuple
paths = make([]*fieldPath, 0)
)

for ; updates > 0; updates-- {
var (
Expand Down Expand Up @@ -487,8 +496,11 @@ func (p *Parser) OnPacketEntities(m *msgs2.CSVCMsg_PacketEntities) error {
e = newEntity(index, serial, class)
p.entities[index] = e

e.readFields(newReader(baseline))
e.readFields(r)
e.readFields(newReader(baseline), &paths)
paths = paths[:0]

e.readFields(r, &paths)
paths = paths[:0]

// Fire created-handlers so update-handlers can be registered
for _, h := range class.createdHandlers {
Expand All @@ -512,7 +524,8 @@ func (p *Parser) OnPacketEntities(m *msgs2.CSVCMsg_PacketEntities) error {
op |= st.EntityOpEntered
}

e.readFields(r)
e.readFields(r, &paths)
paths = paths[:0]
}
} else {
e = p.entities[index]
Expand Down
12 changes: 5 additions & 7 deletions pkg/demoinfocs/sendtables2/field_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,36 +306,34 @@ func (fp *fieldPath) release() {
}

// readFieldPaths reads a new slice of fieldPath values from the given reader
func readFieldPaths(r *reader) []*fieldPath {
func readFieldPaths(r *reader, paths *[]*fieldPath) {
fp := newFieldPath()

node := huffTree

paths := []*fieldPath{}

for !fp.done {
var next huffmanTree

if r.readBits(1) == 1 {
if r.readBoolean() {
next = node.Right()
} else {
next = node.Left()
}

if next.IsLeaf() {
node = huffTree

fieldPathTable[next.Value()].fn(r, fp)

if !fp.done {
paths = append(paths, fp.copy())
*paths = append(*paths, fp.copy())
}
} else {
node = next
}
}

fp.release()

return paths
}

// newHuffmanTree creates a new huffmanTree from the field path table
Expand Down
5 changes: 3 additions & 2 deletions pkg/demoinfocs/sendtables2/quantizedfloat.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func (qfd *quantizedFloatDecoder) validateFlags() {
}
}

var qFloatMultipliers = []float32{0.9999, 0.99, 0.9, 0.8, 0.7}

// Assign multipliers
func (qfd *quantizedFloatDecoder) assignMultipliers(steps uint32) {
qfd.HighLowMul = 0.0
Expand All @@ -85,9 +87,8 @@ func (qfd *quantizedFloatDecoder) assignMultipliers(steps uint32) {

// Adjust precision
if (HighMul*Range > float32(High)) || (float64(HighMul*Range) > float64(High)) {
multipliers := []float32{0.9999, 0.99, 0.9, 0.8, 0.7}

for _, mult := range multipliers {
for _, mult := range qFloatMultipliers {
HighMul = float32(High) / Range * mult

if (HighMul*Range > float32(High)) || (float64(HighMul*Range) > float64(High)) {
Expand Down

0 comments on commit 8bf8a71

Please sign in to comment.