Skip to content

Commit

Permalink
replace resize/reserve to resize_extact/reserve_exact (#4824)
Browse files Browse the repository at this point in the history
  • Loading branch information
taiyang-li authored Mar 4, 2024
1 parent 0f3a7ba commit 3156fcf
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cpp-ch/local-engine/Functions/SparkFunctionMakeDecimal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ namespace
const auto & vector = typeid_cast<const ColumnVector<FromFieldType> *>(arguments[0].column.get());
auto & vec_to = col_to->getData();
auto & datas = vector->getData();
vec_to.resize(input_rows_count);
vec_to.resize_exact(input_rows_count);

for (size_t i = 0; i < input_rows_count; ++i)
{
Expand Down
26 changes: 13 additions & 13 deletions cpp-ch/local-engine/Functions/SparkFunctionRegexpExtractAll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ namespace
const auto & match = matches[match_index];
if (match.offset != std::string::npos)
{
res_strings_chars.resize(res_strings_offset + match.length + 1);
res_strings_chars.resize_exact(res_strings_offset + match.length + 1);
memcpySmallAllowReadWriteOverflow15(&res_strings_chars[res_strings_offset], pos + match.offset, match.length);
res_strings_offset += match.length;
}
else
res_strings_chars.resize(res_strings_offset + 1);
res_strings_chars.resize_exact(res_strings_offset + 1);

/// Update offsets of Column:String
res_strings_chars[res_strings_offset] = 0;
Expand Down Expand Up @@ -211,9 +211,9 @@ namespace
OptimizedRegularExpression::MatchVec matches;
matches.reserve(index + 1);

res_offsets.reserve(offsets.size());
res_strings_chars.reserve(data.size() / 3);
res_strings_offsets.reserve(offsets.size() * 2);
res_offsets.reserve_exact(offsets.size());
res_strings_chars.reserve_exact(data.size() / 3);
res_strings_offsets.reserve_exact(offsets.size() * 2);

size_t res_offset = 0;
size_t res_strings_offset = 0;
Expand Down Expand Up @@ -253,9 +253,9 @@ namespace
OptimizedRegularExpression::MatchVec matches;
matches.reserve(capture + 1);

res_offsets.reserve(offsets.size());
res_strings_chars.reserve(data.size() / 3);
res_strings_offsets.reserve(offsets.size() * 2);
res_offsets.reserve_exact(offsets.size());
res_strings_chars.reserve_exact(data.size() / 3);
res_strings_offsets.reserve_exact(offsets.size() * 2);

size_t res_offset = 0;
size_t res_strings_offset = 0;
Expand Down Expand Up @@ -333,9 +333,9 @@ namespace
}

size_t rows = column_index->size();
res_offsets.reserve(rows);
res_strings_chars.reserve(rows * str.size() / 3);
res_strings_offsets.reserve(rows * 2);
res_offsets.reserve_exact(rows);
res_strings_chars.reserve_exact(rows * str.size() / 3);
res_strings_offsets.reserve_exact(rows * 2);

size_t res_offset = 0;
size_t res_strings_offset = 0;
Expand All @@ -356,12 +356,12 @@ namespace
/// Append matched segment into res_strings_chars
if (match.offset != std::string::npos)
{
res_strings_chars.resize(res_strings_offset + match.length + 1);
res_strings_chars.resize_exact(res_strings_offset + match.length + 1);
memcpySmallAllowReadWriteOverflow15(&res_strings_chars[res_strings_offset], start + match.offset, match.length);
res_strings_offset += match.length;
}
else
res_strings_chars.resize(res_strings_offset + 1);
res_strings_chars.resize_exact(res_strings_offset + 1);

/// Update offsets of Column:String
res_strings_chars[res_strings_offset] = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace
size_t rows = src.size();
ColumnString::Chars & data_to = dst_concrete->getChars();
ColumnString::Offsets & offsets_to = dst_concrete->getOffsets();
offsets_to.resize(rows);
offsets_to.resize_exact(rows);

ColumnString::Offset offset = 0;
for (size_t i = 0; i < rows; ++i)
Expand Down
2 changes: 1 addition & 1 deletion cpp-ch/local-engine/Functions/SparkFunctionRoundHalfUp.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ struct DispatcherRoundingHalfUp
auto col_res = ColumnVector<T>::create();

typename ColumnVector<T>::Container & vec_res = col_res->getData();
vec_res.resize(col->getData().size());
vec_res.resize_exact(col->getData().size());

if (!vec_res.empty())
{
Expand Down
6 changes: 3 additions & 3 deletions cpp-ch/local-engine/Functions/SparkFunctionTrim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ namespace
ColumnString::Offsets & res_offsets,
const String & trim_str) const
{
res_data.reserve(data.size());
res_data.reserve_exact(data.size());

size_t rows = offsets.size();
res_offsets.resize(rows);
res_offsets.resize_exact(rows);

size_t prev_offset = 0;
size_t res_offset = 0;
Expand All @@ -149,7 +149,7 @@ namespace
for (size_t i = 0; i < rows; ++i)
{
trim(reinterpret_cast<const UInt8 *>(&data[prev_offset]), offsets[i] - prev_offset - 1, start, length, trim_set);
res_data.resize(res_data.size() + length + 1);
res_data.resize_exact(res_data.size() + length + 1);
memcpySmallAllowReadWriteOverflow15(&res_data[res_offset], start, length);
res_offset += length + 1;
res_data[res_offset - 1] = '\0';
Expand Down
14 changes: 7 additions & 7 deletions cpp-ch/local-engine/Functions/SparkParseURL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ struct ExtractNullableSubstringImpl
DB::ColumnString::Chars & res_data, DB::ColumnString::Offsets & res_offsets, DB::IColumn & null_map)
{
size_t size = offsets.size();
res_offsets.resize(size);
res_data.reserve(size * Extractor::getReserveLengthForElement());
res_offsets.resize_exact(size);
res_data.reserve_exact(size * Extractor::getReserveLengthForElement());
null_map.reserve(size);

size_t prev_offset = 0;
Expand All @@ -60,7 +60,7 @@ struct ExtractNullableSubstringImpl
{
Extractor::execute(reinterpret_cast<const char *>(&data[prev_offset]), offsets[i] - prev_offset - 1, start, length);

res_data.resize(res_data.size() + length + 1);
res_data.resize_exact(res_data.size() + length + 1);
if (start)
{
memcpySmallAllowReadWriteOverflow15(&res_data[res_offset], start, length);
Expand Down Expand Up @@ -261,8 +261,8 @@ struct SparkExtractURLOneQuery
DB::ColumnString::Chars & res_data, DB::ColumnString::Offsets & res_offsets, DB::IColumn & null_map)
{
const static String protocol_delim = "://";
res_data.reserve(data.size() / 5);
res_offsets.resize(offsets.size());
res_data.reserve_exact(data.size() / 5);
res_offsets.resize_exact(offsets.size());

pattern += '=';
const char * param_str = pattern.c_str();
Expand Down Expand Up @@ -332,15 +332,15 @@ struct SparkExtractURLOneQuery

size_t param_size = param_end - param_begin;

res_data.resize(res_offset + param_size + 1);
res_data.resize_exact(res_offset + param_size + 1);
memcpySmallAllowReadWriteOverflow15(&res_data[res_offset], param_begin, param_size);
res_offset += param_size;
null_map.insert(0);
}
else
{
/// No parameter found, put empty string in result.
res_data.resize(res_offset + 1);
res_data.resize_exact(res_offset + 1);
null_map.insert(1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ DB::ColumnWithTypeAndName convertAggregateStateToFixedString(const DB::ColumnWit
auto res_type = std::make_shared<DataTypeFixedString>(state_size);
auto res_col = res_type->createColumn();
PaddedPODArray<UInt8> & column_chars_t = assert_cast<ColumnFixedString &>(*res_col).getChars();
column_chars_t.reserve(aggregate_col->size() * state_size);
column_chars_t.reserve_exact(aggregate_col->size() * state_size);
for (const auto & item : aggregate_col->getData())
{
column_chars_t.insert_assume_reserved(item, item + state_size);
Expand All @@ -82,7 +82,7 @@ DB::ColumnWithTypeAndName convertAggregateStateToString(const DB::ColumnWithType
PaddedPODArray<UInt8> & column_chars = assert_cast<ColumnString &>(*res_col).getChars();
IColumn::Offsets & column_offsets = assert_cast<ColumnString &>(*res_col).getOffsets();
auto value_writer = WriteBufferFromVector<PaddedPODArray<UInt8>>(column_chars);
column_offsets.reserve(aggregate_col->size());
column_offsets.reserve_exact(aggregate_col->size());
for (const auto & item : aggregate_col->getData())
{
aggregate_col->getAggregateFunction()->serialize(item, value_writer);
Expand All @@ -100,7 +100,7 @@ DB::ColumnWithTypeAndName convertFixedStringToAggregateState(const DB::ColumnWit
ColumnAggregateFunction & real_column = typeid_cast<ColumnAggregateFunction &>(*res_col);
auto & arena = real_column.createOrGetArena();
ColumnAggregateFunction::Container & vec = real_column.getData();
vec.reserve(col.column->size());
vec.reserve_exact(col.column->size());
auto agg_function = agg_type->getFunction();
size_t size_of_state = agg_function->sizeOfData();
size_t align_of_state = agg_function->alignOfData();
Expand Down
2 changes: 1 addition & 1 deletion cpp-ch/local-engine/Storages/IO/CompressedWriteBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void CompressedWriteBuffer::nextImpl()
}
else
{
compressed_buffer.resize(compressed_reserve_size);
compressed_buffer.resize_exact(compressed_reserve_size);
UInt32 compressed_size = codec->compress(working_buffer.begin(), decompressed_size, compressed_buffer.data());
compress_time += compress_time_watch.elapsedNanoseconds();
CityHash_v1_0_2::uint128 checksum_(0,0);
Expand Down
4 changes: 2 additions & 2 deletions cpp-ch/local-engine/Storages/IO/NativeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static void readFixedSizeAggregateData(DB::ReadBuffer &in, DB::ColumnPtr & colum
auto & arena = real_column.createOrGetArena();
ColumnAggregateFunction::Container & vec = real_column.getData();
size_t initial_size = vec.size();
vec.reserve(initial_size + rows);
vec.reserve_exact(initial_size + rows);
for (size_t i = 0; i < rows; ++i)
{
AggregateDataPtr place = arena.alignedAlloc(column_parse_util.aggregate_state_size, column_parse_util.aggregate_state_align);
Expand All @@ -105,7 +105,7 @@ static void readVarSizeAggregateData(DB::ReadBuffer &in, DB::ColumnPtr & column,
auto & arena = real_column.createOrGetArena();
ColumnAggregateFunction::Container & vec = real_column.getData();
size_t initial_size = vec.size();
vec.reserve(initial_size + rows);
vec.reserve_exact(initial_size + rows);
for (size_t i = 0; i < rows; ++i)
{
AggregateDataPtr place = arena.alignedAlloc(column_parse_util.aggregate_state_size, column_parse_util.aggregate_state_align);
Expand Down
12 changes: 6 additions & 6 deletions cpp-ch/local-engine/tests/benchmark_local_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ DB::ContextMutablePtr global_context;
PaddedPODArray<Int32> arr;
PaddedPODArray<UInt8> condition;
PaddedPODArray<Int32> res_data;
arr.reserve(n);
condition.reserve(n);
res_data.reserve(n);
arr.reserve_exact(n);
condition.reserve_exact(n);
res_data.reserve_exact(n);
for (int i = 0; i < n; i++)
{
arr.push_back(i);
Expand Down Expand Up @@ -447,9 +447,9 @@ DB::ContextMutablePtr global_context;
PaddedPODArray<Int32> arr;
PaddedPODArray<UInt8> condition;
PaddedPODArray<Int32> res_data;
arr.reserve(n);
condition.reserve(n);
res_data.reserve(n);
arr.reserve_exact(n);
condition.reserve_exact(n);
res_data.reserve_exact(n);
for (int i = 0; i < n; i++)
{
arr.push_back(i);
Expand Down

0 comments on commit 3156fcf

Please sign in to comment.