Browse Source

Simplify the threshold check in RBTree allocator

Signed-off-by: Vadim Markovtsev <vadim@sourced.tech>
Vadim Markovtsev 6 năm trước cách đây
mục cha
commit
37d3d7724b
2 tập tin đã thay đổi với 16 bổ sung1 xóa
  1. 1 1
      internal/rbtree/rbtree.go
  2. 15 0
      internal/rbtree/rbtree_test.go

+ 1 - 1
internal/rbtree/rbtree.go

@@ -64,7 +64,7 @@ func (allocator *Allocator) Clone() *Allocator {
 
 // Hibernate compresses the allocated memory.
 func (allocator *Allocator) Hibernate() {
-	if allocator.HibernationThreshold > 0 && len(allocator.storage) < allocator.HibernationThreshold {
+	if len(allocator.storage) < allocator.HibernationThreshold {
 		return
 	}
 	allocator.hibernatedLen = len(allocator.storage)

+ 15 - 0
internal/rbtree/rbtree_test.go

@@ -419,3 +419,18 @@ func TestAllocatorHibernateBootEmpty(t *testing.T) {
 	assert.Equal(t, alloc.Size(), 0)
 	assert.Equal(t, alloc.Used(), 0)
 }
+
+func TestAllocatorHibernateBootThreshold(t *testing.T) {
+	alloc := NewAllocator()
+	alloc.malloc()
+	alloc.HibernationThreshold = 3
+	alloc.Hibernate()
+	assert.Equal(t, alloc.hibernatedLen, 0)
+	alloc.Boot()
+	alloc.malloc()
+	alloc.Hibernate()
+	assert.Equal(t, alloc.hibernatedLen, 3)
+	alloc.Boot()
+	assert.Equal(t, alloc.Size(), 3)
+	assert.Equal(t, alloc.Used(), 3)
+}