This is an automated email from the ASF dual-hosted git repository.
zouxinyi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 123c649328c [fix](memory) Fix `PODArray::add_num_element` (#50756)
123c649328c is described below
commit 123c649328cb84f6394db29a149b11fdc5809c9b
Author: Xinyi Zou <[email protected]>
AuthorDate: Sat May 10 15:23:39 2025 +0800
[fix](memory) Fix `PODArray::add_num_element` (#50756)
### What problem does this PR solve?
In `add_num_element`, after `reserve`, the value of `c_end` will be
changed.
---
be/src/vec/common/pod_array.h | 6 +++---
be/test/vec/common/pod_array_test.cpp | 26 ++++++++++++++++++++++++++
2 files changed, 29 insertions(+), 3 deletions(-)
diff --git a/be/src/vec/common/pod_array.h b/be/src/vec/common/pod_array.h
index df379b5f54f..a8abce8385f 100644
--- a/be/src/vec/common/pod_array.h
+++ b/be/src/vec/common/pod_array.h
@@ -413,12 +413,12 @@ public:
template <typename U, typename... TAllocatorParams>
void add_num_element(U&& x, uint32_t num, TAllocatorParams&&...
allocator_params) {
if (num != 0) {
- const auto new_end = this->c_end + this->byte_size(num);
- if (UNLIKELY(new_end > this->c_end_of_storage)) {
+ const auto growth_size = this->byte_size(num);
+ if (UNLIKELY(this->c_end + growth_size > this->c_end_of_storage)) {
this->reserve(this->size() + num);
}
std::fill(t_end(), t_end() + num, x);
- this->c_end = new_end;
+ this->c_end = this->c_end + growth_size;
}
}
diff --git a/be/test/vec/common/pod_array_test.cpp
b/be/test/vec/common/pod_array_test.cpp
index e8b0a67ffa0..db022a12729 100644
--- a/be/test/vec/common/pod_array_test.cpp
+++ b/be/test/vec/common/pod_array_test.cpp
@@ -498,6 +498,32 @@ TEST(PODArrayTest, PODArrayInsert) {
// }
// }
+TEST(PODArrayTest, PODArrayAddNumElement) {
+ static constexpr size_t initial_bytes = 32;
+ using Array = vectorized::PODArray<uint64_t, initial_bytes>;
+ size_t element_size = 8; // sizeof(uint64_t)
+ {
+ Array array;
+
+ array.add_num_element(1, 4);
+ ASSERT_EQ(array.size(), 4);
+ ASSERT_EQ(array.capacity(), 32 / element_size);
+ ASSERT_EQ(array, Array({1, 1, 1, 1}));
+
+ // call reserve
+ array.add_num_element(1, 2);
+ ASSERT_EQ(array.size(), 6);
+ ASSERT_EQ(array.capacity(), 64 / element_size);
+ ASSERT_EQ(array, Array({1, 1, 1, 1, 1, 1}));
+
+ // call reserve
+ array.add_num_element_without_reserve(1, 1);
+ ASSERT_EQ(array.size(), 7);
+ ASSERT_EQ(array.capacity(), 64 / element_size);
+ ASSERT_EQ(array, Array({1, 1, 1, 1, 1, 1, 1}));
+ }
+}
+
TEST(PODArrayTest, PODArrayAssign) {
{
vectorized::PaddedPODArray<uint64_t> array;
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]