]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Some optimizations in PieceRequestOrder.Update and item comparisons
authorMatt Joiner <anacrolix@gmail.com>
Wed, 15 Dec 2021 07:07:17 +0000 (18:07 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 15 Dec 2021 08:18:16 +0000 (19:18 +1100)
request-strategy/order.go
request-strategy/piece-request-order.go

index 0fa516985ed654e67d474b9af710140680734ca7..50c9838fa1c08530dfea60546b0ed8786c4f1a50 100644 (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[:],
                ))
        })
 }
index d906b83595e584b03959578dba37a6e7ac2b4f97..c9a89663706c20dc20ea458d855e5edeceaca364 100644 (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))
        }