This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new a7b89073bd feat: add `reserve` to `Rows` (#9142)
a7b89073bd is described below
commit a7b89073bd428e5dafde872201ccda97ccd6bbed
Author: Raz Luvaton <[email protected]>
AuthorDate: Tue Jan 13 14:15:14 2026 +0200
feat: add `reserve` to `Rows` (#9142)
# Which issue does this PR close?
N/A
# Rationale for this change
allow to reserve so we can avoid reallocating
# What changes are included in this PR?
added `reserve` function to `Rows` + tests
# Are these changes tested?
yes
# Are there any user-facing changes?
yes
---
arrow-row/src/lib.rs | 40 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/arrow-row/src/lib.rs b/arrow-row/src/lib.rs
index 4cafbc2748..2f6ffd76d9 100644
--- a/arrow-row/src/lib.rs
+++ b/arrow-row/src/lib.rs
@@ -1130,6 +1130,12 @@ impl Rows {
self.offsets.push(self.buffer.len())
}
+ /// Reserve capacity for `row_capacity` rows with a total length of
`data_capacity`
+ pub fn reserve(&mut self, row_capacity: usize, data_capacity: usize) {
+ self.buffer.reserve(data_capacity);
+ self.offsets.reserve(row_capacity);
+ }
+
/// Returns the row at index `row`
pub fn row(&self, row: usize) -> Row<'_> {
assert!(row + 1 < self.offsets.len());
@@ -4283,4 +4289,38 @@ mod tests {
"{empty_rows_size_with_preallocate_data} should be larger than
{empty_rows_size_without_preallocate}"
);
}
+
+ #[test]
+ fn reserve_should_increase_capacity_to_the_requested_size() {
+ let row_converter =
RowConverter::new(vec![SortField::new(DataType::UInt8)]).unwrap();
+ let mut empty_rows = row_converter.empty_rows(0, 0);
+ empty_rows.reserve(50, 50);
+ let before_size = empty_rows.size();
+ empty_rows.reserve(50, 50);
+ assert_eq!(
+ empty_rows.size(),
+ before_size,
+ "Size should not change when reserving already reserved space"
+ );
+ empty_rows.reserve(10, 20);
+ assert_eq!(
+ empty_rows.size(),
+ before_size,
+ "Size should not change when already have space for the expected
reserved data"
+ );
+
+ empty_rows.reserve(100, 20);
+ assert!(
+ empty_rows.size() > before_size,
+ "Size should increase when reserving more space than previously
reserved"
+ );
+
+ let before_size = empty_rows.size();
+
+ empty_rows.reserve(20, 100);
+ assert!(
+ empty_rows.size() > before_size,
+ "Size should increase when reserving more space than previously
reserved"
+ );
+ }
}