Some optimizations in PieceRequestOrder.Update and item comparisons

This commit is contained in:
Matt Joiner 2021-12-15 18:07:17 +11:00
parent 8c129d6bfc
commit 1668a18859
2 changed files with 17 additions and 24 deletions

View File

@ -21,24 +21,19 @@ type (
ChunkSpec = types.ChunkSpec
)
type pieceOrderInput struct {
PieceRequestOrderState
PieceRequestOrderKey
}
func pieceOrderLess(i, j pieceOrderInput) multiless.Computation {
func pieceOrderLess(i, j *pieceRequestOrderItem) multiless.Computation {
return multiless.New().Int(
int(j.Priority), int(i.Priority),
int(j.state.Priority), int(i.state.Priority),
).Bool(
j.Partial, i.Partial,
j.state.Partial, i.state.Partial,
).Int64(
i.Availability, j.Availability,
i.state.Availability, j.state.Availability,
).Int(
i.Index, j.Index,
i.key.Index, j.key.Index,
).Lazy(func() multiless.Computation {
return multiless.New().Cmp(bytes.Compare(
i.InfoHash[:],
j.InfoHash[:],
i.key.InfoHash[:],
j.key.InfoHash[:],
))
})
}

View File

@ -37,16 +37,7 @@ type pieceRequestOrderItem struct {
func (me *pieceRequestOrderItem) Less(other btree.Item) bool {
otherConcrete := other.(*pieceRequestOrderItem)
return pieceOrderLess(
pieceOrderInput{
PieceRequestOrderState: me.state,
PieceRequestOrderKey: me.key,
},
pieceOrderInput{
PieceRequestOrderState: otherConcrete.state,
PieceRequestOrderKey: otherConcrete.key,
},
).Less()
return pieceOrderLess(me, otherConcrete).Less()
}
func (me *PieceRequestOrder) Add(key PieceRequestOrderKey, state PieceRequestOrderState) {
@ -63,10 +54,17 @@ func (me *PieceRequestOrder) Add(key PieceRequestOrderKey, state PieceRequestOrd
}
func (me *PieceRequestOrder) Update(key PieceRequestOrderKey, state PieceRequestOrderState) {
item := me.existingItemForKey(key)
if item.state == state {
oldState, ok := me.keys[key]
if !ok {
panic("key should have been added already")
}
if state == oldState {
return
}
item := pieceRequestOrderItem{
key: key,
state: oldState,
}
if me.tree.Delete(&item) == nil {
panic(fmt.Sprintf("%#v", key))
}