]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Use an iterator to skip through dirty chunks
authorMatt Joiner <anacrolix@gmail.com>
Tue, 21 Sep 2021 00:48:15 +0000 (10:48 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 21 Sep 2021 00:48:15 +0000 (10:48 +1000)
piece.go

index 7424bba403476c278ff6c558e18a21f9127b3afa..bd107144ec91e8e9533a591416f41036da2ef848 100644 (file)
--- a/piece.go
+++ b/piece.go
@@ -237,7 +237,30 @@ func (p *Piece) State() PieceState {
        return p.t.PieceState(p.index)
 }
 
-func (p *Piece) iterUndirtiedChunks(f func(cs chunkIndexType)) {
+func (p *Piece) iterUndirtiedChunks(f func(chunkIndexType)) {
+       // Use an iterator to jump between dirty bits.
+       if true {
+               it := p.t.dirtyChunks.Iterator()
+               startIndex := p.requestIndexOffset()
+               endIndex := startIndex + p.numChunks()
+               it.AdvanceIfNeeded(startIndex)
+               lastDirty := startIndex - 1
+               for it.HasNext() {
+                       next := it.Next()
+                       if next >= endIndex {
+                               break
+                       }
+                       for index := lastDirty + 1; index < next; index++ {
+                               f(index - startIndex)
+                       }
+                       lastDirty = next
+               }
+               for index := lastDirty + 1; index < endIndex; index++ {
+                       f(index - startIndex)
+               }
+               return
+       }
+       // The original implementation.
        for i := chunkIndexType(0); i < p.numChunks(); i++ {
                if p.chunkIndexDirty(i) {
                        continue