sync: replace container/heap with github.com/aalpar/deheap

This commit is contained in:
Nick Craig-Wood 2020-03-13 17:10:06 +00:00
parent 84369286df
commit 1e3d899db8
2 changed files with 8 additions and 7 deletions

View file

@ -1,11 +1,11 @@
package sync package sync
import ( import (
"container/heap"
"context" "context"
"strings" "strings"
"sync" "sync"
"github.com/aalpar/deheap"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/fserrors" "github.com/rclone/rclone/fs/fserrors"
@ -38,7 +38,7 @@ func newPipe(orderBy string, stats func(items int, totalSize int64), maxBacklog
less: less, less: less,
} }
if p.less != nil { if p.less != nil {
heap.Init(p) deheap.Init(p)
} }
return p, nil return p, nil
} }
@ -73,9 +73,6 @@ func (p *pipe) Pop() interface{} {
return item return item
} }
// Check interface satisfied
var _ heap.Interface = (*pipe)(nil)
// Put an pair into the pipe // Put an pair into the pipe
// //
// It returns ok = false if the context was cancelled // It returns ok = false if the context was cancelled
@ -90,7 +87,7 @@ func (p *pipe) Put(ctx context.Context, pair fs.ObjectPair) (ok bool) {
// no order-by // no order-by
p.queue = append(p.queue, pair) p.queue = append(p.queue, pair)
} else { } else {
heap.Push(p, pair) deheap.Push(p, pair)
} }
size := pair.Src.Size() size := pair.Src.Size()
if size > 0 { if size > 0 {
@ -129,7 +126,7 @@ func (p *pipe) Get(ctx context.Context) (pair fs.ObjectPair, ok bool) {
p.queue[0] = fs.ObjectPair{} // avoid memory leak p.queue[0] = fs.ObjectPair{} // avoid memory leak
p.queue = p.queue[1:] p.queue = p.queue[1:]
} else { } else {
pair = heap.Pop(p).(fs.ObjectPair) pair = deheap.Pop(p).(fs.ObjectPair)
} }
size := pair.Src.Size() size := pair.Src.Size()
if size > 0 { if size > 0 {

View file

@ -1,6 +1,7 @@
package sync package sync
import ( import (
"container/heap"
"context" "context"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -12,6 +13,9 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// Check interface satisfied
var _ heap.Interface = (*pipe)(nil)
func TestPipe(t *testing.T) { func TestPipe(t *testing.T) {
var queueLength int var queueLength int
var queueSize int64 var queueSize int64