Skip to content

Commit 4fff2c8

Browse files
committed
changed how initialization works for sketching
1 parent 80ff063 commit 4fff2c8

3 files changed

Lines changed: 50 additions & 11 deletions

File tree

include/sketch/sketch_columns.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class FixedSizeSketchColumn {
4646
//no-op
4747
};
4848

49+
inline bool is_initialized() const {
50+
return buckets != nullptr;
51+
}
52+
4953
[[deprecated]]
5054
void zero_contents() {
5155
clear();
@@ -123,6 +127,10 @@ class ResizeableSketchColumn {
123127
return os;
124128
}
125129

130+
inline bool is_initialized() const {
131+
return buckets != nullptr;
132+
}
133+
126134
bool operator==(const ResizeableSketchColumn &other) const {
127135
size_t other_depth = other.get_depth();
128136
if (get_depth() != other_depth) {
@@ -148,7 +156,7 @@ class ResizeableAlignedSketchColumn {
148156
uint8_t capacity;
149157
public:
150158
void set_seed(uint64_t new_seed) { seed = new_seed; };
151-
const uint64_t get_seed() { return seed; };
159+
uint64_t get_seed() const { return seed; };
152160

153161
ResizeableAlignedSketchColumn(uint8_t start_capacity, uint64_t seed);
154162
ResizeableAlignedSketchColumn(const ResizeableAlignedSketchColumn &other);
@@ -166,6 +174,10 @@ class ResizeableAlignedSketchColumn {
166174
clear();
167175
}
168176

177+
inline bool is_initialized() const {
178+
return aligned_buckets != nullptr;
179+
}
180+
169181
void reset_sample_state() {
170182
//no-op
171183
};

include/sketch/sketch_concept.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ concept SketchColumnConcept = requires(T t, T other) {
4040
{ t.sample() } -> std::same_as<SketchSample<V>>;
4141
{ t.update(std::declval<V>()) } -> std::same_as<void>;
4242
{ t.merge(other) } -> std::same_as<void>;
43+
44+
{ t.is_initialized() } -> std::same_as<bool>;
4345

4446
{ t.clear()} -> std::same_as<void>;
4547
{ t.zero_contents()} -> std::same_as<void>;

src/sketch_columns.cpp

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,31 @@
22

33
FixedSizeSketchColumn::FixedSizeSketchColumn(uint8_t capacity, uint64_t seed) :
44
capacity(capacity), seed(seed) {
5-
buckets = new Bucket[capacity];
6-
std::memset(buckets, 0, capacity * sizeof(Bucket));
5+
likely_if (capacity > 0) {
6+
buckets = new Bucket[capacity];
7+
std::memset(buckets, 0, capacity * sizeof(Bucket));
8+
}
9+
else {
10+
buckets = nullptr;
11+
}
712
}
813

914
FixedSizeSketchColumn::FixedSizeSketchColumn(const FixedSizeSketchColumn &other) :
1015
capacity(other.capacity), seed(other.seed), deterministic_bucket(other.deterministic_bucket) {
11-
buckets = new Bucket[capacity];
12-
std::memcpy(buckets, other.buckets, capacity * sizeof(Bucket));
16+
likely_if (capacity > 0) {
17+
buckets = new Bucket[capacity];
18+
std::memcpy(buckets, other.buckets, capacity * sizeof(Bucket));
19+
}
20+
else {
21+
buckets = nullptr;
22+
}
1323
}
1424

1525
FixedSizeSketchColumn::FixedSizeSketchColumn(FixedSizeSketchColumn &&other) :
1626
capacity(other.capacity), seed(other.seed), deterministic_bucket(other.deterministic_bucket) {
17-
buckets = std::move(other.buckets);
27+
buckets = other.buckets;
28+
other.buckets = nullptr;
29+
other.capacity = 0;
1830
}
1931

2032
FixedSizeSketchColumn& FixedSizeSketchColumn::operator=(FixedSizeSketchColumn &&other) {
@@ -26,6 +38,7 @@ FixedSizeSketchColumn& FixedSizeSketchColumn::operator=(FixedSizeSketchColumn &&
2638

2739
buckets = other.buckets;
2840
other.buckets = nullptr;
41+
other.capacity = 0;
2942
}
3043
return *this;
3144
}
@@ -84,24 +97,35 @@ void FixedSizeSketchColumn::update(const vec_t update) {
8497
deterministic_bucket ^= {update, checksum};
8598
}
8699

87-
88-
ResizeableSketchColumn::ResizeableSketchColumn(uint8_t start_capacity, uint64_t seed) :
89-
capacity(start_capacity), seed(seed) {
100+
ResizeableSketchColumn::ResizeableSketchColumn(uint8_t start_capacity,
101+
uint64_t seed)
102+
: capacity(start_capacity), seed(seed) {
103+
likely_if(capacity > 0) {
90104
buckets = new Bucket[start_capacity];
91105
std::memset(buckets, 0, capacity * sizeof(Bucket));
106+
}
107+
else {
108+
buckets = nullptr;
109+
}
92110
}
93111

94112
ResizeableSketchColumn::ResizeableSketchColumn(const ResizeableSketchColumn &other) :
95113
capacity(other.capacity), seed(other.seed), deterministic_bucket(other.deterministic_bucket) {
96-
buckets = new Bucket[capacity];
97-
std::memcpy(buckets, other.buckets, capacity * sizeof(Bucket));
114+
likely_if(capacity > 0) {
115+
buckets = new Bucket[capacity];
116+
std::memcpy(buckets, other.buckets, capacity * sizeof(Bucket));
117+
}
118+
else {
119+
buckets = nullptr;
120+
}
98121
}
99122

100123
ResizeableSketchColumn::ResizeableSketchColumn(ResizeableSketchColumn &&other) :
101124
capacity(other.capacity), seed(other.seed), deterministic_bucket(other.deterministic_bucket) {
102125
// move constructor
103126
buckets = other.buckets;
104127
other.buckets = nullptr;
128+
other.capacity = 0;
105129
}
106130

107131
ResizeableSketchColumn& ResizeableSketchColumn::operator=(ResizeableSketchColumn &&other) {
@@ -113,6 +137,7 @@ ResizeableSketchColumn& ResizeableSketchColumn::operator=(ResizeableSketchColumn
113137

114138
buckets = other.buckets;
115139
other.buckets = nullptr;
140+
other.capacity = 0;
116141
}
117142
return *this;
118143
}

0 commit comments

Comments
 (0)