MCPcopy Create free account
hub / github.com/apache/arrow-rs / get_slice_memory_size

Method get_slice_memory_size

arrow-data/src/data.rs:510–561  ·  view source on GitHub ↗

Returns the total number of the bytes of memory occupied by the buffers by this slice of [`ArrayData`] (See also diagram on [`ArrayData`]). This is approximately the number of bytes if a new [`ArrayData`] was formed by creating new [`Buffer`]s with exactly the data needed. For example, a [`DataType::Int64`] with `100` elements, [`Self::get_slice_memory_size`] would return `100 * 8 = 800`. If the

(&self)

Source from the content-addressed store, hash-verified

508 /// first `20` elements, then [`Self::get_slice_memory_size`] on the
509 /// sliced [`ArrayData`] would return `20 * 8 = 160`.
510 pub fn get_slice_memory_size(&self) -> Result<usize, ArrowError> {
511 let mut result: usize = 0;
512 let layout = layout(&self.data_type);
513
514 for spec in layout.buffers.iter() {
515 match spec {
516 BufferSpec::FixedWidth { byte_width, .. } => {
517 let buffer_size = self.len.checked_mul(*byte_width).ok_or_else(|| {
518 ArrowError::ComputeError(
519 "Integer overflow computing buffer size".to_string(),
520 )
521 })?;
522 result += buffer_size;
523 }
524 BufferSpec::VariableWidth => {
525 let buffer_len = match self.data_type {
526 DataType::Utf8 | DataType::Binary => {
527 let offsets = self.typed_offsets::<i32>()?;
528 (offsets[self.len] - offsets[0]) as usize
529 }
530 DataType::LargeUtf8 | DataType::LargeBinary => {
531 let offsets = self.typed_offsets::<i64>()?;
532 (offsets[self.len] - offsets[0]) as usize
533 }
534 _ => {
535 return Err(ArrowError::NotYetImplemented(format!(
536 "Invalid data type for VariableWidth buffer. Expected Utf8, LargeUtf8, Binary or LargeBinary. Got {}",
537 self.data_type
538 )));
539 }
540 };
541 result += buffer_len;
542 }
543 BufferSpec::BitMap => {
544 let buffer_size = bit_util::ceil(self.len, 8);
545 result += buffer_size;
546 }
547 BufferSpec::AlwaysNull => {
548 // Nothing to do
549 }
550 }
551 }
552
553 if self.nulls().is_some() {
554 result += bit_util::ceil(self.len, 8);
555 }
556
557 for child in &self.child_data {
558 result += child.get_slice_memory_size()?;
559 }
560 Ok(result)
561 }
562
563 /// Returns the total number of bytes of memory occupied
564 /// physically by this [`ArrayData`] and all its [`Buffer`]s and

Callers

nothing calls this directly

Calls 5

layoutFunction · 0.85
ceilFunction · 0.50
iterMethod · 0.45
checked_mulMethod · 0.45
nullsMethod · 0.45

Tested by

no test coverage detected