Minimize allocations due to heap escapes

This commit is contained in:
Matt Joiner 2021-12-09 21:49:29 +11:00
parent 8c9a308c9a
commit 8c5df03e14
2 changed files with 13 additions and 10 deletions

View File

@ -81,7 +81,7 @@ func GetRequestablePieces(input Input, pro *PieceRequestOrder, f func(ih metainf
} }
var allTorrentsUnverifiedBytes int64 var allTorrentsUnverifiedBytes int64
pro.tree.Ascend(func(i btree.Item) bool { pro.tree.Ascend(func(i btree.Item) bool {
_i := i.(pieceRequestOrderItem) _i := i.(*pieceRequestOrderItem)
ih := _i.key.InfoHash ih := _i.key.InfoHash
var t Torrent = input.Torrent(ih) var t Torrent = input.Torrent(ih)
var piece Piece = t.Piece(_i.key.Index) var piece Piece = t.Piece(_i.key.Index)

View File

@ -35,8 +35,8 @@ type pieceRequestOrderItem struct {
state PieceRequestOrderState state PieceRequestOrderState
} }
func (me pieceRequestOrderItem) Less(other btree.Item) bool { func (me *pieceRequestOrderItem) Less(other btree.Item) bool {
otherConcrete := other.(pieceRequestOrderItem) otherConcrete := other.(*pieceRequestOrderItem)
return pieceOrderLess( return pieceOrderLess(
pieceOrderInput{ pieceOrderInput{
PieceRequestOrderState: me.state, PieceRequestOrderState: me.state,
@ -53,7 +53,7 @@ func (me *PieceRequestOrder) Add(key PieceRequestOrderKey, state PieceRequestOrd
if _, ok := me.keys[key]; ok { if _, ok := me.keys[key]; ok {
panic(key) panic(key)
} }
if me.tree.ReplaceOrInsert(pieceRequestOrderItem{ if me.tree.ReplaceOrInsert(&pieceRequestOrderItem{
key: key, key: key,
state: state, state: state,
}) != nil { }) != nil {
@ -63,13 +63,15 @@ func (me *PieceRequestOrder) Add(key PieceRequestOrderKey, state PieceRequestOrd
} }
func (me *PieceRequestOrder) Update(key PieceRequestOrderKey, state PieceRequestOrderState) { func (me *PieceRequestOrder) Update(key PieceRequestOrderKey, state PieceRequestOrderState) {
if me.tree.Delete(me.existingItemForKey(key)) == nil { item := me.existingItemForKey(key)
if item.state == state {
return
}
if me.tree.Delete(&item) == nil {
panic(fmt.Sprintf("%#v", key)) panic(fmt.Sprintf("%#v", key))
} }
if me.tree.ReplaceOrInsert(pieceRequestOrderItem{ item.state = state
key: key, if me.tree.ReplaceOrInsert(&item) != nil {
state: state,
}) != nil {
panic(key) panic(key)
} }
me.keys[key] = state me.keys[key] = state
@ -83,7 +85,8 @@ func (me *PieceRequestOrder) existingItemForKey(key PieceRequestOrderKey) pieceR
} }
func (me *PieceRequestOrder) Delete(key PieceRequestOrderKey) { func (me *PieceRequestOrder) Delete(key PieceRequestOrderKey) {
if me.tree.Delete(me.existingItemForKey(key)) == nil { item := me.existingItemForKey(key)
if me.tree.Delete(&item) == nil {
panic(key) panic(key)
} }
delete(me.keys, key) delete(me.keys, key)