Skip to content

Commit 2773154

Browse files
committed
Cleaned up some pooling stuff
1 parent ce86910 commit 2773154

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

bitread/bitread.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (r *BitReader) ReadUBitInt() uint {
7676
return res
7777
}
7878

79-
var bitReaderPool sync.Pool = sync.Pool{
79+
var bitReaderPool = sync.Pool{
8080
New: func() interface{} {
8181
return new(BitReader)
8282
},

demopacket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (p *Parser) parsePacket() {
9292
}
9393
p.msgQueue <- m
9494

95-
// Reset to 0 length and pool
95+
// Reset length to 0 and pool
9696
*b = (*b)[:0]
9797
byteSlicePool.Put(b)
9898

sendtables/entity.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,10 @@ func (e *Entity) FindProperty(name string) *PropertyEntry {
3737
return prop
3838
}
3939

40-
// Wrapping the slice in a struct causes far fewer object allocations for some reason
41-
type entrySliceBacker struct {
42-
slice []*PropertyEntry
43-
}
44-
45-
var entrySliceBackerPool sync.Pool = sync.Pool{
40+
var updatedPropIndicesPool = sync.Pool{
4641
New: func() interface{} {
47-
return &entrySliceBacker{make([]*PropertyEntry, 0, 8)}
42+
s := make([]int, 0, 8)
43+
return &s
4844
},
4945
}
5046

@@ -53,21 +49,20 @@ var entrySliceBackerPool sync.Pool = sync.Pool{
5349
func (e *Entity) ApplyUpdate(reader *bit.BitReader) {
5450
idx := -1
5551
newWay := reader.ReadBit()
56-
backer := entrySliceBackerPool.Get().(*entrySliceBacker)
52+
updatedPropIndices := updatedPropIndicesPool.Get().(*[]int)
5753

58-
// TODO: Use index slice instead?
5954
for idx = readFieldIndex(reader, idx, newWay); idx != -1; idx = readFieldIndex(reader, idx, newWay) {
60-
backer.slice = append(backer.slice, &e.props[idx])
55+
*updatedPropIndices = append(*updatedPropIndices, idx)
6156
}
6257

63-
for _, prop := range backer.slice {
64-
prop.FirePropertyUpdate(propDecoder.decodeProp(prop.entry, reader))
58+
for _, idx := range *updatedPropIndices {
59+
e.props[idx].FirePropertyUpdate(propDecoder.decodeProp(e.props[idx].entry, reader))
6560
}
6661

67-
// Reset to 0 length before pooling
68-
backer.slice = backer.slice[:0]
62+
// Reset length to 0 before pooling
63+
*updatedPropIndices = (*updatedPropIndices)[:0]
6964
// Defer has quite the overhead so we just fill the pool here
70-
entrySliceBackerPool.Put(backer)
65+
updatedPropIndicesPool.Put(updatedPropIndices)
7166
}
7267

7368
func readFieldIndex(reader *bit.BitReader, lastIndex int, newWay bool) int {

0 commit comments

Comments
 (0)