@@ -37,8 +37,7 @@ void ColumnFixedString::Append(std::string_view str) {
37
37
+ std::to_string (str.size ()) + " bytes." );
38
38
}
39
39
40
- if (data_.capacity () - data_.size () < str.size ())
41
- {
40
+ if (data_.capacity () - data_.size () < str.size ()) {
42
41
// round up to the next block size
43
42
const auto new_size = (((data_.size () + string_size_) / DEFAULT_BLOCK_SIZE) + 1 ) * DEFAULT_BLOCK_SIZE;
44
43
data_.reserve (new_size);
@@ -129,13 +128,11 @@ struct ColumnString::Block
129
128
data_(new CharT[capacity])
130
129
{}
131
130
132
- inline auto GetAvailable () const
133
- {
131
+ inline auto GetAvailable () const {
134
132
return capacity - size;
135
133
}
136
134
137
- std::string_view AppendUnsafe (std::string_view str)
138
- {
135
+ std::string_view AppendUnsafe (std::string_view str) {
139
136
const auto pos = &data_[size];
140
137
141
138
memcpy (pos, str.data (), str.size ());
@@ -144,13 +141,11 @@ struct ColumnString::Block
144
141
return std::string_view (pos, str.size ());
145
142
}
146
143
147
- auto GetCurrentWritePos ()
148
- {
144
+ auto GetCurrentWritePos () {
149
145
return &data_[size];
150
146
}
151
147
152
- std::string_view ConsumeTailAsStringViewUnsafe (size_t len)
153
- {
148
+ std::string_view ConsumeTailAsStringViewUnsafe (size_t len) {
154
149
const auto start = &data_[size];
155
150
size += len;
156
151
return std::string_view (start, len);
@@ -170,7 +165,8 @@ ColumnString::ColumnString(size_t element_count)
170
165
: Column(Type::CreateString())
171
166
{
172
167
items_.reserve (element_count);
173
- blocks_.reserve (element_count / 2 );
168
+ // 100 is arbitrary number, assumption that string values are about ~40 bytes long.
169
+ blocks_.reserve (std::max<size_t >(1 , element_count / 100 ));
174
170
}
175
171
176
172
ColumnString::ColumnString (const std::vector<std::string>& data)
@@ -179,8 +175,7 @@ ColumnString::ColumnString(const std::vector<std::string>& data)
179
175
items_.reserve (data.size ());
180
176
blocks_.emplace_back (ComputeTotalSize (data));
181
177
182
- for (const auto & s : data)
183
- {
178
+ for (const auto & s : data) {
184
179
AppendUnsafe (s);
185
180
}
186
181
};
@@ -201,21 +196,15 @@ ColumnString::~ColumnString()
201
196
{}
202
197
203
198
void ColumnString::Append (std::string_view str) {
204
- if (blocks_.size () == 0 || blocks_.back ().GetAvailable () < str.length ())
205
- {
199
+ if (blocks_.size () == 0 || blocks_.back ().GetAvailable () < str.length ()) {
206
200
blocks_.emplace_back (std::max (DEFAULT_BLOCK_SIZE, str.size ()));
207
201
}
208
202
209
203
items_.emplace_back (blocks_.back ().AppendUnsafe (str));
210
204
}
211
205
212
206
void ColumnString::Append (const char * str) {
213
- auto len = strlen (str);
214
- if (blocks_.size () == 0 || blocks_.back ().GetAvailable () < len) {
215
- blocks_.emplace_back (std::max (DEFAULT_BLOCK_SIZE, len));
216
- }
217
-
218
- items_.emplace_back (blocks_.back ().AppendUnsafe (str));
207
+ Append (std::string_view (str, strlen (str)));
219
208
}
220
209
221
210
void ColumnString::Append (std::string&& steal_value) {
@@ -298,14 +287,14 @@ size_t ColumnString::Size() const {
298
287
}
299
288
300
289
ColumnRef ColumnString::Slice (size_t begin, size_t len) const {
301
- auto result = std::make_shared<ColumnString>(len );
290
+ auto result = std::make_shared<ColumnString>();
302
291
303
292
if (begin < items_.size ()) {
304
293
len = std::min (len, items_.size () - begin);
294
+ result->items_ .reserve (len);
305
295
306
296
result->blocks_ .emplace_back (ComputeTotalSize (items_, begin, len));
307
- for (size_t i = begin; i < begin + len; ++i)
308
- {
297
+ for (size_t i = begin; i < begin + len; ++i) {
309
298
result->Append (items_[i]);
310
299
}
311
300
}
0 commit comments