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
pro.tree.Ascend(func(i btree.Item) bool {
_i := i.(pieceRequestOrderItem)
_i := i.(*pieceRequestOrderItem)
ih := _i.key.InfoHash
var t Torrent = input.Torrent(ih)
var piece Piece = t.Piece(_i.key.Index)

View File

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