diff --git a/cpp/core/jni/JniWrapper.cc b/cpp/core/jni/JniWrapper.cc index bfab41e3652..adada15f91d 100644 --- a/cpp/core/jni/JniWrapper.cc +++ b/cpp/core/jni/JniWrapper.cc @@ -490,6 +490,7 @@ Java_org_apache_gluten_vectorized_PlanEvaluatorJniWrapper_nativeCreateKernelWith // Handle the Java iters jsize itersLen = env->GetArrayLength(iterArr); std::vector> inputIters; + inputIters.reserve(itersLen); for (int idx = 0; idx < itersLen; idx++) { jobject iter = env->GetObjectArrayElement(iterArr, idx); auto arrayIter = std::make_unique(env, iter, ctx, idx); @@ -835,6 +836,7 @@ JNIEXPORT jlong JNICALL Java_org_apache_gluten_columnarbatch_ColumnarBatchJniWra auto safeArray = getIntArrayElementsSafe(env, jcolumnIndices); int size = env->GetArrayLength(jcolumnIndices); std::vector columnIndices; + columnIndices.reserve(size); for (int32_t i = 0; i < size; i++) { columnIndices.push_back(safeArray.elems()[i]); } diff --git a/cpp/core/shuffle/Utils.cc b/cpp/core/shuffle/Utils.cc index 51667274edd..d2c9c6fe6d7 100644 --- a/cpp/core/shuffle/Utils.cc +++ b/cpp/core/shuffle/Utils.cc @@ -190,6 +190,7 @@ arrow::Result gluten::createTempShuffleFile(const std::string& dir) arrow::Result>> gluten::toShuffleTypeId( const std::vector>& fields) { std::vector> shuffleTypeId; + shuffleTypeId.reserve(fields.size()); for (auto field : fields) { switch (field->type()->id()) { case arrow::BooleanType::type_id: diff --git a/cpp/velox/benchmarks/GenericBenchmark.cc b/cpp/velox/benchmarks/GenericBenchmark.cc index 811ca826979..21212541371 100644 --- a/cpp/velox/benchmarks/GenericBenchmark.cc +++ b/cpp/velox/benchmarks/GenericBenchmark.cc @@ -402,6 +402,7 @@ auto BM_Generic = [](::benchmark::State& state, auto plan = getPlanFromFile("Plan", planFile); std::vector splits{}; + splits.reserve(splitFiles.size()); for (const auto& splitFile : splitFiles) { splits.push_back(getPlanFromFile("ReadRel.LocalFiles", splitFile)); } @@ -428,6 +429,7 @@ auto BM_Generic = [](::benchmark::State& state, ScopedTimer timer(&elapsedTime); for (auto _ : state) { std::vector> inputIters; + inputIters.reserve(dataFiles.size()); std::vector inputItersRaw; if (!dataFiles.empty()) { for (const auto& input : dataFiles) { diff --git a/cpp/velox/compute/iceberg/IcebergWriter.cc b/cpp/velox/compute/iceberg/IcebergWriter.cc index 0e671f1773b..2e9aa866050 100644 --- a/cpp/velox/compute/iceberg/IcebergWriter.cc +++ b/cpp/velox/compute/iceberg/IcebergWriter.cc @@ -54,6 +54,7 @@ std::shared_ptr createIcebergInsertTableHandle( std::vector> columnHandles; std::vector columnNames = outputRowType->names(); + columnHandles.reserve(columnNames.size()); std::vector columnTypes = outputRowType->children(); std::vector partitionColumns; partitionColumns.reserve(spec->fields.size()); diff --git a/cpp/velox/jni/JniFileSystem.cc b/cpp/velox/jni/JniFileSystem.cc index 024d745b2bf..a8b7bf3fdbe 100644 --- a/cpp/velox/jni/JniFileSystem.cc +++ b/cpp/velox/jni/JniFileSystem.cc @@ -347,6 +347,7 @@ class JniFileSystem : public facebook::velox::filesystems::FileSystem { static_cast(env->CallObjectMethod(obj_, jniFileSystemList, createJString(env, path))); checkException(env); jsize length = env->GetArrayLength(jarray); + out.reserve(length); for (jsize i = 0; i < length; ++i) { jstring element = static_cast(env->GetObjectArrayElement(jarray, i)); std::string cElement = jStringToCString(env, element); diff --git a/cpp/velox/jni/VeloxJniWrapper.cc b/cpp/velox/jni/VeloxJniWrapper.cc index f44fed08882..a0889cf5a95 100644 --- a/cpp/velox/jni/VeloxJniWrapper.cc +++ b/cpp/velox/jni/VeloxJniWrapper.cc @@ -262,6 +262,7 @@ JNIEXPORT jlong JNICALL Java_org_apache_gluten_columnarbatch_VeloxColumnarBatchJ auto safeArray = getLongArrayElementsSafe(env, batchHandles); std::vector> batches; + batches.reserve(handleCount); for (int i = 0; i < handleCount; ++i) { int64_t handle = safeArray.elems()[i]; auto batch = ObjectStore::retrieve(handle); diff --git a/cpp/velox/shuffle/VeloxHashShuffleWriter.cc b/cpp/velox/shuffle/VeloxHashShuffleWriter.cc index e83ce8a566a..6a1298d743e 100644 --- a/cpp/velox/shuffle/VeloxHashShuffleWriter.cc +++ b/cpp/velox/shuffle/VeloxHashShuffleWriter.cc @@ -277,6 +277,7 @@ arrow::Status VeloxHashShuffleWriter::write(std::shared_ptr cb, i } END_TIMING(); std::vector range; + range.reserve(numColumns); for (int32_t i = 1; i < numColumns; i++) { range.push_back(i); } @@ -377,6 +378,7 @@ arrow::Status VeloxHashShuffleWriter::buildPartition2Row(uint32_t rowNum) { // calc valid partition list partitionUsed_.clear(); + partitionUsed_.reserve(numPartitions_); for (auto pid = 0; pid != numPartitions_; ++pid) { if (partition2RowCount_[pid] > 0) { partitionUsed_.push_back(pid); @@ -737,6 +739,7 @@ arrow::Status VeloxHashShuffleWriter::splitComplexType(const facebook::velox::Ro arrow::Status VeloxHashShuffleWriter::initColumnTypes(const facebook::velox::RowVector& rv) { schema_ = toArrowSchema(rv.type(), veloxPool_.get()); + veloxColumnTypes_.reserve(rv.childrenSize()); for (size_t i = 0; i < rv.childrenSize(); ++i) { veloxColumnTypes_.push_back(rv.childAt(i)->type()); } diff --git a/cpp/velox/shuffle/VeloxRssSortShuffleWriter.cc b/cpp/velox/shuffle/VeloxRssSortShuffleWriter.cc index c93045c1b58..54e25397203 100644 --- a/cpp/velox/shuffle/VeloxRssSortShuffleWriter.cc +++ b/cpp/velox/shuffle/VeloxRssSortShuffleWriter.cc @@ -84,6 +84,7 @@ arrow::Status VeloxRssSortShuffleWriter::write(std::shared_ptr cb RETURN_NOT_OK(partitioner_->compute(pidArr, pidBatch->numRows(), batches_.size(), rowVectorIndexMap_)); END_TIMING(); std::vector range; + range.reserve(numColumns); for (int32_t i = 1; i < numColumns; i++) { range.push_back(i); } diff --git a/cpp/velox/shuffle/VeloxSortShuffleWriter.cc b/cpp/velox/shuffle/VeloxSortShuffleWriter.cc index eb5bb89cafe..7a4066153b0 100644 --- a/cpp/velox/shuffle/VeloxSortShuffleWriter.cc +++ b/cpp/velox/shuffle/VeloxSortShuffleWriter.cc @@ -138,6 +138,7 @@ arrow::Result VeloxSortShuffleWriter::getPeeledRo RETURN_NOT_OK(partitioner_->compute(pidArr, pidBatch->numRows(), row2Partition_)); std::vector range; + range.reserve(numColumns); for (int32_t i = 1; i < numColumns; i++) { range.push_back(i); } diff --git a/cpp/velox/substrait/SubstraitToVeloxPlan.cc b/cpp/velox/substrait/SubstraitToVeloxPlan.cc index eb10a67e851..e93ea75ba87 100644 --- a/cpp/velox/substrait/SubstraitToVeloxPlan.cc +++ b/cpp/velox/substrait/SubstraitToVeloxPlan.cc @@ -674,6 +674,7 @@ std::shared_ptr makeCudfHiveInsertTableHandle( const std::unordered_map& serdeParameters, const std::shared_ptr& writerOptions) { std::vector> columnHandles; + columnHandles.reserve(tableColumnNames.size()); for (int i = 0; i < tableColumnNames.size(); ++i) { columnHandles.push_back(std::make_shared( diff --git a/cpp/velox/tests/MemoryManagerTest.cc b/cpp/velox/tests/MemoryManagerTest.cc index 8dd41aec7df..1367e2e0483 100644 --- a/cpp/velox/tests/MemoryManagerTest.cc +++ b/cpp/velox/tests/MemoryManagerTest.cc @@ -78,6 +78,7 @@ TEST_F(MemoryManagerTest, memoryPoolWithBlockReseravtion) { std::vector allocations; std::vector sizes{ kMemoryReservationBlockSizeDefault - 1 * kMB, kMemoryReservationBlockSizeDefault - 2 * kMB}; + allocations.reserve(sizes.size()); for (const auto& size : sizes) { auto buf = pool->allocate(size); allocations.push_back({buf, size, pool.get()}); @@ -100,6 +101,7 @@ TEST_F(MemoryManagerTest, memoryAllocatorWithBlockReservation) { std::vector allocations; std::vector sizes{ kMemoryReservationBlockSizeDefault - 1 * kMB, kMemoryReservationBlockSizeDefault - 2 * kMB}; + allocations.reserve(sizes.size()); for (auto i = 0; i < sizes.size(); i++) { auto size = sizes[i]; auto currentBytes = allocator_->getBytes(); @@ -353,6 +355,7 @@ TEST_F(MultiMemoryManagerTest, spill) { std::vector> vmms{}; std::vector threads{}; std::vector> buffs{}; + buffs.reserve(numThreads); for (size_t i = 0; i < numThreads; ++i) { buffs.push_back({}); vmms.emplace_back(nullptr); diff --git a/cpp/velox/tests/VeloxBatchResizerTest.cc b/cpp/velox/tests/VeloxBatchResizerTest.cc index f1157991e54..f5333f8b625 100644 --- a/cpp/velox/tests/VeloxBatchResizerTest.cc +++ b/cpp/velox/tests/VeloxBatchResizerTest.cc @@ -55,6 +55,7 @@ class VeloxBatchResizerTest : public ::testing::Test, public test::VectorTestBas void checkResize(int32_t min, int32_t max, int64_t preferredBatchBytes, std::vector inSizes, std::vector outSizes) { auto inBatches = std::vector>(); + inBatches.reserve(inSizes.size()); for (const auto& size : inSizes) { inBatches.push_back(std::make_shared(newVector(size))); } diff --git a/cpp/velox/tests/VeloxSubstraitRoundTripTest.cc b/cpp/velox/tests/VeloxSubstraitRoundTripTest.cc index cec61afd075..7b8e8336ea9 100644 --- a/cpp/velox/tests/VeloxSubstraitRoundTripTest.cc +++ b/cpp/velox/tests/VeloxSubstraitRoundTripTest.cc @@ -47,6 +47,7 @@ class VeloxSubstraitRoundTripTest : public OperatorTestBase { /// @param batchSize The batch Size of the data. std::vector makeVectors(int64_t size, int64_t childSize, int64_t batchSize) { std::vector vectors; + vectors.reserve(size); std::mt19937 gen(std::mt19937::default_seed); for (int i = 0; i < size; i++) { std::vector children;