Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions cpp/src/arrow/util/small_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ struct StaticVectorStorage : public StaticVectorStorageBase<T, N, D> {
using Base::size_;
using Base::static_data_;

static constexpr bool can_overflow = false;

StaticVectorStorage() noexcept = default;

constexpr storage_type* storage_ptr() { return static_data_; }
Expand Down Expand Up @@ -117,6 +119,8 @@ struct SmallVectorStorage {
storage_type* data_ = static_data_;
size_t dynamic_capacity_ = 0;

static constexpr bool can_overflow = true;

SmallVectorStorage() noexcept = default;

~SmallVectorStorage() { destroy(); }
Expand Down Expand Up @@ -163,9 +167,13 @@ struct SmallVectorStorage {
other.data_ = other.static_data_;
other.dynamic_capacity_ = 0;
other.size_ = 0;
} else if (size_ != 0) {
// Use a compile-time memcpy size (N) for trivial types
storage_type::move_construct_several(other.static_data_, static_data_, size_, N);
} else {
// Reset to static storage (in case we had dynamic storage before destroy())
data_ = static_data_;

@HyukjinKwon HyukjinKwon Feb 4, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a piggyback bug fix that is technically not related to this optimization. I found out while benchmarking. I can separate it out if anyone prefers it.

if (size_ != 0) {
// Use a compile-time memcpy size (N) for trivial types
storage_type::move_construct_several(other.static_data_, static_data_, size_, N);
}
}
}

Expand Down Expand Up @@ -238,9 +246,23 @@ class StaticVectorImpl {

StaticVectorImpl& operator=(StaticVectorImpl&& other) noexcept {
if (ARROW_PREDICT_TRUE(&other != this)) {
// TODO move_assign?
storage_.destroy();
storage_.move_construct(std::move(other.storage_));
// Determine whether to use fast path (destroy + move_construct):
// - For trivial types: always use fast path (memcpy is fast)
// - For dynamic storage: always use fast path (pointer swap is O(1))
// - For static storage + non-trivial types: use assign_by_moving (may reuse
// objects)
bool use_fast_path = std::is_trivial<T>::value;
if constexpr (Storage::can_overflow) {
use_fast_path = use_fast_path || storage_.dynamic_capacity_ ||
other.storage_.dynamic_capacity_;
}

if (use_fast_path) {
storage_.destroy();
storage_.move_construct(std::move(other.storage_));
} else {
assign_by_moving(other.storage_.size_, other.data());
}
}
return *this;
}
Expand Down
46 changes: 46 additions & 0 deletions cpp/src/arrow/util/small_vector_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -782,5 +782,51 @@ TEST(SmallVector, Traits) {
ASSERT_FALSE((std::is_trivially_destructible<SmallVector<int, 4>>::value));
}

TEST(SmallVector, MoveAssignDynamicToStatic) {
SmallVector<std::string, 8> src;
SmallVector<std::string, 8> dst;

// src: 3 elements (static storage)
src.push_back("a");
src.push_back("b");
src.push_back("c");

// dst: 12 elements (dynamic storage, uses heap)
for (int i = 0; i < 12; ++i) {
dst.push_back("item" + std::to_string(i));
}

// Move from dynamic to static storage
dst = std::move(src);

ASSERT_EQ(dst.size(), 3);
EXPECT_EQ(dst[0], "a");
EXPECT_EQ(dst[1], "b");
EXPECT_EQ(dst[2], "c");
}

TEST(SmallVector, MoveAssignStaticToDynamic) {
SmallVector<std::string, 8> src;
SmallVector<std::string, 8> dst;

// src: 12 elements (dynamic storage, uses heap)
for (int i = 0; i < 12; ++i) {
src.push_back("item" + std::to_string(i));
}

// dst: 3 elements (static storage)
dst.push_back("x");
dst.push_back("y");
dst.push_back("z");

// Move from static to dynamic storage
dst = std::move(src);

ASSERT_EQ(dst.size(), 12);
for (int i = 0; i < 12; ++i) {
EXPECT_EQ(dst[i], "item" + std::to_string(i));
}
}

} // namespace internal
} // namespace arrow
Loading