Skip to content

S2 perf improvements #563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/llgcode/draw2d v0.0.0-20230723155556-e595d7c7e75e
github.com/markus-wa/go-heatmap/v2 v2.0.0
github.com/markus-wa/go-unassert v0.1.3
github.com/markus-wa/gobitread v0.2.3
github.com/markus-wa/gobitread v0.2.4
github.com/markus-wa/godispatch v1.4.1
github.com/markus-wa/ice-cipher-go v0.0.0-20230901094113-348096939ba7
github.com/markus-wa/quickhull-go/v2 v2.2.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/markus-wa/go-unassert v0.1.3 h1:4N2fPLUS3929Rmkv94jbWskjsLiyNT2yQpCul
github.com/markus-wa/go-unassert v0.1.3/go.mod h1:/pqt7a0LRmdsRNYQ2nU3SGrXfw3bLXrvIkakY/6jpPY=
github.com/markus-wa/gobitread v0.2.3 h1:COx7dtYQ7Q+77hgUmD+O4MvOcqG7y17RP3Z7BbjRvPs=
github.com/markus-wa/gobitread v0.2.3/go.mod h1:PcWXMH4gx7o2CKslbkFkLyJB/aHW7JVRG3MRZe3PINg=
github.com/markus-wa/gobitread v0.2.4 h1:BDr3dZnsqntDD4D8E7DzhkQlASIkQdfxCXLhWcI2K5A=
github.com/markus-wa/gobitread v0.2.4/go.mod h1:PcWXMH4gx7o2CKslbkFkLyJB/aHW7JVRG3MRZe3PINg=
github.com/markus-wa/godispatch v1.4.1 h1:Cdff5x33ShuX3sDmUbYWejk7tOuoHErFYMhUc2h7sLc=
github.com/markus-wa/godispatch v1.4.1/go.mod h1:tk8L0yzLO4oAcFwM2sABMge0HRDJMdE8E7xm4gK/+xM=
github.com/markus-wa/ice-cipher-go v0.0.0-20230901094113-348096939ba7 h1:aR9pvnlnBxifXBmzidpAiq2prLSGlkhE904qnk2sCz4=
Expand Down
9 changes: 6 additions & 3 deletions pkg/demoinfocs/common/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Player struct {
demoInfoProvider demoInfoProvider // provider for demo info such as tick-rate or current tick

SteamID64 uint64 // 64-bit representation of the user's Steam ID. See https://developer.valvesoftware.com/wiki/SteamID
LastAlivePosition r3.Vector // The location where the player was last alive. Should be equal to Position if the player is still alive.
LastAlivePosition r3.Vector // Deprecated: will be removed in v5 due to performance concerns, track this yourself.
UserID int // Mostly used in game-events to address this player
Name string // Steam / in-game user name
Inventory map[int]*Equipment // All weapons / equipment the player is currently carrying. See also Weapons().
Expand All @@ -33,7 +33,7 @@ type Player struct {
IsPlanting bool
IsReloading bool
IsUnknown bool // Used to identify unknown/broken players. see https://github.com/markus-wa/demoinfocs-golang/issues/162
PreviousFramePosition r3.Vector // CS2 only, used to compute velocity as it's not networked in CS2 demos
PreviousFramePosition r3.Vector // Deprecated: may be removed in v5 due to performance concerns, track this yourself.
}

func (p *Player) PlayerPawnEntity() st.Entity {
Expand Down Expand Up @@ -85,9 +85,11 @@ func (p *Player) IsAlive() bool {
}

if p.demoInfoProvider.IsSource2() {
if pawnEntity := p.PlayerPawnEntity(); pawnEntity != nil {
pawnEntity := p.PlayerPawnEntity()
if pawnEntity != nil {
return pawnEntity.PropertyValueMust("m_lifeState").S2UInt64() == 0
}

return getBool(p.Entity, "m_bPawnIsAlive")
}

Expand Down Expand Up @@ -535,6 +537,7 @@ func (p *Player) PositionEyes() r3.Vector {
}

// Velocity returns the player's velocity.
// Deprecated: will be removed due to performance concerns, you will need to track this yourself.
func (p *Player) Velocity() r3.Vector {
if p.demoInfoProvider.IsSource2() {
t := 64.0
Expand Down
4 changes: 3 additions & 1 deletion pkg/demoinfocs/datatables.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package demoinfocs
import (
"fmt"
"math"
"os"
"strings"

"github.com/golang/geo/r3"
Expand Down Expand Up @@ -637,6 +638,7 @@ func (p *parser) bindNewPlayerPawnS2(pawnEntity st.Entity) {
if pl == nil {
return
}

if pl.IsAlive() {
pl.LastAlivePosition = pos
}
Expand Down Expand Up @@ -919,7 +921,7 @@ func (p *parser) bindGrenadeProjectiles(entity st.Entity) {
if exists {
wep = weaponType
} else {
fmt.Printf("unknown grenade model %d\n", model)
fmt.Fprintf(os.Stderr, "unknown grenade model %d\n", model)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/demoinfocs/demoinfocs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func TestConcurrent(t *testing.T) {
func parseDefaultDemo(tb testing.TB) {
tb.Helper()

f := openFile(tb, defaultDemPath)
f := openFile(tb, s2DemPath)
defer mustClose(tb, f)

p := demoinfocs.NewParser(f)
Expand Down Expand Up @@ -599,15 +599,15 @@ func BenchmarkDemoInfoCs(b *testing.B) {
}

func BenchmarkInMemory(b *testing.B) {
f := openFile(b, defaultDemPath)
f := openFile(b, s2DemPath)
defer mustClose(b, f)

inf, err := f.Stat()
assert.NoError(b, err, "failed to stat file %q", defaultDemPath)
assert.NoError(b, err, "failed to stat file %q", s2DemPath)

d := make([]byte, inf.Size())
n, err := f.Read(d)
assert.NoError(b, err, "failed to read file %q", defaultDemPath)
assert.NoError(b, err, "failed to read file %q", s2DemPath)
assert.Equal(b, int64(n), inf.Size(), "byte count not as expected")

b.ResetTimer()
Expand Down
20 changes: 6 additions & 14 deletions pkg/demoinfocs/sendtables2/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,9 @@ func (p *Parser) FilterEntity(fb func(*Entity) bool) []*Entity {
}

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

for _, fp := range *paths {
for _, fp := range (*paths)[:n] {
f := e.class.serializer.getFieldForFieldPath(fp, 0)
name := e.class.getNameForFieldPath(fp)
decoder, base := e.class.serializer.getDecoderForFieldPath2(fp, 0)
Expand Down Expand Up @@ -462,8 +462,6 @@ func (e *Entity) readFields(r *reader, paths *[]*fieldPath) {
S2: true,
})
}

fp.release()
}
}

Expand Down Expand Up @@ -491,10 +489,7 @@ func (p *Parser) OnPacketEntities(m *msgs2.CSVCMsg_PacketEntities) error {
op st.EntityOp
}

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

for ; updates > 0; updates-- {
var (
Expand Down Expand Up @@ -530,12 +525,10 @@ func (p *Parser) OnPacketEntities(m *msgs2.CSVCMsg_PacketEntities) error {

if baseline != nil {
// POV demos are missing some baselines?
e.readFields(newReader(baseline), &paths)
paths = paths[:0]
e.readFields(newReader(baseline), &p.pathCache)
}

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

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

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

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

node := huffTree
i := 0

for !fp.done {
var next huffmanTree
Expand All @@ -326,14 +326,26 @@ func readFieldPaths(r *reader, paths *[]*fieldPath) {
fieldPathTable[next.Value()].fn(r, fp)

if !fp.done {
*paths = append(*paths, fp.copy())
if len(*paths) <= i {
*paths = append(*paths, fp.copy())
} else {
x := (*paths)[i]
x.last = fp.last
x.done = fp.done

copy(x.path, fp.path)
}

i++
}
} else {
node = next
}
}

fp.release()

return i
}

// newHuffmanTree creates a new huffmanTree from the field path table
Expand Down
7 changes: 6 additions & 1 deletion pkg/demoinfocs/sendtables2/field_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ func (s *fieldState) get(fp *fieldPath) interface{} {
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 newCap > cap(x.state) {
if z+2 > cap(x.state) {
newSlice := make([]interface{}, z+1, newCap)
copy(newSlice, x.state)
x.state = newSlice
Expand All @@ -45,15 +47,18 @@ func (s *fieldState) set(fp *fieldPath, v interface{}) {
x.state = x.state[:z+1]
}
}

if i == fp.last {
if _, ok := x.state[z].(*fieldState); !ok {
x.state[z] = v
}
return
}

if _, ok := x.state[z].(*fieldState); !ok {
x.state[z] = newFieldState()
}

x = x.state[z].(*fieldState)
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/demoinfocs/sendtables2/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Parser struct {
entityFullPackets int
entities map[int32]*Entity
entityHandlers []st.EntityHandler
pathCache []*fieldPath
}

func (p *Parser) ReadEnterPVS(r *bit.BitReader, index int, entities map[int]st.Entity, slot int) st.Entity {
Expand Down
Loading