Skip to content

Commit cf96494

Browse files
authored
maintain base capacity after IterPop iteration (#44)
* maintain base capacity after IterPop iteration After iteration of the iterator returned from IterPopFront or IterPopBack, the internal buffer was resized using the default base capacity instead of the specified base capacity, if one was provided to the Deque. This issue does not cause any problems, but may lead to more allocations in cases where a larger than default base capacity was specified. This change uses the correct base capacity. * use const for constant values in tests
1 parent 9f53a9e commit cf96494

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

deque.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,11 +515,11 @@ func (q *Deque[T]) shrinkToFit() {
515515
if q.count == 0 {
516516
q.head = 0
517517
q.tail = 0
518-
q.buf = make([]T, minCapacity)
518+
q.buf = make([]T, q.minCap)
519519
return
520520
}
521521

522-
c := minCapacity
522+
c := q.minCap
523523
for c < q.count {
524524
c <<= 1
525525
}

deque_test.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func TestFrontBack(t *testing.T) {
8787

8888
func TestGrowShrinkBack(t *testing.T) {
8989
var q Deque[int]
90-
size := minCapacity * 2
90+
const size = minCapacity * 2
9191

9292
for i := 0; i < size; i++ {
9393
if q.Len() != i {
@@ -117,7 +117,7 @@ func TestGrowShrinkBack(t *testing.T) {
117117

118118
func TestGrowShrinkFront(t *testing.T) {
119119
var q Deque[int]
120-
size := minCapacity * 2
120+
const size = minCapacity * 2
121121

122122
for i := 0; i < size; i++ {
123123
if q.Len() != i {
@@ -286,7 +286,7 @@ func TestGrow(t *testing.T) {
286286
}
287287

288288
func TestNew(t *testing.T) {
289-
minCap := 64
289+
const minCap = 64
290290
q := &Deque[string]{}
291291
q.SetBaseCap(minCap)
292292
if q.Cap() != 0 {
@@ -752,8 +752,16 @@ func TestRIter(t *testing.T) {
752752
}
753753

754754
func TestIterPopBack(t *testing.T) {
755+
const (
756+
baseCap = 32
757+
size = baseCap * 5
758+
)
755759
var q Deque[int]
756-
size := minCapacity * 5
760+
q.SetBaseCap(baseCap)
761+
762+
for i := range q.IterPopBack() {
763+
t.Fatalf("popped %d when nothig to pop", i)
764+
}
757765

758766
q.Grow(size)
759767
for i := 0; i < size; i++ {
@@ -779,6 +787,9 @@ func TestIterPopBack(t *testing.T) {
779787
if q.Len() != 0 {
780788
t.Error("q.Len() =", q.Len(), "expected 0")
781789
}
790+
if q.Cap() != baseCap {
791+
t.Error("capacity should return to configured base capacity")
792+
}
782793

783794
q.Grow(size)
784795
for i := 0; i < size; i++ {
@@ -835,8 +846,12 @@ func TestIterPopBack(t *testing.T) {
835846
}
836847

837848
func TestIterPopFront(t *testing.T) {
849+
const (
850+
baseCap = 32
851+
size = baseCap * 5
852+
)
838853
var q Deque[int]
839-
size := minCapacity * 5
854+
q.SetBaseCap(baseCap)
840855

841856
q.Grow(size)
842857
for i := 0; i < size; i++ {
@@ -860,6 +875,10 @@ func TestIterPopFront(t *testing.T) {
860875
if q.Len() != 0 {
861876
t.Error("q.Len() =", q.Len(), "expected 0")
862877
}
878+
if q.Cap() != baseCap {
879+
t.Error("capacity should return to configured base capacity")
880+
}
881+
863882
for range q.IterPopFront() {
864883
t.Fatal("iteration with 0 items")
865884
}

0 commit comments

Comments
 (0)