Skip to content

Commit b1d35cc

Browse files
committed
Performance improvements for property-decoding
1 parent 0dce661 commit b1d35cc

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

entities.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ func (p *Parser) readEnterPVS(reader *bit.BitReader, entityID int) *st.Entity {
5454
newEntity := st.NewEntity(entityID, p.stParser.ServerClasses()[scID])
5555

5656
if p.preprocessedBaselines[scID] != nil {
57-
for idx, val := range p.preprocessedBaselines[scID] {
58-
newEntity.Props()[idx].FirePropertyUpdate(val)
59-
}
57+
newEntity.ApplyBaseline(p.preprocessedBaselines[scID])
6058
} else {
6159
if p.instanceBaselines[scID] != nil {
6260
r := bit.NewSmallBitReader(bytes.NewReader(p.instanceBaselines[scID]))

sendtables/entity.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ func (e *Entity) ApplyUpdate(reader *bit.BitReader) {
5656
}
5757

5858
for _, idx := range *updatedPropIndices {
59-
e.props[idx].FirePropertyUpdate(propDecoder.decodeProp(e.props[idx].entry, reader))
59+
propDecoder.decodeProp(&e.props[idx], reader)
60+
e.props[idx].firePropertyUpdate()
6061
}
6162

6263
// Reset length to 0 before pooling
@@ -115,6 +116,13 @@ func (e *Entity) InitializeBaseline(r *bit.BitReader) map[int]PropValue {
115116
return baseline
116117
}
117118

119+
// ApplyBaseline baseline applies a previously collected baseline
120+
func (e *Entity) ApplyBaseline(baseline map[int]PropValue) {
121+
for idx := range baseline {
122+
e.props[idx].value = baseline[idx]
123+
}
124+
}
125+
118126
const maxCoordInt = 16384
119127

120128
// Position returns the entity's position in world coordinates.
@@ -164,13 +172,11 @@ func (pe *PropertyEntry) Value() PropValue {
164172
return pe.value
165173
}
166174

167-
// FirePropertyUpdate triggers all registered PropertyUpdateHandler
168-
// on the PropertyEntry with the given PropValue.
169-
func (pe *PropertyEntry) FirePropertyUpdate(value PropValue) {
170-
pe.value = value
175+
// Trigger all the registered PropertyUpdateHandlers on this entry.
176+
func (pe *PropertyEntry) firePropertyUpdate() {
171177
for _, h := range pe.updateHandlers {
172178
if h != nil {
173-
h(value)
179+
h(pe.value)
174180
}
175181
}
176182
}

sendtables/propdecoder.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,28 @@ type PropValue struct {
8282

8383
type propertyDecoder struct{}
8484

85-
func (propertyDecoder) decodeProp(fProp *FlattenedPropEntry, reader *bit.BitReader) PropValue {
86-
switch fProp.prop.rawType {
85+
func (propertyDecoder) decodeProp(prop *PropertyEntry, reader *bit.BitReader) {
86+
switch prop.entry.prop.rawType {
8787
case propTypeFloat:
88-
return PropValue{FloatVal: propDecoder.decodeFloat(fProp.prop, reader)}
88+
prop.value.FloatVal = propDecoder.decodeFloat(prop.entry.prop, reader)
8989

9090
case propTypeInt:
91-
return PropValue{IntVal: propDecoder.decodeInt(fProp.prop, reader)}
91+
prop.value.IntVal = propDecoder.decodeInt(prop.entry.prop, reader)
9292

9393
case propTypeVectorXY:
94-
return PropValue{VectorVal: propDecoder.decodeVectorXY(fProp.prop, reader)}
94+
prop.value.VectorVal = propDecoder.decodeVectorXY(prop.entry.prop, reader)
9595

9696
case propTypeVector:
97-
return PropValue{VectorVal: propDecoder.decodeVector(fProp.prop, reader)}
97+
prop.value.VectorVal = propDecoder.decodeVector(prop.entry.prop, reader)
9898

9999
case propTypeArray:
100-
return PropValue{ArrayVal: propDecoder.decodeArray(fProp, reader)}
100+
prop.value.ArrayVal = propDecoder.decodeArray(prop.entry, reader)
101101

102102
case propTypeString:
103-
return PropValue{StringVal: propDecoder.decodeString(fProp.prop, reader)}
103+
prop.value.StringVal = propDecoder.decodeString(prop.entry.prop, reader)
104104

105105
default:
106-
panic(fmt.Sprintf("Unknown prop type %d", fProp.prop.rawType))
106+
panic(fmt.Sprintf("Unknown prop type %d", prop.entry.prop.rawType))
107107
}
108108
}
109109

@@ -295,10 +295,13 @@ func (propertyDecoder) decodeArray(fProp *FlattenedPropEntry, reader *bit.BitRea
295295

296296
res := make([]PropValue, 0, nElements)
297297

298-
tmp := &FlattenedPropEntry{prop: fProp.arrayElementProp}
298+
tmp := &PropertyEntry{
299+
entry: &FlattenedPropEntry{prop: fProp.arrayElementProp},
300+
}
299301

300302
for i := 0; i < nElements; i++ {
301-
res = append(res, propDecoder.decodeProp(tmp, reader))
303+
propDecoder.decodeProp(tmp, reader)
304+
res = append(res, tmp.value)
302305
}
303306

304307
return res

sendtables/sendtables.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (sc *ServerClass) FireEntityCreatedEvent(entity *Entity) {
5959
}
6060

6161
for _, v := range entity.props {
62-
v.FirePropertyUpdate(v.value)
62+
v.firePropertyUpdate()
6363
}
6464
}
6565

0 commit comments

Comments
 (0)