From 0e62143fcf44e551508154803fcac4af46a5f56c Mon Sep 17 00:00:00 2001 From: sumeerbhola Date: Tue, 9 May 2023 23:08:38 -0400 Subject: [PATCH] db: intialize memTable.reserve when creating memTable Informs https://github.com/cockroachdb/cockroach/issues/102945 Fixes #2516 --- mem_table.go | 1 + mem_table_test.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/mem_table.go b/mem_table.go index 13f4329076..acc0b90b72 100644 --- a/mem_table.go +++ b/mem_table.go @@ -143,6 +143,7 @@ func newMemTable(opts memTableOptions) *memTable { m.skl.Reset(arena, m.cmp) m.rangeDelSkl.Reset(arena, m.cmp) m.rangeKeySkl.Reset(arena, m.cmp) + m.reserved = arena.Size() return m } diff --git a/mem_table_test.go b/mem_table_test.go index 5380c7cfa8..1c7fddd233 100644 --- a/mem_table_test.go +++ b/mem_table_test.go @@ -389,6 +389,21 @@ func TestMemTableConcurrentDeleteRange(t *testing.T) { } } +func TestMemTableReserved(t *testing.T) { + m := newMemTable(memTableOptions{size: 5000}) + // Increase to 2 references. + m.writerRef() + // The initial reservation accounts for the already allocated bytes from the + // arena. + require.Equal(t, m.reserved, m.skl.Arena().Size()) + b := newBatch(nil) + b.Set([]byte("blueberry"), []byte("pie"), nil) + require.NotEqual(t, 0, int(b.memTableSize)) + prevReserved := m.reserved + m.prepare(b) + require.Equal(t, int(m.reserved), int(b.memTableSize)+int(prevReserved)) +} + func buildMemTable(b *testing.B) (*memTable, [][]byte) { m := newMemTable(memTableOptions{}) var keys [][]byte