Skip to content

Commit 6c11903

Browse files
committed
sketch shrinking
1 parent dee1d8b commit 6c11903

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/sketch_columns.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,15 @@ void ResizeableSketchColumn::merge(ResizeableSketchColumn const& other) {
391391
for (size_t i = 0; i < other.capacity; ++i) {
392392
buckets[i] ^= other.buckets[i];
393393
}
394+
395+
// Shrink after merge when the resulting column is shallower.
396+
// Keep only a small safety margin above observed depth.
397+
constexpr size_t merge_slack = 1;
398+
size_t depth = get_depth();
399+
size_t target_capacity = std::max<size_t>(1, depth + merge_slack);
400+
if (target_capacity < capacity) {
401+
reallocate(static_cast<uint8_t>(target_capacity));
402+
}
394403
}
395404

396405
uint8_t ResizeableSketchColumn::get_depth() const {

test/sketch_test.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,10 @@ TEST(SketchColumnTestSuite, TestMergeResizing) {
575575
}
576576
ASSERT_EQ(column1.capacity, 10);
577577
column1.merge(column2);
578-
ASSERT_EQ(column1.capacity, column2.capacity);
578+
ASSERT_LE(column1.capacity, column2.capacity);
579+
ASSERT_GE(column1.capacity, 1);
580+
auto sample = column1.sample();
581+
ASSERT_EQ(sample.result, GOOD);
579582
}
580583

581584
// as we insert more unique items, maximum nonzero bucket depth should never decrease
@@ -606,7 +609,7 @@ TEST(SketchColumnTestSuite, TestClear) {
606609
auto sample = column.sample();
607610
ASSERT_EQ(sample.result, GOOD);
608611
column.clear();
609-
for (size_t i = 0; i < capacity; i++) {
612+
for (size_t i = 0; i < column.capacity; i++) {
610613
bool good = Bucket_Boruvka::is_empty(column.buckets[i]);
611614
ASSERT_EQ(good, true);
612615
}
@@ -624,7 +627,7 @@ TEST(SketchColumnTestSuite, TestClearMerge) {
624627
}
625628
column1.merge(column2);
626629
column2.clear();
627-
for (size_t i = 0; i < capacity; i++) {
630+
for (size_t i = 0; i < column2.capacity; i++) {
628631
bool good = Bucket_Boruvka::is_empty(column2.buckets[i]);
629632
ASSERT_EQ(good, true);
630633
}
@@ -640,7 +643,7 @@ TEST(SketchColumnTestSuite, TestClearMerge) {
640643
}
641644
column3.merge(column4);
642645
column3.clear();
643-
for (size_t i = 0; i < capacity; i++) {
646+
for (size_t i = 0; i < column3.capacity; i++) {
644647
bool empty = Bucket_Boruvka::is_empty(column3.buckets[i]);
645648
ASSERT_EQ(empty, true);
646649
}

0 commit comments

Comments
 (0)